#!/bin/bash

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# 输出彩色信息
info() { echo -e "${GREEN}[INFO]${NC} $1"; }
warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
error() { echo -e "${RED}[ERROR]${NC} $1"; }

# 停止服务
stop_services() {
    info "停止服务..."
    
    if systemctl is-active --quiet lcf-frps 2>/dev/null; then
        systemctl stop lcf-frps
        info "已停止 lcf-frps 服务"
    else
        warn "lcf-frps 服务未运行"
    fi
    
    if systemctl is-active --quiet lcf-controller 2>/dev/null; then
        systemctl stop lcf-controller
        info "已停止 lcf-controller 服务"
    else
        warn "lcf-controller 服务未运行"
    fi
}

# 禁用服务
disable_services() {
    info "禁用服务..."
    
    if systemctl is-enabled --quiet lcf-frps 2>/dev/null; then
        systemctl disable lcf-frps
        info "已禁用 lcf-frps 服务"
    else
        warn "lcf-frps 服务未启用"
    fi
    
    if systemctl is-enabled --quiet lcf-controller 2>/dev/null; then
        systemctl disable lcf-controller
        info "已禁用 lcf-controller 服务"
    else
        warn "lcf-controller 服务未启用"
    fi
}

# 删除服务文件
remove_service_files() {
    info "删除服务文件..."
    
    local service_files=(
        "/etc/systemd/system/lcf-frps.service"
        "/etc/systemd/system/lcf-controller.service"
    )
    
    for file in "${service_files[@]}"; do
        if [ -f "$file" ]; then
            rm -f "$file"
            info "已删除服务文件: $file"
        else
            warn "服务文件不存在: $file"
        fi
    done
    
    # 重新加载 systemd
    systemctl daemon-reload
    systemctl reset-failed
    info "已重新加载 systemd"
}

# 删除安装文件
remove_installation_files() {
    info "删除安装文件..."
    
    local install_dirs=(
        "/opt/lcf"
        "/tmp/frps_dashboard_pwd"
        "/tmp/frps_node_id"
    )
    
    for dir in "${install_dirs[@]}"; do
        if [ -e "$dir" ]; then
            rm -rf "$dir"
            info "已删除: $dir"
        else
            warn "文件/目录不存在: $dir"
        fi
    done
}

# 检查残留进程
check_remaining_processes() {
    info "检查残留进程..."
    
    local processes=(
        "frps"
        "controller"
    )
    
    for proc in "${processes[@]}"; do
        if pgrep -f "$proc" >/dev/null 2>&1; then
            warn "发现残留进程: $proc"
            pkill -f "$proc"
            sleep 2
            if pgrep -f "$proc" >/dev/null 2>&1; then
                warn "强制杀死进程: $proc"
                pkill -9 -f "$proc"
            fi
        fi
    done
}

# 显示卸载摘要
show_uninstall_summary() {
    info "=== 卸载完成摘要 ==="
    info "已删除的服务:"
    echo "  - lcf-frps.service"
    echo "  - lcf-controller.service"
    info "已删除的目录:"
    echo "  - /opt/lcf/"
    info "已清理的临时文件:"
    echo "  - /tmp/frps_dashboard_pwd"
    echo "  - /tmp/frps_node_id"
    info "建议手动检查:"
    echo "  - 使用 'ps aux | grep frp' 检查是否有残留进程"
    echo "  - 使用 'netstat -tlnp | grep 2333' 检查端口是否释放"
    echo "  - 使用 'netstat -tlnp | grep 8233' 检查面板端口是否释放"
}

# 确认卸载
confirm_uninstall() {
    warn "=== 警告 ==="
    warn "此操作将完全卸载 LoCyanFrp 服务，包括:"
    warn "  - 停止并删除 FRPS 和控制器服务"
    warn "  - 删除所有配置文件和数据"
    warn "  - 删除 /opt/lcf/ 目录"
    echo
    read -p "确定要继续卸载吗? (y/N): " confirm
    
    case $confirm in
        [yY]|[yY][eE][sS])
            info "开始卸载..."
            ;;
        *)
            info "取消卸载"
            exit 0
            ;;
    esac
}

# 主卸载函数
main() {
    # 检查 root 权限
    if [ "$EUID" -ne 0 ]; then
        error "请使用 root 权限运行此脚本"
        exit 1
    fi
    
    confirm_uninstall
    
    stop_services
    disable_services
    remove_service_files
    check_remaining_processes
    remove_installation_files
    show_uninstall_summary
    
    info "LoCyanFrp 卸载完成!"
}

# 执行主函数
main "$@"
