#!/usr/bin/env bash
# CPN Control Panel Network: upgrade cpn-installer from GitHub Releases.
# Preferred: curl -fsSL https://cpn.newstargeted.com/upgrade.sh | bash
# Fallback:  curl -fsSL https://raw.githubusercontent.com/Control-Panel-Network/CPN-Control-Panel-Network/stable/scripts/upgrade.sh | bash
# Alias: repo-root / scripts preUpgrade.sh. Shared helpers: scripts/cpn-bootstrap-lib.sh
# Env/CLI: CPN_RELEASE_TAG, CPN_BRANCH, -b/--ref, --bypass (see cpn-bootstrap-lib.sh --help via upgrade.sh --help)
set -euo pipefail

CPN_GITHUB_REPO="${CPN_GITHUB_REPO:-Control-Panel-Network/CPN-Control-Panel-Network}"
CPN_REQUIRE_GPG="${CPN_REQUIRE_GPG:-1}"
CPN_ALLOW_UNSIGNED="${CPN_ALLOW_UNSIGNED:-0}"
CPN_STABLE_ONLY="${CPN_STABLE_ONLY:-0}"
CPN_EXPECTED_FPR="${CPN_EXPECTED_FPR:-FE70B9718F63B10BB70A6F70BECBB7488AE5C3E5}"
CPN_REF_ARG="${CPN_REF_ARG:-}"
CPN_BOOTSTRAP_LIB_REF="${CPN_BOOTSTRAP_LIB_REF:-stable}"
API_BASE="https://api.github.com/repos/${CPN_GITHUB_REPO}"
RAW_KEY_URL="https://raw.githubusercontent.com/${CPN_GITHUB_REPO}/stable/packaging/RPM-GPG-KEY-CPN"

die() { echo "CPN upgrade error: $*" >&2; exit 1; }
info() { echo "CPN: $*"; }

require_root() {
  if [[ "$(id -u)" -ne 0 ]]; then
    die "root is required (re-run with sudo or as root)"
  fi
}

