#!/bin/bash

# ==========================================================
# ProxMenux - A menu-driven script for Proxmox VE management
# ==========================================================
# Author      : MacRimi
# Copyright   : (c) 2024-2025 MacRimi
# License     : GPL-3.0 (https://github.com/MacRimi/ProxMenux/blob/main/LICENSE)
# Version     : 1.2
# Last Updated: 18/03/2026
# ==========================================================
# Description:
# Main entry point for ProxMenux.
# - Loads configuration and utility functions.
# - Detects if running in Beta Program mode (develop branch).
# - Checks for updates from the appropriate branch (main or develop).
# - In beta mode: compares beta_version.txt; notifies when a stable
#   release is available and prompts the user to switch.
# - Launches the main menu.
# ==========================================================

# ── Configuration ──────────────────────────────────────────
BASE_DIR="/usr/local/share/proxmenux"
LOCAL_SCRIPTS="$BASE_DIR/scripts"
CONFIG_FILE="$BASE_DIR/config.json"
CACHE_FILE="$BASE_DIR/cache.json"
UTILS_FILE="$BASE_DIR/utils.sh"
LOCAL_VERSION_FILE="$BASE_DIR/version.txt"
BETA_VERSION_FILE="$BASE_DIR/beta_version.txt"
VENV_PATH="/opt/googletrans-env"

REPO_MAIN="https://raw.githubusercontent.com/MacRimi/ProxMenux/main"
REPO_DEVELOP="https://raw.githubusercontent.com/MacRimi/ProxMenux/develop"

# ── Load utilities ─────────────────────────────────────────
[[ -f "$UTILS_FILE" ]] && source "$UTILS_FILE"

: "${LOCAL_SCRIPTS:=/usr/local/share/proxmenux/scripts}"

# ── Detect beta mode ───────────────────────────────────────
# Returns 0 (true) if this install is part of the beta program.
is_beta() {
    [[ -f "$CONFIG_FILE" ]] || return 1
    local beta_flag
    beta_flag=$(jq -r '.beta_program.status // empty' "$CONFIG_FILE" 2>/dev/null)
    [[ "$beta_flag" == "active" ]]
}

# ── Recover broken Monitor unit before anything else ──────
#
# v1.2.2 changed the AppImage layout: the binary is extracted to
# /usr/local/share/proxmenux/monitor-app/ and the systemd unit
# executes AppRun out of that directory. The install_proxmenux.sh
# update path before v1.2.2.1 only rewrote the unit on fresh installs,
# so every user updating from v1.2.1 stable inherited the old unit
# whose ExecStart still pointed at the bare AppImage. On PVE 9.x /
# Debian 13 that bare AppImage hits status=203/EXEC immediately and
# the service enters the activating loop reported in #222.
#
# Re-running the new installer fixes it permanently, but the update
# prompt below uses --defaultno so a user pressing Enter by reflex
# stays broken. Patch the unit defensively at every menu launch: if
# the bug's exact fingerprint is present (unit ExecStart matches the
# bare AppImage path AND the extracted AppRun already exists) we
# silently rewrite the unit and bounce the service. The check is a
# no-op for healthy installs and for hosts that never installed the
# Monitor at all, so it's safe to run unconditionally.
auto_repair_monitor_unit() {
    local unit_file="/etc/systemd/system/proxmenux-monitor.service"
    local extracted_runtime="/usr/local/share/proxmenux/monitor-app"
    local apprun="$extracted_runtime/AppRun"
    local bare_appimage="/usr/local/share/proxmenux/ProxMenux-Monitor.AppImage"

    [[ -f "$unit_file" && -x "$apprun" ]] || return 0
    grep -q "^ExecStart=${bare_appimage}\$" "$unit_file" || return 0

    local port working_dir
    port=$(awk -F'"' '/^Environment="PORT=/ {print $2; exit}' "$unit_file" 2>/dev/null \
        | sed 's/^PORT=//')
    [[ -z "$port" ]] && port="8008"
    working_dir=$(awk -F'=' '/^WorkingDirectory=/ {print $2; exit}' "$unit_file" 2>/dev/null)
    [[ -z "$working_dir" ]] && working_dir="/usr/local/share/proxmenux"

    cat > "$unit_file" <<EOF
[Unit]
Description=ProxMenux Monitor - Web Dashboard
After=network.target

[Service]
Type=simple
User=root
WorkingDirectory=$working_dir
ExecStart=$apprun
Restart=on-failure
RestartSec=10
Environment="PORT=$port"

[Install]
WantedBy=multi-user.target
EOF

    systemctl daemon-reload >/dev/null 2>&1
    systemctl restart proxmenux-monitor.service >/dev/null 2>&1
    sleep 2

    if systemctl is-active --quiet proxmenux-monitor.service 2>/dev/null; then
        type msg_ok >/dev/null 2>&1 \
            && msg_ok "$(translate 'ProxMenux Monitor unit repaired and restarted')" \
            || echo "[ProxMenux] Monitor unit repaired and restarted"
    fi
}

