#!/usr/bin/env bash
# QKpanel 一键卸载脚本
# 用法:
#   curl -fsSL https://download.qkpanel.com/uninstall.sh | bash -s -- --yes
#   curl -fsSL https://download.qkpanel.com/uninstall.sh | QKPANEL_YES=1 bash
# 选项:
#   --yes, -y           跳过确认（非交互模式必需）
#   --keep-data         保留 data/ 数据目录
# 环境变量:
#   QKPANEL_YES=1       同 --yes
#   QKPANEL_KEEP_DATA=1 同 --keep-data
#   QKPANEL_INSTALL_PATH  安装目录，默认 /opt/qkpanel

set -euo pipefail

PANEL_ROOT="${QKPANEL_INSTALL_PATH:-/opt/qkpanel}"
SERVICE_NAME="qkpanel"
SYSTEMD_UNIT="/etc/systemd/system/${SERVICE_NAME}.service"
CRON_FILE="/etc/cron.d/qkpanel"

KEEP_DATA="${QKPANEL_KEEP_DATA:-0}"
AUTO_YES="${QKPANEL_YES:-0}"

log_info() { echo "[INFO] $*"; }
log_ok() { echo "[OK] $*"; }
log_warn() { echo "[WARN] $*" >&2; }
log_error() { echo "[ERROR] $*" >&2; }

usage() {
  cat <<EOF
QKpanel 卸载脚本

用法:
  bash uninstall.sh [选项]

选项:
  --yes, -y      确认卸载（curl 管道执行时必须提供）
  --keep-data    保留 ${PANEL_ROOT}/data/ 目录
  -h, --help     显示帮助

示例:
  curl -fsSL https://download.qkpanel.com/uninstall.sh | bash -s -- --yes
  curl -fsSL https://download.qkpanel.com/uninstall.sh | bash -s -- --yes --keep-data
EOF
}

while [[ $# -gt 0 ]]; do
  case "$1" in
    --yes|-y) AUTO_YES=1; shift ;;
    --keep-data) KEEP_DATA=1; shift ;;
    -h|--help) usage; exit 0 ;;
    *) log_error "未知参数: $1"; usage; exit 1 ;;
  esac
done

if [[ "${EUID:-$(id -u)}" -ne 0 ]]; then
  log_error "请使用 root 用户运行卸载脚本"
  exit 1
fi

if [[ ! -d "$PANEL_ROOT" ]] && [[ ! -f "$SYSTEMD_UNIT" ]] && ! systemctl list-unit-files "${SERVICE_NAME}.service" --no-legend 2>/dev/null | grep -q "${SERVICE_NAME}.service"; then
  log_warn "未检测到 QKpanel 安装，无需卸载"
  exit 0
fi

if [[ "$AUTO_YES" != "1" ]]; then
  if [[ ! -t 0 ]]; then
    log_error "非交互模式卸载需要显式确认"
    log_error "请使用: curl -fsSL https://download.qkpanel.com/uninstall.sh | bash -s -- --yes"
    exit 1
  fi

  echo "即将卸载 QKpanel"
  echo "  安装目录: ${PANEL_ROOT}"
  if [[ "$KEEP_DATA" == "1" ]]; then
    echo "  数据目录: 保留"
  else
    echo "  数据目录: 删除"
  fi
  read -rp "确认继续？[y/N] " confirm
  if [[ ! "$confirm" =~ ^[Yy]$ ]]; then
    echo "已取消卸载"
    exit 0
  fi
fi

if systemctl list-unit-files "${SERVICE_NAME}.service" --no-legend 2>/dev/null | grep -q "${SERVICE_NAME}.service"; then
  if systemctl is-active --quiet "$SERVICE_NAME" 2>/dev/null; then
    log_info "停止 ${SERVICE_NAME} 服务..."
    systemctl stop "$SERVICE_NAME"
  fi

  if systemctl is-enabled --quiet "$SERVICE_NAME" 2>/dev/null; then
    log_info "禁用 ${SERVICE_NAME} 服务..."
    systemctl disable "$SERVICE_NAME" >/dev/null 2>&1 || true
  fi
fi

if [[ -f "$SYSTEMD_UNIT" ]]; then
  log_info "移除 systemd 单元..."
  rm -f "$SYSTEMD_UNIT"
  systemctl daemon-reload
fi

if [[ -f "$CRON_FILE" ]]; then
  log_info "清理面板定时任务..."
  rm -f "$CRON_FILE"
fi

if [[ -d "$PANEL_ROOT" ]]; then
  if [[ "$KEEP_DATA" == "1" ]]; then
    log_info "删除程序文件（保留 data/）..."
    find "$PANEL_ROOT" -mindepth 1 -maxdepth 1 ! -name 'data' -exec rm -rf {} +
  else
    log_info "删除安装目录 ${PANEL_ROOT}..."
    rm -rf "$PANEL_ROOT"
  fi
fi

log_ok "QKpanel 卸载完成"