require_https_url() {
  local url="$1"
  [[ "$url" == https://* ]] || die "refusing non-HTTPS URL: $url"
}

have_cmd() { command -v "$1" >/dev/null 2>&1; }

cpn_peek_ref_arg() {
  local -a args=("$@")
  local i
  for ((i = 0; i < ${#args[@]}; i++)); do
    case "${args[$i]}" in
      -b|--branch|--ref)
        if ((i + 1 < ${#args[@]})); then
          CPN_BOOTSTRAP_LIB_REF="${args[$((i + 1))]}"
          CPN_REF_ARG="${args[$((i + 1))]}"
          export CPN_BOOTSTRAP_LIB_REF CPN_REF_ARG
        fi
        ;;
    esac
  done
}

cpn_source_bootstrap_lib() {
  local ref="${CPN_BOOTSTRAP_LIB_REF:-stable}"
  local here candidate url
  here="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" 2>/dev/null && pwd || true)"
  for candidate in \
    "${here}/cpn-bootstrap-lib.sh" \
    "./scripts/cpn-bootstrap-lib.sh" \
    "./cpn-bootstrap-lib.sh"; do
    if [[ -n "$candidate" && -f "$candidate" ]]; then
      # shellcheck disable=SC1090
      . "$candidate"
      return 0
    fi
  done
  for url in \
    "https://cpn.newstargeted.com/cpn-bootstrap-lib.sh" \
    "https://raw.githubusercontent.com/${CPN_GITHUB_REPO}/${ref}/scripts/cpn-bootstrap-lib.sh" \
    "https://raw.githubusercontent.com/${CPN_GITHUB_REPO}/stable/scripts/cpn-bootstrap-lib.sh"; do
    if have_cmd curl && . <(curl -fsSL --proto '=https' --tlsv1.2 --max-time 30 "$url"); then
      return 0
    fi
  done
  die "could not load cpn-bootstrap-lib.sh (host or GitHub raw)"
}

download() {
  local url="$1" dest="$2"
  require_https_url "$url"
  if have_cmd curl; then
    curl -fsSL --proto '=https' --tlsv1.2 --max-time 120 -o "$dest" "$url" \
      || die "download failed: $url"
  elif have_cmd wget; then
    wget -qO "$dest" --https-only --secure-protocol=TLSv1_2 "$url" \
      || die "download failed: $url"
  else
    die "curl or wget is required"
  fi
}

download_text() {
  local url="$1"
  require_https_url "$url"
  local -a hdr=(
    -H "Accept: application/vnd.github+json"
    -H "User-Agent: CPN-upgrade.sh"
  )
  local tok=""
  if [[ -n "${CPN_GITHUB_TOKEN:-}" ]]; then
    tok="${CPN_GITHUB_TOKEN}"
  elif [[ -n "${GITHUB_TOKEN:-}" ]]; then
    tok="${GITHUB_TOKEN}"
  elif [[ -r "${CPN_DATA_DIR:-/var/lib/cpn}/secrets/github-token" ]]; then
    tok="$(tr -d '\r\n' < "${CPN_DATA_DIR:-/var/lib/cpn}/secrets/github-token")"
  fi
  if [[ -n "$tok" ]]; then
    hdr+=(-H "Authorization: Bearer ${tok}")
  fi
  if have_cmd curl; then
    curl -fsSL --proto '=https' --tlsv1.2 --max-time 60 \
      "${hdr[@]}" \
      "$url"
  elif have_cmd wget; then
    local -a wh=(--header="Accept: application/vnd.github+json" --header="User-Agent: CPN-upgrade.sh")
    if [[ -n "$tok" ]]; then
      wh+=(--header="Authorization: Bearer ${tok}")
    fi
    wget -qO- --https-only "${wh[@]}" "$url"
  else
    die "curl or wget is required"
  fi
}

# Build a minimal GitHub-like release JSON from CDN SHA256SUMS (bypasses api.github.com).
direct_release_json_for_tag() {
  local tag="$1"
  [[ -n "$tag" ]] || return 1
  case "$tag" in
    v*|V*) ;;
    *) tag="v${tag}" ;;
  esac
  local base="https://github.com/${CPN_GITHUB_REPO}/releases/download/${tag}"
  local sums
  sums="$(curl -fsSL --proto '=https' --tlsv1.2 --max-time 30 -H 'User-Agent: CPN-upgrade.sh' "${base}/SHA256SUMS" 2>/dev/null)" \
    || return 1
  [[ -n "$sums" ]] || return 1
  printf '%s' "$sums" | TAG="$tag" REPO="$CPN_GITHUB_REPO" BASE="$base" python3 -c '
import json,os,sys
tag=os.environ["TAG"]
repo=os.environ["REPO"]
base=os.environ["BASE"].rstrip("/")
names=[]
for line in sys.stdin:
    line=line.strip()
    if not line or line.startswith("#"):
        continue
    parts=line.split()
    if len(parts) < 2:
        continue
    name=parts[1].lstrip("*")
    if name and name not in names:
        names.append(name)
for extra in ("SHA256SUMS","SHA256SUMS.asc","RPM-GPG-KEY-CPN"):
    if extra not in names:
        names.append(extra)
assets=[{"name":n,"browser_download_url":f"{base}/{n}","content_type":"application/octet-stream","size":0} for n in names]
print(json.dumps({
  "tag_name": tag,
  "name": tag,
  "draft": False,
  "prerelease": ("alpha" in tag or "beta" in tag or "rc" in tag),
  "published_at": "",
  "html_url": f"https://github.com/{repo}/releases/tag/{tag}",
  "assets": assets,
}))
'
}

sha256_file() {
  local path="$1"
  if have_cmd sha256sum; then
    sha256sum "$path" | awk '{print $1}'
  elif have_cmd shasum; then
    shasum -a 256 "$path" | awk '{print $1}'
  else
    die "sha256sum or shasum is required"
  fi
}

require_existing_install() {
  if ! have_cmd cpn-installer && [[ ! -x /usr/bin/cpn-installer ]]; then
    die "cpn-installer is not installed. Use https://cpn.newstargeted.com/install.sh (GitHub raw fallback available) for a new install."
  fi
}

detect_guest() {
  [[ -r /etc/os-release ]] || die "missing /etc/os-release; unsupported OS"
  # shellcheck disable=SC1091
  . /etc/os-release
  local id="${ID:-}" id_like="${ID_LIKE:-}" version_id="${VERSION_ID:-}"
  local major="${version_id%%.*}"
  local arch
  arch="$(uname -m)"

  FAMILY=""
  EL_MAJOR=""
  PKG_ARCH=""
  DEB_ARCH=""
  DIST_LABEL=""

  case "$arch" in
    x86_64|amd64) PKG_ARCH="x86_64"; DEB_ARCH="amd64" ;;
    aarch64|arm64) PKG_ARCH="aarch64"; DEB_ARCH="arm64" ;;
    *) die "unsupported architecture: $arch" ;;
  esac

  case "$id" in
    almalinux|rocky|rhel|centos|cloudlinux|ol|eurolinux|scientific)
      FAMILY="dnf"
      EL_MAJOR="$major"
      DIST_LABEL="${id} ${version_id}"
      ;;
    ubuntu|debian)
      FAMILY="apt"
      DIST_LABEL="${id} ${version_id}"
      ;;
    *)
      case " $id_like " in
        *" rhel "*|*" centos "*|*" fedora "*)
          FAMILY="dnf"
          EL_MAJOR="$major"
          DIST_LABEL="${id} ${version_id}"
          ;;
        *" debian "*)
          FAMILY="apt"
          DIST_LABEL="${id} ${version_id}"
          ;;
        *)
          die "unknown or unsupported OS (ID=${id:-?}). See docs/SUPPORT.md"
          ;;
      esac
      ;;
  esac

  if [[ "$FAMILY" == "dnf" ]]; then
    [[ "$EL_MAJOR" =~ ^[0-9]+$ ]] || die "could not parse Enterprise Linux major from VERSION_ID=${version_id}"
    if [[ "$EL_MAJOR" -lt 9 ]]; then
      die "EL${EL_MAJOR} has no native CPN release RPM. Upgrade the guest OS or install from a supported release asset."
    fi
    if [[ "$EL_MAJOR" -gt 10 ]]; then
      die "unsupported Enterprise Linux major: ${EL_MAJOR}"
    fi
  fi
}

