#!/usr/bin/env bash set -euo pipefail # ── Resolve GNIZA_DIR ──────────────────────────────────────── GNIZA_DIR="$(cd "$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")/.." && pwd)" export GNIZA_DIR # ── Source libraries in dependency order ───────────────────── source "$GNIZA_DIR/lib/constants.sh" source "$GNIZA_DIR/lib/utils.sh" detect_mode source "$GNIZA_DIR/lib/logging.sh" source "$GNIZA_DIR/lib/config.sh" source "$GNIZA_DIR/lib/locking.sh" source "$GNIZA_DIR/lib/targets.sh" source "$GNIZA_DIR/lib/remotes.sh" source "$GNIZA_DIR/lib/backup.sh" source "$GNIZA_DIR/lib/mysql.sh" source "$GNIZA_DIR/lib/restore.sh" source "$GNIZA_DIR/lib/retention.sh" source "$GNIZA_DIR/lib/schedule.sh" source "$GNIZA_DIR/lib/notify.sh" source "$GNIZA_DIR/lib/ssh.sh" source "$GNIZA_DIR/lib/rclone.sh" source "$GNIZA_DIR/lib/snapshot.sh" source "$GNIZA_DIR/lib/transfer.sh" # ── Help ───────────────────────────────────────────────────── show_help() { cat </dev/null | head -1) if [[ -z "$latest" ]]; then echo "No log files found." else if [[ -n "$tail_n" ]]; then tail -n "$tail_n" "$latest" else cat "$latest" fi fi else ls -lt "$log_dir"/gniza-*.log 2>/dev/null || echo "No log files found." fi ;; version) echo "gniza v${GNIZA4LINUX_VERSION}" ;; "") show_help ;; *) die "Unknown command: $SUBCOMMAND (see --help)" ;; esac } # ── Mode selection ─────────────────────────────────────────── if [[ "$SUBCOMMAND" == "web" ]]; then _web_action="${SUBCMD_ARGS[0]:-start}" case "$_web_action" in start) _web_port="" _web_host="" _web_port=$(_parse_flag "--port" "${SUBCMD_ARGS[@]+"${SUBCMD_ARGS[@]}"}") || true _web_host=$(_parse_flag "--host" "${SUBCMD_ARGS[@]+"${SUBCMD_ARGS[@]}"}") || true _web_args=() [[ -n "$_web_port" ]] && _web_args+=(--port="$_web_port") [[ -n "$_web_host" ]] && _web_args+=(--host="$_web_host") PYTHONPATH="$GNIZA_DIR:${PYTHONPATH:-}" exec python3 -m web "${_web_args[@]}" ;; install-service) _service_src="$GNIZA_DIR/etc/gniza-web.service" _service_dst="/etc/systemd/system/gniza-web.service" if [[ ! -f "$_service_src" ]]; then die "Service file not found: $_service_src" fi cp "$_service_src" "$_service_dst" systemctl daemon-reload systemctl enable gniza-web systemctl start gniza-web echo "GNIZA web service installed and started." echo "Access the dashboard at http://$(hostname -I | awk '{print $1}'):8080" ;; remove-service) systemctl stop gniza-web 2>/dev/null || true systemctl disable gniza-web 2>/dev/null || true rm -f /etc/systemd/system/gniza-web.service systemctl daemon-reload echo "GNIZA web service removed." ;; status) systemctl status gniza-web 2>/dev/null || echo "GNIZA web service is not installed." ;; *) die "Unknown web action: $_web_action (expected start|install-service|remove-service|status)" ;; esac elif [[ -n "$SUBCOMMAND" ]]; then # Explicit subcommand: always CLI run_cli elif [[ "$FORCE_CLI" == "true" ]]; then run_cli elif python3 -c "import textual" 2>/dev/null && [[ -t 1 ]]; then # Python Textual TUI mode PYTHONPATH="$GNIZA_DIR:${PYTHONPATH:-}" exec python3 -m tui "$@" else # Fallback to CLI help run_cli fi