#!/bin/bash

# CDN LVS Agent 安装脚本
# 适用于 Debian 12 系统

set -e

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

# 日志函数
log_info() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

log_warn() {
    echo -e "${YELLOW}[WARN]${NC} $1"
}

log_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# 检查是否为root用户
check_root() {
    if [ "$EUID" -ne 0 ]; then
        log_error "请使用root权限运行此脚本"
        exit 1
    fi
}

# 检查系统版本
check_system() {
    if [ ! -f /etc/debian_version ]; then
        log_error "此脚本仅支持Debian系统"
        exit 1
    fi
    
    version=$(cat /etc/debian_version)
    log_info "检测到Debian版本: $version"
}

# 创建用户和组
create_user() {
    if ! id "cdn-lvs-agent" &>/dev/null; then
        log_info "创建用户 cdn-lvs-agent"
        useradd --system --no-create-home --shell /bin/false cdn-lvs-agent
    else
        log_info "用户 cdn-lvs-agent 已存在"
    fi
}

# 创建目录结构
create_directories() {
    log_info "创建目录结构"
    mkdir -p /opt/cdn-lvs-agent/{bin,configs,logs}
    mkdir -p /var/log/cdn-lvs-agent
}

# 复制文件
copy_files() {
    log_info "复制程序文件"
    
    # 复制二进制文件
    if [ -f "./server" ]; then
        cp ./server /opt/cdn-lvs-agent/bin/
        chmod +x /opt/cdn-lvs-agent/bin/server
    else
        log_error "找不到server二进制文件"
        exit 1
    fi
    
    # 复制配置文件
    if [ -f "./config.yaml" ]; then
        cp ./config.yaml /opt/cdn-lvs-agent/configs/
    else
        log_warn "未找到config.yaml文件，使用默认配置"
        cat > /opt/cdn-lvs-agent/configs/config.yaml << 'EOF'
server:
  http:
    addr: 0.0.0.0:8000
    timeout: 1s
  grpc:
    addr: 0.0.0.0:90
    timeout: 1s
tracing:
  endpoint: 127.0.0.1:35019
EOF
    fi
    
    # 设置文件权限
    chown -R cdn-lvs-agent:cdn-lvs-agent /opt/cdn-lvs-agent
    chown -R cdn-lvs-agent:cdn-lvs-agent /var/log/cdn-lvs-agent
    chmod 755 /opt/cdn-lvs-agent/bin/server
    chmod 644 /opt/cdn-lvs-agent/configs/config.yaml
}

# 安装systemd服务
install_service() {
    log_info "安装systemd服务"
    
    if [ -f "./cdn-lvs-agent.service" ]; then
        cp ./cdn-lvs-agent.service /etc/systemd/system/
        systemctl daemon-reload
        systemctl enable cdn-lvs-agent.service
    else
        log_error "找不到cdn-lvs-agent.service文件"
        exit 1
    fi
}

# 启动服务
start_service() {
    log_info "启动cdn-lvs-agent服务"
    systemctl start cdn-lvs-agent.service
    
    # 检查服务状态
    sleep 2
    if systemctl is-active --quiet cdn-lvs-agent.service; then
        log_info "服务启动成功"
        systemctl status cdn-lvs-agent.service --no-pager -l
    else
        log_error "服务启动失败"
        systemctl status cdn-lvs-agent.service --no-pager -l
        exit 1
    fi
}

# 显示服务信息
show_info() {
    log_info "安装完成！"
    echo ""
    echo "服务管理命令："
    echo "  启动服务: systemctl start cdn-lvs-agent"
    echo "  停止服务: systemctl stop cdn-lvs-agent"
    echo "  重启服务: systemctl restart cdn-lvs-agent"
    echo "  查看状态: systemctl status cdn-lvs-agent"
    echo "  查看日志: journalctl -u cdn-lvs-agent -f"
    echo ""
    echo "配置文件位置: /opt/cdn-lvs-agent/configs/config.yaml"
    echo "日志文件位置: /var/log/cdn-lvs-agent/"
    echo ""
}

# 卸载函数
uninstall() {
    log_info "开始卸载cdn-lvs-agent服务"
    
    # 停止服务
    if systemctl is-active --quiet cdn-lvs-agent.service; then
        systemctl stop cdn-lvs-agent.service
    fi
    
    # 禁用服务
    if systemctl is-enabled --quiet cdn-lvs-agent.service; then
        systemctl disable cdn-lvs-agent.service
    fi
    
    # 删除服务文件
    if [ -f /etc/systemd/system/cdn-lvs-agent.service ]; then
        rm /etc/systemd/system/cdn-lvs-agent.service
        systemctl daemon-reload
    fi
    
    # 删除用户
    if id "cdn-lvs-agent" &>/dev/null; then
        userdel cdn-lvs-agent
    fi
    
    # 删除文件
    rm -rf /opt/cdn-lvs-agent
    rm -rf /var/log/cdn-lvs-agent
    
    log_info "卸载完成"
}

# 主函数
main() {
    case "${1:-install}" in
        "install")
            check_root
            check_system
            create_user
            create_directories
            copy_files
            install_service
            start_service
            show_info
            ;;
        "uninstall")
            check_root
            uninstall
            ;;
        *)
            echo "用法: $0 [install|uninstall]"
            echo "  install   - 安装服务 (默认)"
            echo "  uninstall - 卸载服务"
            exit 1
            ;;
    esac
}

main "$@" 