pick_release_json() {
  local json body
  if [[ -n "${CPN_RELEASE_TAG:-}" ]]; then
    body="$(download_text "${API_BASE}/releases/tags/${CPN_RELEASE_TAG}" 2>/dev/null || true)"
    if [[ -z "$body" ]]; then
      info "API tag lookup failed for ${CPN_RELEASE_TAG}; trying direct download URLs"
      body="$(direct_release_json_for_tag "${CPN_RELEASE_TAG}")" \
        || die "release tag not found: ${CPN_RELEASE_TAG}"
    fi
    printf '%s' "$body"
    return
  fi
  body="$(download_text "${API_BASE}/releases?per_page=30" 2>/dev/null || true)"
  if [[ -z "$body" ]]; then
    info "GitHub Releases API list failed (rate limit or access denied); trying direct tip download URLs"
    local tip
    for tip in "v0.2.6-alpha.28" "v0.2.6-alpha.27" "v0.2.6-alpha.26" "v0.2.6-alpha.25"; do
      if body="$(direct_release_json_for_tag "$tip")"; then
        info "resolved tip via direct download: $tip"
        printf '%s' "$body"
        return
      fi
    done
    die "could not list GitHub Releases (set CPN_GITHUB_TOKEN or ${CPN_DATA_DIR:-/var/lib/cpn}/secrets/github-token, or CPN_RELEASE_TAG)"
  fi
  # Skip published tags that still have no matching package assets (release workflow mid-run).
  json="$(printf '%s' "$body" | FAMILY="$FAMILY" EL_MAJOR="${EL_MAJOR:-}" PKG_ARCH="$PKG_ARCH" DEB_ARCH="$DEB_ARCH" python3 -c '
import json,sys,os,re
items=json.load(sys.stdin)
stable_only=os.environ.get("CPN_STABLE_ONLY","0").strip()=="1"
include_pre_legacy=os.environ.get("CPN_INCLUDE_PRERELEASE","1").strip()
include_pre = (not stable_only) and include_pre_legacy != "0"
family=os.environ.get("FAMILY","").strip()
el_major=os.environ.get("EL_MAJOR","").strip()
pkg_arch=os.environ.get("PKG_ARCH","").strip()
deb_arch=os.environ.get("DEB_ARCH","").strip()

def has_package(rel):
    names=[a.get("name","") for a in (rel.get("assets") or [])]
    if not names:
        return False
    if "SHA256SUMS" not in names:
        return False
    if family=="dnf":
        pat=re.compile(r"^cpn-installer-.*\.el%s\.%s\.rpm$" % (re.escape(el_major), re.escape(pkg_arch)))
        return any(pat.match(n) for n in names)
    suffix="_%s.deb" % deb_arch
    return any(n.startswith("cpn-installer_") and n.endswith(suffix) for n in names)

for item in items:
    if item.get("draft"):
        continue
    if item.get("prerelease") and not include_pre:
        continue
    if not has_package(item):
        continue
    print(json.dumps(item))
    break
else:
    sys.exit(2)
')" || die "no matching GitHub release with packages for this OS (set CPN_RELEASE_TAG, or wait for the Release workflow to finish uploading assets)"
  printf '%s' "$json"
}