# ── Check for updates ──────────────────────────────────────
check_updates() {
    if is_beta; then
        check_updates_beta
    else
        check_updates_stable
    fi
}

# ── Stable update check (main branch) ─────────────────────
check_updates_stable() {
    local VERSION_URL="$REPO_MAIN/version.txt"
    local INSTALL_URL="$REPO_MAIN/install_proxmenux.sh"
    local INSTALL_SCRIPT="$BASE_DIR/install_proxmenux.sh"

    [[ ! -f "$LOCAL_VERSION_FILE" ]] && return 0

    local REMOTE_VERSION LOCAL_VERSION
    REMOTE_VERSION="$(curl -fsSL "$VERSION_URL" 2>/dev/null | head -n 1)"
    [[ -z "$REMOTE_VERSION" ]] && return 0

    LOCAL_VERSION="$(head -n 1 "$LOCAL_VERSION_FILE" 2>/dev/null)"
    [[ -z "$LOCAL_VERSION" ]] && return 0
    [[ "$LOCAL_VERSION" = "$REMOTE_VERSION" ]] && return 0

    # Extract the translated prompt strings into variables FIRST so the
    # whiptail line below is trivially parseable. A user on the 1.2.2
    # update path hit:
    #   menu: line 138: syntax error near unexpected token `$REMOTE_VERSION'
    # The original inline form was technically valid bash, but a
    # translate() return that contains a stray quote or paren is enough
    # to confuse a partially-rewritten file (race during update) or a
    # corrupted download. Splitting the strings out closes the entire
    # parsing-risk surface for zero behavioural change.
    local PROMPT_TITLE PROMPT_AVAIL PROMPT_ASK
    PROMPT_TITLE="$(translate 'Update Available')"
    PROMPT_AVAIL="$(translate 'New version available')"
    PROMPT_ASK="$(translate 'Do you want to update now?')"

    # Running inside the Monitor's WebSocket terminal: the installer will
    # restart the Monitor service, which kills this shell mid-install and
    # leaves the update broken. Inform and route to SSH / host console.
    if [[ "${PROXMENUX_TERMINAL:-}" == "monitor" ]]; then
        local WS_INFO
        WS_INFO="$(translate 'A new ProxMenux version is available:') ${REMOTE_VERSION}

$(translate 'This session is running in the Monitor terminal. Updating from here would restart the Monitor service and cut the connection mid-install, leaving the update in a broken state.')

$(translate 'Run the update from an SSH session or the Proxmox host console with:')

  bash -c \"\$(wget -qLO - ${INSTALL_URL})\"

$(translate 'You can keep using ProxMenux from this terminal.')"
        whiptail --title "$PROMPT_TITLE" --msgbox "$WS_INFO" 20 78
        return 0
    fi

    if whiptail --title "$PROMPT_TITLE" \
                --yesno "$PROMPT_AVAIL ($REMOTE_VERSION)\n\n$PROMPT_ASK" \
                10 60 --defaultno; then

        msg_warn "$(translate 'Starting ProxMenux update...')"

        if curl -fsSL "$INSTALL_URL" -o "$INSTALL_SCRIPT"; then
            chmod +x "$INSTALL_SCRIPT"
            # Replace this shell before the installer refreshes /usr/local/bin/menu.
            exec bash "$INSTALL_SCRIPT" --update
        fi
    fi
}

