#!/usr/bin/env bash
# Sync install wizard static files into the running Nexus prod container.
# Primary fix: Dockerfile.prod bakes these into the image on build.
# This script is a safety net after upgrade if an old image is still running.
set -euo pipefail

NEXUS_ROOT="${NEXUS_ROOT:-/opt/nexus}"
NEXUS_PUBLISH_PORT="${NEXUS_PUBLISH_PORT:-8600}"
CONTAINER="${NEXUS_CONTAINER:-}"

info() { echo -e "\033[0;32m[INFO]\033[0m  $*"; }
warn() { echo -e "\033[1;33m[WARN]\033[0m  $*"; }
error() { echo -e "\033[0;31m[ERROR]\033[0m $*" >&2; }

resolve_container() {
    if [[ -n "$CONTAINER" ]]; then
        return 0
    fi
    CONTAINER="$(docker ps --format '{{.Names}}' | grep -E 'nexus-prod-nexus|nexus-nexus-1' | head -1 || true)"
    if [[ -z "$CONTAINER" ]]; then
        error "未找到运行中的 Nexus 容器（可设 NEXUS_CONTAINER=名称）"
        exit 1
    fi
    info "容器: $CONTAINER"
}

sync_file() {
    local src="$1" dest="$2"
    if [[ ! -f "$src" ]]; then
        error "缺少源文件: $src"
        exit 1
    fi
    docker exec "$CONTAINER" mkdir -p "$(dirname "$dest")"
    docker cp "$src" "${CONTAINER}:${dest}"
}

wizard_archived_in_container() {
    docker exec "$CONTAINER" test -f /app/.install_locked 2>/dev/null \
        || docker exec "$CONTAINER" test -f /var/lib/nexus/.install_locked 2>/dev/null \
        || docker exec "$CONTAINER" test -f /app/web/app/install.html.bak 2>/dev/null
}

main() {
    local app="$NEXUS_ROOT/web/app"
    local wizard_archived=false
    if [[ ! -f "$app/install.html" ]]; then
        error "仓库中无 $app/install.html，请先 git pull"
        exit 1
    fi
    resolve_container
    if wizard_archived_in_container; then
        wizard_archived=true
        info "安装已锁定或向导已归档，跳过静态/Python 热同步"
        local code
        code="$(curl -s -o /dev/null -w '%{http_code}' "http://127.0.0.1:${NEXUS_PUBLISH_PORT}/app/install.html" 2>/dev/null || echo 000)"
        if [[ "$code" == "404" ]]; then
            info "/app/install.html 已归档 → HTTP 404"
            exit 0
        fi
        warn "/app/install.html 应已归档但返回 HTTP $code"
        exit 1
    fi

    info "同步安装向导静态文件..."
    sync_file "$app/install.html" /app/web/app/install.html
    sync_file "$app/vendor/tailwindcss-browser.js" /app/web/app/vendor/tailwindcss-browser.js
    sync_file "$app/vendor/theme-init.js" /app/web/app/vendor/theme-init.js
    sync_file "$app/vendor/theme.css" /app/web/app/vendor/theme.css
    sync_file "$app/vendor/alpinejs.min.js" /app/web/app/vendor/alpinejs.min.js

    local install_py="$NEXUS_ROOT/server/api/install.py" code
    if [[ -f "$install_py" ]]; then
        info "同步 install.py（Docker Redis env-check 修复）..."
        sync_file "$install_py" /app/server/api/install.py
        info "重启 Nexus 容器以加载 Python 变更..."
        docker restart "$CONTAINER" >/dev/null
        for _ in $(seq 1 30); do
            if curl -sf "http://127.0.0.1:${NEXUS_PUBLISH_PORT}/health" >/dev/null 2>&1; then
                break
            fi
            sleep 2
        done
    else
        warn "未找到 $install_py，跳过 Python 热同步"
    fi

    code="$(curl -s -o /dev/null -w '%{http_code}' "http://127.0.0.1:${NEXUS_PUBLISH_PORT}/app/install.html" 2>/dev/null || echo 000)"
    if [[ "$code" == "200" ]]; then
        info "/app/install.html → HTTP 200"
    else
        warn "/app/install.html → HTTP $code（请检查 Nexus 是否在 ${NEXUS_PUBLISH_PORT} 监听）"
        exit 1
    fi
}

main "$@"