asset_url_by_name() {
  local release_json="$1" name="$2"
  printf '%s' "$release_json" | python3 -c '
import json,sys
wanted=sys.argv[1]
rel=json.load(sys.stdin)
for asset in rel.get("assets") or []:
    if asset.get("name")==wanted:
        url=asset.get("browser_download_url") or ""
        if not url.startswith("https://"):
            raise SystemExit(2)
        print(url)
        raise SystemExit(0)
raise SystemExit(1)
' "$name"
}

select_package_name() {
  local release_json="$1"
  printf '%s' "$release_json" | python3 -c '
import json,sys,re
family, el_major, pkg_arch, deb_arch = sys.argv[1:5]
rel=json.load(sys.stdin)
names=[a.get("name","") for a in (rel.get("assets") or [])]
if family=="dnf":
    pat=re.compile(r"^cpn-installer-.*\.el%s\.%s\.rpm$" % (re.escape(el_major), re.escape(pkg_arch)))
    for name in names:
        if pat.match(name):
            print(name); raise SystemExit(0)
    raise SystemExit("no matching el%s %s RPM in this release" % (el_major, pkg_arch))
else:
    suffix="_%s.deb" % deb_arch
    for name in names:
        if name.startswith("cpn-installer_") and name.endswith(suffix):
            print(name); raise SystemExit(0)
    raise SystemExit("no matching %s .deb in this release" % deb_arch)
' "$FAMILY" "${EL_MAJOR:-}" "$PKG_ARCH" "$DEB_ARCH"
}

verify_checksum() {
  local artifact="$1" sums="$2"
  local base expected actual
  base="$(basename "$artifact")"
  expected="$(awk -v f="$base" '$2 == f { print $1; exit }' "$sums")"
  [[ -n "$expected" ]] || die "SHA256SUMS has no entry for $base"
  actual="$(sha256_file "$artifact")"
  [[ "$actual" == "$expected" ]] || die "SHA-256 mismatch for $base (expected $expected, got $actual)"
  info "SHA-256 OK: $base"
}

verify_gpg_sums() {
  local sums="$1" asc="$2" keyfile="$3"
  if [[ ! -f "$asc" ]]; then
    if [[ "$CPN_ALLOW_UNSIGNED" == "1" ]]; then
      info "warning: SHA256SUMS.asc missing; continuing because CPN_ALLOW_UNSIGNED=1"
      return
    fi
    die "missing SHA256SUMS.asc (set CPN_ALLOW_UNSIGNED=1 only for local lab builds)"
  fi
  if [[ "$CPN_REQUIRE_GPG" != "1" && "$CPN_ALLOW_UNSIGNED" == "1" ]]; then
    info "skipping GPG (CPN_REQUIRE_GPG=0)"
    return
  fi
  have_cmd gpg || die "gpg is required to verify SHA256SUMS.asc"
  local gnupghome fpr
  gnupghome="$(mktemp -d)"
  chmod 700 "$gnupghome"
  export GNUPGHOME="$gnupghome"
  # shellcheck disable=SC2064
  trap "rm -rf '$gnupghome'" RETURN
  gpg --batch --import "$keyfile" >/dev/null 2>&1 || die "could not import RPM-GPG-KEY-CPN"
  fpr="$(gpg --batch --with-colons --fingerprint | awk -F: '/^fpr:/ { print $10; exit }')"
  fpr="$(printf '%s' "$fpr" | tr -d ' ')"
  [[ "$fpr" == "$CPN_EXPECTED_FPR" ]] || die "GPG fingerprint mismatch (got ${fpr:-empty}, expected $CPN_EXPECTED_FPR)"
  gpg --batch --verify "$asc" "$sums" >/dev/null 2>&1 || die "GPG verification failed for SHA256SUMS.asc"
  info "GPG OK: SHA256SUMS.asc (fingerprint $CPN_EXPECTED_FPR)"
}

