#!/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" ]]
}

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

# ── Safe self-update ──────────────────────────────────────
# Validates the downloaded installer and rolls back /usr/local/bin/menu
# if the post-install launcher fails to parse. Why: a corrupted download
# or CRLF contamination has already left users with an unusable launcher;
# without a rollback the only recovery is curl-reinstall by hand.
run_installer_safely() {
    local installer_url="$1"
    local installer_dest="$2"
    local launcher="/usr/local/bin/menu"
    local tmp_installer="/tmp/proxmenux_installer_$$.sh"
    local menu_backup="/tmp/proxmenux_menu_backup_$$"

    if ! curl -fsSL "$installer_url" -o "$tmp_installer"; then
        msg_error "Failed to download installer from $installer_url"
        return 1
    fi

    sed -i 's/\r$//' "$tmp_installer" 2>/dev/null || true

    if ! bash -n "$tmp_installer" 2>/dev/null; then
        msg_error "Downloaded installer has invalid syntax. Update aborted."
        rm -f "$tmp_installer"
        return 1
    fi

    [[ -f "$launcher" ]] && cp -f "$launcher" "$menu_backup"

    chmod +x "$tmp_installer"
    if [[ -n "$installer_dest" ]]; then
        cp -f "$tmp_installer" "$installer_dest"
        chmod +x "$installer_dest"
        bash "$installer_dest" --update
    else
        bash "$tmp_installer"
    fi
    local rc=$?

    if [[ -f "$launcher" ]] && ! bash -n "$launcher" 2>/dev/null; then
        msg_error "New launcher is broken. Restoring previous version."
        if [[ -f "$menu_backup" ]]; then
            cp -f "$menu_backup" "$launcher"
            sed -i 's/\r$//' "$launcher" 2>/dev/null || true
            chmod +x "$launcher"
        fi
        rc=1
    fi

    rm -f "$tmp_installer" "$menu_backup"
    return $rc
}

# ── 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

    if whiptail --title "$(translate 'Update Available')" \
                --yesno "$(translate 'New version available') ($REMOTE_VERSION)\n\n$(translate 'Do you want to update now?')" \
                10 60 --defaultno; then

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

        run_installer_safely "$INSTALL_URL" "$INSTALL_SCRIPT"
    fi
}

# ── Beta update check (develop branch) ────────────────────
# Beta users track BOTH channels independently: a new stable on main is
# still relevant to them (ends the beta cycle), and a new beta on develop
# is their normal update path. We prompt for each one in turn — declining
# stable doesn't skip the beta prompt.
check_updates_beta() {
    local STABLE_VERSION_URL="$REPO_MAIN/version.txt"
    local BETA_VERSION_URL="$REPO_DEVELOP/beta_version.txt"
    local INSTALL_STABLE_URL="$REPO_MAIN/install_proxmenux.sh"
    local INSTALL_BETA_URL="$REPO_DEVELOP/install_proxmenux_beta.sh"
    local INSTALL_STABLE_SCRIPT="$BASE_DIR/install_proxmenux.sh"
    local INSTALL_BETA_SCRIPT="$BASE_DIR/install_proxmenux_beta.sh"

    local REMOTE_STABLE LOCAL_STABLE REMOTE_BETA LOCAL_BETA
    REMOTE_STABLE="$(curl -fsSL "$STABLE_VERSION_URL" 2>/dev/null | head -n 1 | tr -d '[:space:]')"
    LOCAL_STABLE="$(head -n 1 "$LOCAL_VERSION_FILE" 2>/dev/null | tr -d '[:space:]')"
    REMOTE_BETA="$(curl -fsSL "$BETA_VERSION_URL" 2>/dev/null | head -n 1 | tr -d '[:space:]')"
    LOCAL_BETA="$(head -n 1 "$BETA_VERSION_FILE" 2>/dev/null | tr -d '[:space:]')"

    # ── 1. Stable release on main ──
    if [[ -n "$LOCAL_STABLE" && -n "$REMOTE_STABLE" && "$LOCAL_STABLE" != "$REMOTE_STABLE" ]] && \
       [[ "$(printf '%s\n%s\n' "$LOCAL_STABLE" "$REMOTE_STABLE" | sort -V | tail -1)" = "$REMOTE_STABLE" ]]; then
        if whiptail --title "🎉 Stable Release Available" \
                    --yesno "A new ProxMenux stable release is available on main.\n\nInstalled stable : $LOCAL_STABLE\nNew stable       : $REMOTE_STABLE\nYour beta        : ${LOCAL_BETA:-n/a}\n\nInstall the stable release now?\n\n(Choosing 'No' keeps your beta — any beta update is offered next.)" \
                    15 70 --defaultno; then
            msg_warn "Installing stable release $REMOTE_STABLE ..."
            run_installer_safely "$INSTALL_STABLE_URL" "$INSTALL_STABLE_SCRIPT"
            return 0
        fi
    fi

    # ── 2. Beta build on develop ──
    if [[ -n "$LOCAL_BETA" && -n "$REMOTE_BETA" && "$LOCAL_BETA" != "$REMOTE_BETA" ]] && \
       [[ "$(printf '%s\n%s\n' "$LOCAL_BETA" "$REMOTE_BETA" | sort -V | tail -1)" = "$REMOTE_BETA" ]]; then
        if whiptail --title "Beta Update Available" \
                    --yesno "A new beta build is available on the develop branch.\n\nInstalled beta : $LOCAL_BETA\nNew beta build : $REMOTE_BETA\n\nUpdate the beta now?" \
                    13 68 --defaultno; then
            msg_warn "Updating to beta build $REMOTE_BETA ..."
            run_installer_safely "$INSTALL_BETA_URL" "$INSTALL_BETA_SCRIPT"
        fi
    fi
}

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

load_language
initialize_cache
check_updates
main_menu
