#!/usr/bin/env bash
# Nexus 6.0 — 一键更新（拉代码 + 重建镜像 + 重启）
#
# 已在 /opt/nexus:
#   bash deploy/update.sh
#   bash deploy/update.sh --no-cache    # 强制无缓存重建（entrypoint/Dockerfile 变更后）
#
# 远程一条命令（推荐在仓库目录执行，管道模式会回退 /opt/nexus）:
#   cd /opt/nexus && bash deploy/update.sh
#   curl -fsSL "http://66.154.115.8:3000/admin/Nexus/raw/branch/main/deploy/update.sh" | bash
#
set -euo pipefail

_resolve_nexus_root() {
    local src="${BASH_SOURCE[0]:-}"
    if [[ -n "$src" && "$src" != /dev/fd/* && "$src" != "-" && -f "$src" ]]; then
        cd "$(dirname "$(readlink -f "$src")")/.." && pwd
        return
    fi
    if [[ -d /opt/nexus ]]; then
        echo "警告: 通过管道执行无法解析脚本路径，已回退 /opt/nexus；推荐: cd /opt/nexus && bash deploy/update.sh" >&2
        echo /opt/nexus
        return
    fi
    echo "错误: 无法定位 Nexus 根目录；请 cd /opt/nexus && bash deploy/update.sh" >&2
    exit 1
}

ROOT="$(_resolve_nexus_root)"

NEXUS_ROOT="$ROOT" bash "$ROOT/deploy/install-nx-cli.sh" 2>/dev/null || true
exec bash "$ROOT/deploy/nexus-1panel.sh" upgrade "$@"