upgrade_package() {
  local artifact="$1"
  if cpn_try_retag_package "$artifact" "$FAMILY"; then
    return 0
  fi
  case "$FAMILY" in
    dnf)
      if have_cmd dnf; then
        if dnf upgrade -y "$artifact"; then
          return 0
        fi
        # Explicit -b / CPN_RELEASE_TAG pin may target an older NEVRA than installed.
        if [[ -n "${CPN_RELEASE_TAG:-}${CPN_REF_ARG:-}${CPN_BRANCH:-}" ]]; then
          info "pin may be older than installed; retrying with rpm --oldpackage"
          if have_cmd rpm && rpm -Uvh --oldpackage "$artifact"; then
            return 0
          fi
        fi
        dnf install -y "$artifact"
      elif have_cmd yum; then
        yum upgrade -y "$artifact" || yum install -y "$artifact"
      else
        die "dnf or yum is required"
      fi
      ;;
    apt)
      if have_cmd apt-get; then
        if ! apt-get install -y "$artifact"; then
          if [[ -n "${CPN_RELEASE_TAG:-}${CPN_REF_ARG:-}${CPN_BRANCH:-}" ]]; then
            info "pin may be older than installed; retrying with apt allow-downgrades"
            apt-get install -y --allow-downgrades "$artifact" || die "could not install pinned package $(basename "$artifact")"
            return 0
          fi
          die "could not install package $(basename "$artifact")"
        fi
      else
        die "apt-get is required"
      fi
      ;;
    *) die "internal error: unknown family $FAMILY" ;;
  esac
}

print_next_steps() {
  local ran_panel="${1:-0}"
  local panel_rc="${2:-0}"
  cat <<EOF

CPN package upgrade finished.
EOF
  if [[ "$ran_panel" == "1" ]]; then
    if [[ "$panel_rc" -eq 0 ]]; then
      cat <<'EOF'
Panel maintenance completed: ran `cpn-installer --upgrade` (cleanup + service verify).
EOF
    else
      cat <<'EOF'
Panel maintenance was started but exited with an error.
Retry: sudo cpn-installer --upgrade
Optional Docker refresh (CPN-managed only): sudo cpn-installer --upgrade --bypass
EOF
    fi
  else
    cat <<'EOF'
No completed panel install detected on this host (no install-manifest / panel-bootstrap).
Package binary updated only. Open the installer when ready:
  sudo cpn-installer
EOF
  fi
  cat <<'EOF'

Keep backups before upgrading production-like hosts. CPN is still under active development.
EOF
}

panel_install_detected() {
  local data_dir="${CPN_DATA_DIR:-/var/lib/cpn}"
  [[ -f "${data_dir}/install-manifest.json" || -f "${data_dir}/panel-bootstrap.json" ]]
}

run_panel_maintenance() {
  local bin=""
  if have_cmd cpn-installer; then
    bin="$(command -v cpn-installer)"
  elif [[ -x /usr/bin/cpn-installer ]]; then
    bin="/usr/bin/cpn-installer"
  else
    die "cpn-installer missing after package upgrade"
  fi
  local args=(--upgrade)
  if [[ "${CPN_UPGRADE_BYPASS:-0}" == "1" ]] || [[ "${CPN_UPGRADE_BYPASS_FLAG:-0}" == "1" ]]; then
    args+=(--bypass)
    info "passing --bypass (CPN-managed Docker refresh opt-in)"
  fi
  info "running panel maintenance: ${bin} ${args[*]}"
  # Non-interactive: --upgrade does not require a TTY.
  if "$bin" "${args[@]}"; then
    return 0
  fi
  return 1
}

ensure_panel_service_reachable() {
  if !have_cmd systemctl; then
    return 0
  fi
  local data_dir="${CPN_DATA_DIR:-/var/lib/cpn}"
  local allow=0
  if [[ -f "${data_dir}/allow_remote" ]]; then
    case "$(tr -d '[:space:]' < "${data_dir}/allow_remote" | tr '[:upper:]' '[:lower:]')" in
      1|true|yes|on) allow=1 ;;
    esac
  fi
  if [[ "$allow" -eq 1 ]]; then
    mkdir -p /etc/systemd/system/cpn-installer.service.d
    cat > /etc/systemd/system/cpn-installer.service.d/10-allow-remote.conf <<'DROPIN'
# Generated by CPN upgrade.sh after an allow-remote install.
[Service]
Environment=CPN_ALLOW_REMOTE=1
DROPIN
  fi
  systemctl daemon-reload >/dev/null 2>&1 || true
  systemctl enable cpn-installer.service >/dev/null 2>&1 || true
  systemctl reset-failed cpn-installer.service >/dev/null 2>&1 || true
  systemctl restart cpn-installer.service >/dev/null 2>&1 \
    || systemctl start cpn-installer.service >/dev/null 2>&1 \
    || true
  info "panel service restart attempted (allow_remote=${allow})"
}

