#!/bin/bash

# QZMusic-Web 卸载脚本 - 完全停止并删除所有 QZMusic-Web 部署

set -e

echo "=========================================="
echo "   QZMusic-Web 完全卸载"
echo "=========================================="
echo ""

# ========= 配置 =========
INSTALL_DIR="/opt/QZMusic-Web"
PORT=1219
SERVICE_NAME="qzmusic-web"

GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'

log_info()  { echo -e "${GREEN}[INFO]${NC} $1"; }
log_warn()  { echo -e "${YELLOW}[WARN]${NC} $1"; }
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }

SUDO=""
if [ "$EUID" -ne 0 ]; then
    SUDO="sudo"
fi

# ========= 确认 =========
echo "⚠️  即将卸载 QZMusic-Web"
echo ""
echo "将执行以下操作:"
echo "  - 停止并禁用 systemd 服务 $SERVICE_NAME"
echo "  - 删除服务文件 /etc/systemd/system/$SERVICE_NAME.service"
echo "  - 杀掉占用 $PORT 端口的进程"
echo "  - 删除目录 $INSTALL_DIR"
echo "  - 关闭防火墙 $PORT/tcp 放行"
echo ""
read -p "确认删除？输入 yes 继续: " confirm
if [ "$confirm" != "yes" ]; then
    log_info "取消卸载"
    exit 0
fi
echo ""

# ========= 停止服务 =========
log_info "停止 systemd 服务..."
if systemctl list-unit-files 2>/dev/null | grep -q "$SERVICE_NAME.service"; then
    $SUDO systemctl stop    "$SERVICE_NAME" 2>/dev/null || true
    $SUDO systemctl disable "$SERVICE_NAME" 2>/dev/null || true
    log_info "服务已停止 & 禁用"
else
    log_warn "未检测到 systemd 服务 $SERVICE_NAME，跳过"
fi

# ========= 删除服务文件 =========
SERVICE_FILE="/etc/systemd/system/$SERVICE_NAME.service"
if [ -f "$SERVICE_FILE" ]; then
    $SUDO rm -f "$SERVICE_FILE"
    $SUDO systemctl daemon-reload
    log_info "已删除服务文件: $SERVICE_FILE"
fi

# ========= 杀端口 =========
if command -v lsof &> /dev/null; then
    OLD_PID=$(lsof -ti:$PORT 2>/dev/null || true)
    if [ -n "$OLD_PID" ]; then
        log_info "杀掉占用 $PORT 的进程: $OLD_PID"
        $SUDO kill $OLD_PID 2>/dev/null || true
    fi
fi

# ========= 删除目录 =========
if [ -d "$INSTALL_DIR" ]; then
    log_info "删除目录: $INSTALL_DIR"
    $SUDO rm -rf "$INSTALL_DIR"
else
    log_warn "目录不存在: $INSTALL_DIR"
fi

# ========= 关闭防火墙端口 =========
log_info "关闭防火墙 $PORT/tcp 放行..."
if command -v firewall-cmd &> /dev/null && systemctl is-active --quiet firewalld 2>/dev/null; then
    $SUDO firewall-cmd --permanent --remove-port=${PORT}/tcp >/dev/null 2>&1 || true
    $SUDO firewall-cmd --reload >/dev/null
    log_info "firewalld: 已关闭 $PORT/tcp"
elif command -v ufw &> /dev/null && systemctl is-active --quiet ufw 2>/dev/null; then
    $SUDO ufw delete allow ${PORT}/tcp >/dev/null 2>&1 || true
    log_info "ufw: 已关闭 $PORT/tcp"
else
    log_warn "未检测到活动的 firewalld/ufw，跳过防火墙配置。"
fi

# ========= 清理缓存 =========
log_info "清理 npm 缓存..."
if [ -d "$HOME/.npm/_cacache" ]; then
    npm cache clean --force >/dev/null 2>&1 || true
fi
$SUDO rm -rf /tmp/QZMusic* /var/tmp/QZMusic* 2>/dev/null || true

echo ""
echo "=========================================="
echo "   ✅ 卸载完成！"
echo "=========================================="
echo ""
echo "已清理:"
echo "  - 目录: $INSTALL_DIR"
echo "  - systemd 服务: $SERVICE_NAME"
echo "  - 端口: $PORT"
echo "  - 相关缓存"
echo ""
echo "如需重新安装，请运行:"
echo "  bash <(curl -sL http://171.80.3.149:4321/miao-moe/QZMusic-Web/raw/branch/master/install.sh)"
echo ""