# ── Beta-mode update check (main + develop) ───────────────
# When the beta program is active, check BOTH channels. The stable check
# is delegated to check_updates_stable (same prompt, same installer). After
# that we only need the beta-specific part: develop vs beta_version.txt.
check_updates_beta() {
    # 1. Stable release on main — reuse the non-beta path.
    check_updates_stable

    # 2. Beta build on develop.
    [[ ! -f "$BETA_VERSION_FILE" ]] && return 0

    local REMOTE_BETA LOCAL_BETA
    REMOTE_BETA="$(curl -fsSL "$REPO_DEVELOP/beta_version.txt" 2>/dev/null | head -n 1)"
    LOCAL_BETA="$(head -n 1 "$BETA_VERSION_FILE" 2>/dev/null)"
    [[ -z "$REMOTE_BETA" || -z "$LOCAL_BETA" || "$LOCAL_BETA" = "$REMOTE_BETA" ]] && return 0
    [[ "$(printf '%s\n%s\n' "$LOCAL_BETA" "$REMOTE_BETA" | sort -V | tail -1)" = "$REMOTE_BETA" ]] || return 0

    if [[ "${PROXMENUX_TERMINAL:-}" == "monitor" ]]; then
        whiptail --title "Beta Update Available" --msgbox "\
A new beta build is available: $REMOTE_BETA

This session is running in the Monitor terminal. Updating from here would restart the Monitor service and cut the connection mid-install, leaving the update in a broken state.

Run the update from an SSH session or the Proxmox host console with:

  bash -c \"\$(wget -qLO - $REPO_DEVELOP/install_proxmenux_beta.sh)\"

You can keep using ProxMenux from this terminal." 20 78
        return 0
    fi

    if whiptail --title "Beta Update Available" \
                --yesno "A new beta build is available!\n\nInstalled beta : $LOCAL_BETA\nNew beta build : $REMOTE_BETA\n\nDo you want to update now?" \
                12 64 --defaultno; then

        msg_warn "Updating to beta build $REMOTE_BETA ..."

        local INSTALL_BETA_SCRIPT="$BASE_DIR/install_proxmenux_beta.sh"
        if curl -fsSL "$REPO_DEVELOP/install_proxmenux_beta.sh" -o "$INSTALL_BETA_SCRIPT"; then
            chmod +x "$INSTALL_BETA_SCRIPT"
            # Replace this shell before the installer refreshes /usr/local/bin/menu.
            exec bash "$INSTALL_BETA_SCRIPT" --update
        else
            msg_error "Could not download the beta installer from the develop branch."
        fi
    fi
}

# ── Main ───────────────────────────────────────────────────
main_menu() {
    local MAIN_MENU="$LOCAL_SCRIPTS/menus/main_menu.sh"
    exec bash "$MAIN_MENU"
}

# `menu -v` / `-h` print info and exit without opening the TUI so
# admins can query the install over SSH (issue #240).
case "${1:-}" in
    -v|--version|-V)
        local_ver="unknown"
        [[ -f "$LOCAL_VERSION_FILE" ]] && local_ver="$(<"$LOCAL_VERSION_FILE")"
        printf 'ProxMenux %s\n' "$local_ver"
        if is_beta && [[ -f "$BETA_VERSION_FILE" ]]; then
            printf 'Beta build: %s\n' "$(<"$BETA_VERSION_FILE")"
        fi
        exit 0
        ;;
    -h|--help)
        cat <<'EOF'
Usage: menu [OPTION]

Interactive menu for Proxmox VE management.

Options:
  -v, --version   Print installed ProxMenux version and exit.
  -h, --help      Show this help and exit.

Run without arguments to launch the interactive menu.
EOF
        exit 0
        ;;
esac

load_language
initialize_cache
auto_repair_monitor_unit
check_updates
main_menu