# True when the panel unit is active and /login answers successfully.
panel_http_login_ok() {
  if ! have_cmd systemctl || ! have_cmd curl; then
    return 1
  fi
  local port=2087
  local data_dir="${CPN_DATA_DIR:-/var/lib/cpn}"
  if [[ -f "${data_dir}/listen_port" ]]; then
    port="$(tr -d '[:space:]' < "${data_dir}/listen_port" || true)"
  fi
  [[ "$port" =~ ^[0-9]+$ ]] || port=2087
  local i
  for i in 1 2 3 4 5 6 7 8; do
    if systemctl is-active --quiet cpn-installer.service \
      && curl --fail --silent --show-error --max-time 8 \
        --output /dev/null "http://127.0.0.1:${port}/login"; then
      return 0
    fi
    sleep 1
  done
  return 1
}

main() {
  cpn_peek_ref_arg "$@"
  cpn_source_bootstrap_lib
  cpn_parse_bootstrap_args upgrade -- "$@"

  require_root
  have_cmd python3 || die "python3 is required"
  require_existing_install
  detect_guest
  info "detected ${DIST_LABEL} (${FAMILY}, arch ${PKG_ARCH}/${DEB_ARCH})"
  cpn_apply_git_ref_pin

  local work release_json tag pkg_name pkg_url sums_url asc_url key_url
  work="$(mktemp -d /var/tmp/cpn-upgrade.XXXXXX)"
  chmod 700 "$work"
  # shellcheck disable=SC2064
  trap "rm -rf '$work'" EXIT

  release_json="$(pick_release_json)"
  tag="$(printf '%s' "$release_json" | python3 -c 'import json,sys; print(json.load(sys.stdin).get("tag_name",""))')"
  [[ -n "$tag" ]] || die "release JSON missing tag_name"
  info "using release $tag"

  pkg_name="$(select_package_name "$release_json")" \
    || die "no compatible package for this OS in release $tag"
  pkg_url="$(asset_url_by_name "$release_json" "$pkg_name")" \
    || die "missing download URL for $pkg_name"
  sums_url="$(asset_url_by_name "$release_json" "SHA256SUMS")" \
    || die "release $tag is missing SHA256SUMS"
  asc_url="$(asset_url_by_name "$release_json" "SHA256SUMS.asc" 2>/dev/null || true)"

  download "$pkg_url" "$work/$pkg_name"
  download "$sums_url" "$work/SHA256SUMS"
  if [[ -n "${asc_url:-}" ]]; then
    download "$asc_url" "$work/SHA256SUMS.asc"
  fi
  key_url="$(asset_url_by_name "$release_json" "RPM-GPG-KEY-CPN" 2>/dev/null || true)"
  if [[ -n "${key_url:-}" ]]; then
    download "$key_url" "$work/RPM-GPG-KEY-CPN"
  else
    download "$RAW_KEY_URL" "$work/RPM-GPG-KEY-CPN"
  fi

  verify_checksum "$work/$pkg_name" "$work/SHA256SUMS"
  verify_gpg_sums "$work/SHA256SUMS" "$work/SHA256SUMS.asc" "$work/RPM-GPG-KEY-CPN"

  if [[ "$FAMILY" == "dnf" ]] && have_cmd rpm; then
    rpm --import "$work/RPM-GPG-KEY-CPN" >/dev/null 2>&1 || true
    if have_cmd rpmkeys; then
      rpmkeys --import "$work/RPM-GPG-KEY-CPN" >/dev/null 2>&1 || true
    fi
  fi

  info "upgrading with $pkg_name"
  upgrade_package "$work/$pkg_name"

  local ran_panel=0 panel_rc=0
  if panel_install_detected; then
    ran_panel=1
    if run_panel_maintenance; then
      panel_rc=0
    else
      panel_rc=1
    fi
    ensure_panel_service_reachable
    # Older binaries killed CLI --upgrade via pkill -x cpn-installer ("Terminated").
    # If heal left the unit active and /login OK, treat package upgrade as success.
    if [[ "$panel_rc" -ne 0 ]] && panel_http_login_ok; then
      info "panel maintenance exited early; service healed and /login OK"
      panel_rc=0
    fi
  else
    info "panel install markers not found; skipped automatic cpn-installer --upgrade"
  fi

  print_next_steps "$ran_panel" "$panel_rc"
  if [[ "$ran_panel" == "1" && "$panel_rc" -ne 0 ]]; then
    exit 1
  fi
}

main "$@"
