#!/bin/bash
# ============================================================================
# PPPoE拨号系统一键安装脚本
# 版本: 1.0
# 作者: Auto Installer
# ============================================================================

set -e  # 遇到错误立即停止

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

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

log_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

log_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

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

# 分隔线
print_separator() {
    echo "================================================================"
}

# 显示标题
print_title() {
    clear
    print_separator
    echo -e "${GREEN}   PPPoE多账号拨号系统一键安装脚本"
    echo -e "${GREEN}   版本: 1.0"
    print_separator
    echo ""
}

# 检查是否为root用户
check_root() {
    if [ "$(id -u)" != "0" ]; then
        log_error "请使用root用户运行此脚本"
        echo "请执行: sudo su"
        exit 1
    fi
    log_success "root权限检查通过"
}

# 检测系统版本
check_os() {
    if [ -f /etc/redhat-release ]; then
        OS_VERSION=$(cat /etc/redhat-release)
        log_info "检测到系统: $OS_VERSION"
    elif [ -f /etc/lsb-release ]; then
        OS_VERSION=$(cat /etc/lsb-release | grep DESCRIPTION | cut -d '"' -f 2)
        log_info "检测到系统: $OS_VERSION"
    else
        log_warning "无法识别的系统版本"
        read -p "是否继续安装? (y/n): " -n 1 -r
        echo
        if [[ ! $REPLY =~ ^[Yy]$ ]]; then
            exit 1
        fi
    fi
}

# 获取本机IP
get_local_ip() {
    LOCAL_IP=$(ip addr show | grep -E 'inet (192|10|172)' | grep -v '127.0.0.1' | head -1 | awk '{print $2}' | cut -d '/' -f1)
    if [ -z "$LOCAL_IP" ]; then
        LOCAL_IP=$(hostname -I | awk '{print $1}')
    fi
    echo "$LOCAL_IP"
}

# 检查网络连通性
check_network() {
    log_info "检查网络连通性..."
    if ping -c 2 -W 2 www.baidu.com > /dev/null 2>&1; then
        log_success "网络连通性正常"
        return 0
    else
        log_warning "网络连接可能存在问题，请检查网络配置"
        return 1
    fi
}

# 显示菜单
show_menu() {
    print_title
    echo "请选择安装模式:"
    echo "1. 快速安装 (使用默认配置)"
    echo "2. 自定义安装 (手动配置账号信息)"
    echo "3. 仅安装依赖 (不配置账号)"
    echo "4. 退出安装"
    echo ""
    read -p "请输入选择 (1-4): " choice
}

# 获取网卡信息
get_network_interfaces() {
    echo "检测到的网络接口:"
    echo "--------------------------------"
    ip -o link show | awk -F': ' '{print $2}' | grep -v lo | cat -n
    echo "--------------------------------"
    
    # 获取默认网卡
    DEFAULT_NIC=$(ip route | grep default | awk '{print $5}' | head -1)
    if [ -z "$DEFAULT_NIC" ]; then
        DEFAULT_NIC="eth0"
    fi
    
    read -p "请输入拨号网卡名称 [默认: $DEFAULT_NIC]: " NETCARD
    NETCARD=${NETCARD:-$DEFAULT_NIC}
    
    # 验证网卡是否存在
    if ! ip link show "$NETCARD" > /dev/null 2>&1; then
        log_error "网卡 $NETCARD 不存在，请重新输入"
        get_network_interfaces
    fi
    
    log_success "选择网卡: $NETCARD"
}

# 获取宽带账号信息
get_pppoe_accounts() {
    echo ""
    echo "配置PPPoE拨号账号"
    echo "--------------------------------"
    
    ACCOUNTS=()
    ACCOUNT_COUNT=0
    
    while true; do
        ACCOUNT_COUNT=$((ACCOUNT_COUNT + 1))
        echo ""
        echo "账号 #$ACCOUNT_COUNT 配置:"
        
        # 账号
        while true; do
            read -p "  宽带账号 (如: 1234567890@163.gd): " ACCOUNT
            if [ -n "$ACCOUNT" ]; then
                break
            else
                log_warning "账号不能为空"
            fi
        done
        
        # 密码
        while true; do
            read -p "  宽带密码: " PASSWORD
            if [ -n "$PASSWORD" ]; then
                break
            else
                log_warning "密码不能为空"
            fi
        done
        
        # VLAN ID
        read -p "  VLAN ID (0表示无VLAN，默认100): " VLAN
        VLAN=${VLAN:-100}
        
        # MAC地址
        read -p "  MAC地址 (留空自动生成): " MAC
        if [ -z "$MAC" ]; then
            # 生成随机MAC
            MAC=$(printf '00:%02x:%02x:%02x:%02x:%02x' \
                $((RANDOM%256)) $((RANDOM%256)) $((RANDOM%256)) \
                $((RANDOM%256)) $((RANDOM%256)))
        fi
        
        # 添加到数组
        ACCOUNTS+=("$ACCOUNT:$PASSWORD:$VLAN:$MAC")
        
        echo ""
        read -p "是否添加更多账号? (y/n): " -n 1 -r
        echo
        if [[ ! $REPLY =~ ^[Yy]$ ]]; then
            break
        fi
    done
    
    log_success "共配置 ${#ACCOUNTS[@]} 个宽带账号"
}

# 创建配置文件
create_pppoe_config() {
    log_info "创建配置文件..."
    
    CONFIG_FILE="/etc/borui/config/pppoe.json"
    
    # 开始创建JSON数组
    echo "[" > "$CONFIG_FILE"
    
    for i in "${!ACCOUNTS[@]}"; do
        IFS=':' read -r ACCOUNT PASSWORD VLAN MAC <<< "${ACCOUNTS[$i]}"
        
        # 生成ID (1000, 1001, 1002...)
        ID=$((1000 + i))
        
        # 写入账号配置
        cat >> "$CONFIG_FILE" << EOF
    {
        "id": "$ID",
        "aid": "account_$ID",
        "type": "pppoe",
        "account": "$ACCOUNT",
        "password": "$PASSWORD",
        "vlan": "$VLAN",
        "bandwidth": "100M",
        "mac": "$MAC",
        "main_nic": "$NETCARD"
    }
EOF
        
        # 如果不是最后一个元素，添加逗号
        if [ $i -lt $((${#ACCOUNTS[@]} - 1)) ]; then
            echo "    ," >> "$CONFIG_FILE"
        fi
    done
    
    echo "]" >> "$CONFIG_FILE"
    
    log_success "配置文件已创建: $CONFIG_FILE"
    
    # 显示配置文件内容
    echo ""
    log_info "配置文件内容预览:"
    echo "--------------------------------"
    cat "$CONFIG_FILE"
    echo "--------------------------------"
}

# 快速安装模式
quick_install() {
    log_info "开始快速安装模式..."
    
    # 使用默认网卡
    DEFAULT_NIC=$(ip route | grep default | awk '{print $5}' | head -1)
    if [ -z "$DEFAULT_NIC" ]; then
        DEFAULT_NIC="eth0"
    fi
    NETCARD=$DEFAULT_NIC
    log_info "使用默认网卡: $NETCARD"
    
    # 创建示例配置文件
    log_info "创建示例配置文件..."
    CONFIG_FILE="/etc/borui/config/pppoe.json"
    
    mkdir -p /etc/borui/config
    cat > "$CONFIG_FILE" << 'EOF'
[
    {
        "id": "1000",
        "aid": "example_account_1",
        "type": "pppoe",
        "account": "your_username_1@example.com",
        "password": "your_password_1",
        "vlan": "100",
        "bandwidth": "100M",
        "mac": "00:11:22:33:44:55",
        "main_nic": "eth0"
    },
    {
        "id": "1001",
        "aid": "example_account_2",
        "type": "pppoe",
        "account": "your_username_2@example.com",
        "password": "your_password_2",
        "vlan": "101",
        "bandwidth": "100M",
        "mac": "00:11:22:33:44:56",
        "main_nic": "eth0"
    }
]
EOF
    
    # 替换网卡名称
    sed -i "s/eth0/$NETCARD/g" "$CONFIG_FILE"
    
    log_warning "请安装完成后编辑配置文件: $CONFIG_FILE"
    log_warning "将示例账号密码替换为你的真实宽带账号密码"
}

# 安装系统依赖
install_dependencies() {
    log_info "开始安装系统依赖..."
    
    print_separator
    
    # 更新系统
    log_info "更新系统包列表..."
    yum makecache fast > /dev/null 2>&1 || yum makecache > /dev/null 2>&1
    
    # 安装基础工具
    log_info "安装基础工具..."
    yum install -y wget curl vim net-tools > /dev/null 2>&1
    
    # 安装PPPoE相关依赖
    log_info "安装PPPoE拨号依赖..."
    yum install -y ppp rp-pppoe > /dev/null 2>&1
    
    # 检查并安装缺失的包
    for package in yum-utils python3-pip epel-release; do
        if ! rpm -qa | grep -q "^${package%-*}"; then
            log_info "安装 $package..."
            yum install -y "$package" > /dev/null 2>&1
        fi
    done
    
    # 启用EPEL仓库
    if ! yum repolist | grep -q epel; then
        log_info "启用EPEL仓库..."
        yum install -y epel-release > /dev/null 2>&1
    fi
    
    # 安装Python依赖
    log_info "安装Python依赖..."
    pip3 install fastapi uvicorn requests pystun prettytable > /dev/null 2>&1
    
    # 检查安装结果
    log_info "检查依赖安装情况..."
    if command -v pppd > /dev/null 2>&1; then
        log_success "pppd 安装成功"
    else
        log_error "pppd 安装失败"
        exit 1
    fi
    
    if pip3 list | grep -q fastapi; then
        log_success "Python依赖安装成功"
    else
        log_error "Python依赖安装失败"
        exit 1
    fi
    
    log_success "所有依赖安装完成"
}

# 创建目录结构
create_directories() {
    log_info "创建目录结构..."
    
    # 配置目录
    mkdir -p /etc/borui/config
    mkdir -p /etc/borui/backup
    
    # 日志目录
    mkdir -p /var/log/dialmger
    mkdir -p /var/log/pppoe
    mkdir -p /var/log/dialmger_server
    
    # 临时目录
    mkdir -p /tmp/pppoe_install
    
    log_success "目录创建完成"
}

# 安装脚本文件
install_scripts() {
    log_info "安装脚本文件..."
    
    # 获取当前脚本所在目录
    SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
    
    # 检查文件是否存在
    if [ ! -f "$SCRIPT_DIR/dialmtor.py" ]; then
        log_error "找不到 dialmtor.py 文件"
        log_info "尝试从网络下载..."
        
        # 从你的服务器下载
        if curl -k -o /tmp/dialmtor.py https://download.lizhiyun.top/pppoe/dialmtor.py 2>/dev/null; then
            log_success "下载 dialmtor.py 成功"
            DIALMTOR_SOURCE="/tmp/dialmtor.py"
        else
            log_error "无法下载 dialmtor.py"
            exit 1
        fi
    else
        DIALMTOR_SOURCE="$SCRIPT_DIR/dialmtor.py"
    fi
    
    if [ ! -f "$SCRIPT_DIR/dialmger.py" ]; then
        log_error "找不到 dialmger.py 文件"
        log_info "尝试从网络下载..."
        
        if curl -k -o /tmp/dialmger.py https://download.lizhiyun.top/pppoe/dialmger.py 2>/dev/null; then
            log_success "下载 dialmger.py 成功"
            DIALMGER_SOURCE="/tmp/dialmger.py"
        else
            log_error "无法下载 dialmger.py"
            exit 1
        fi
    else
        DIALMGER_SOURCE="$SCRIPT_DIR/dialmger.py"
    fi
    
    # 安装服务端脚本
    log_info "安装服务端脚本..."
    cp "$DIALMTOR_SOURCE" /usr/local/bin/dialmtor
    chmod +x /usr/local/bin/dialmtor
    
    # 安装客户端脚本
    log_info "安装客户端脚本..."
    cp "$DIALMGER_SOURCE" /usr/local/bin/dialmger
    chmod +x /usr/local/bin/dialmger
    
    # 创建管理脚本
    log_info "创建管理脚本..."
    cat > /usr/local/bin/pppoe-manager << 'EOF'
#!/bin/bash
# PPPoE拨号管理系统菜单

while true; do
    clear
    echo "========================================"
    echo "      PPPoE拨号管理系统菜单"
    echo "========================================"
    echo ""
    echo "1. 查看拨号状态"
    echo "2. 重启所有拨号"
    echo "3. 重试失败拨号"
    echo "4. 停止所有拨号"
    echo "5. 检查NAT类型"
    echo "6. 编辑配置文件"
    echo "7. 查看服务日志"
    echo "8. 查看拨号日志"
    echo "9. 重启拨号服务"
    echo "10. 停止拨号服务"
    echo "11. 备份配置文件"
    echo "12. 恢复配置文件"
    echo "13. 退出"
    echo ""
    echo "========================================"
    read -p "请输入选项 (1-13): " choice
    
    case $choice in
        1)
            echo ""
            echo "拨号状态:"
            echo "--------------------------------"
            dialmger --list --more
            echo ""
            read -p "按回车键继续..."
            ;;
        2)
            echo ""
            echo "正在重启所有拨号..."
            dialmger --restart
            echo "重启完成，等待60秒后查看状态..."
            sleep 60
            dialmger --list
            read -p "按回车键继续..."
            ;;
        3)
            echo ""
            echo "正在重试失败拨号..."
            dialmger --retry
            read -p "按回车键继续..."
            ;;
        4)
            echo ""
            echo "正在停止所有拨号..."
            dialmger --stop
            read -p "按回车键继续..."
            ;;
        5)
            echo ""
            echo "正在检查NAT类型..."
            dialmger --checknat
            read -p "按回车键继续..."
            ;;
        6)
            echo ""
            echo "编辑配置文件..."
            vim /etc/borui/config/pppoe.json
            echo "配置文件已修改，需要重启服务生效"
            read -p "是否立即重启服务? (y/n): " -n 1 -r
            echo
            if [[ $REPLY =~ ^[Yy]$ ]]; then
                systemctl restart dialmtor
                echo "服务已重启"
            fi
            read -p "按回车键继续..."
            ;;
        7)
            echo ""
            echo "最近的服务日志:"
            echo "--------------------------------"
            journalctl -u dialmtor -n 50 --no-pager
            echo ""
            read -p "按回车键继续..."
            ;;
        8)
            echo ""
            echo "拨号日志列表:"
            echo "--------------------------------"
            ls -la /var/log/pppoe/
            echo ""
            read -p "输入要查看的日志文件名 (或按回车返回): " logfile
            if [ -n "$logfile" ]; then
                if [ -f "/var/log/pppoe/$logfile" ]; then
                    echo ""
                    echo "日志内容:"
                    echo "--------------------------------"
                    tail -50 "/var/log/pppoe/$logfile"
                else
                    echo "文件不存在"
                fi
            fi
            read -p "按回车键继续..."
            ;;
        9)
            echo ""
            echo "正在重启拨号服务..."
            systemctl restart dialmtor
            sleep 2
            systemctl status dialmtor --no-pager
            read -p "按回车键继续..."
            ;;
        10)
            echo ""
            echo "正在停止拨号服务..."
            systemctl stop dialmtor
            systemctl status dialmtor --no-pager
            read -p "按回车键继续..."
            ;;
        11)
            echo ""
            echo "备份配置文件..."
            BACKUP_FILE="/etc/borui/backup/pppoe_$(date +%Y%m%d_%H%M%S).json"
            cp /etc/borui/config/pppoe.json "$BACKUP_FILE"
            echo "配置文件已备份到: $BACKUP_FILE"
            read -p "按回车键继续..."
            ;;
        12)
            echo ""
            echo "恢复配置文件..."
            echo "可用备份文件:"
            echo "--------------------------------"
            ls -la /etc/borui/backup/ | grep pppoe
            echo ""
            read -p "输入要恢复的备份文件名: " backupfile
            if [ -f "/etc/borui/backup/$backupfile" ]; then
                cp "/etc/borui/backup/$backupfile" /etc/borui/config/pppoe.json
                echo "配置文件已恢复，需要重启服务"
                read -p "是否立即重启服务? (y/n): " -n 1 -r
                echo
                if [[ $REPLY =~ ^[Yy]$ ]]; then
                    systemctl restart dialmtor
                    echo "服务已重启"
                fi
            else
                echo "备份文件不存在"
            fi
            read -p "按回车键继续..."
            ;;
        13)
            echo ""
            echo "退出管理菜单"
            exit 0
            ;;
        *)
            echo ""
            echo "无效选项"
            sleep 1
            ;;
    esac
done
EOF
    
    chmod +x /usr/local/bin/pppoe-manager
    
    log_success "脚本安装完成"
}

# 配置systemd服务
configure_systemd() {
    log_info "配置systemd服务..."
    
    # 获取当前脚本所在目录
    SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
    
    # 检查服务文件是否存在
    if [ -f "$SCRIPT_DIR/dialmtor.service" ]; then
        SERVICE_SOURCE="$SCRIPT_DIR/dialmtor.service"
    else
        # 尝试从网络下载
        log_info "从网络下载service文件..."
        if curl -k -o /tmp/dialmtor.service https://download.lizhiyun.top/pppoe/dialmtor.service 2>/dev/null; then
            SERVICE_SOURCE="/tmp/dialmtor.service"
            log_success "下载service文件成功"
        else
            # 创建默认的service文件
            log_info "创建默认service文件..."
            SERVICE_SOURCE="/tmp/dialmtor.service"
            cat > "$SERVICE_SOURCE" << EOF
[Unit]
Description=Dialmtor Service Pro
After=network.target

[Service]
User=root
WorkingDirectory=/usr/local/bin
ExecStart=/usr/local/bin/dialmtor --nic $NETCARD
Restart=always
RestartSec=10
Environment="PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

[Install]
WantedBy=multi-user.target
EOF
        fi
    fi
    
    # 复制service文件
    cp "$SERVICE_SOURCE" /etc/systemd/system/dialmtor.service
    
    # 重新加载systemd
    systemctl daemon-reload
    
    # 启用服务
    systemctl enable dialmtor
    
    log_success "systemd服务配置完成"
}

# 启动服务
start_service() {
    log_info "启动拨号服务..."
    
    # 启动服务
    systemctl start dialmtor
    
    # 检查服务状态
    sleep 3
    if systemctl is-active --quiet dialmtor; then
        log_success "服务启动成功"
    else
        log_error "服务启动失败"
        journalctl -u dialmtor --no-pager | tail -20
        exit 1
    fi
    
    # 显示服务状态
    echo ""
    log_info "服务状态:"
    systemctl status dialmtor --no-pager | head -20
}

# 等待拨号完成并验证
wait_and_verify() {
    log_info "等待拨号完成..."
    
    echo ""
    echo -n "正在拨号"
    for i in {1..30}; do
        echo -n "."
        sleep 2
    done
    echo ""
    
    log_info "检查拨号状态..."
    
    # 使用客户端检查状态
    if command -v dialmger > /dev/null 2>&1; then
        echo ""
        echo "拨号状态:"
        echo "--------------------------------"
        dialmger --list
        
        # 检查是否有成功的拨号
        if dialmger --list --json | grep -q '"code": "0"'; then
            log_success "至少有一个拨号成功"
            
            # 显示获取的IP地址
            echo ""
            log_info "获取的IP地址:"
            ip addr show | grep -E 'ppp[0-9]+' | grep 'inet ' | awk '{print "接口: "$2" IP: "$4}'
        else
            log_warning "暂时没有成功的拨号，可能需要更长时间"
        fi
    else
        log_warning "无法使用dialmger命令，请手动检查"
    fi
    
    # 显示网络接口
    echo ""
    log_info "网络接口状态:"
    echo "--------------------------------"
    ip addr show | grep -E '(ppp|macvlan|eth)' | grep -v '@' | head -20
}

# 显示安装摘要
show_summary() {
    LOCAL_IP=$(get_local_ip)
    
    print_separator
    echo -e "${GREEN}           安装完成摘要"
    print_separator
    echo ""
    echo "服务信息:"
    echo "--------------------------------"
    echo "服务名称: dialmtor"
    echo "配置文件: /etc/borui/config/pppoe.json"
    echo "日志目录: /var/log/pppoe/"
    echo "API地址: http://127.0.0.2:6999"
    echo ""
    echo "管理命令:"
    echo "--------------------------------"
    echo "启动服务: systemctl start dialmtor"
    echo "停止服务: systemctl stop dialmtor"
    echo "重启服务: systemctl restart dialmtor"
    echo "查看状态: systemctl status dialmtor"
    echo "查看日志: journalctl -u dialmtor -f"
    echo ""
    echo "客户端命令:"
    echo "--------------------------------"
    echo "查看拨号: dialmger --list"
    echo "详细查看: dialmger --list --more"
    echo "重启拨号: dialmger --restart"
    echo "重试失败: dialmger --retry"
    echo "检查NAT: dialmger --checknat"
    echo "管理菜单: pppoe-manager"
    echo ""
    echo "网络接口:"
    echo "--------------------------------"
    echo "物理网卡: $NETCARD"
    echo "本地IP: $LOCAL_IP"
    echo ""
    echo "后续步骤:"
    echo "--------------------------------"
    echo "1. 编辑配置文件: vim /etc/borui/config/pppoe.json"
    echo "2. 将示例账号密码替换为你的真实宽带账号"
    echo "3. 重启服务: systemctl restart dialmtor"
    echo "4. 等待2-3分钟让拨号完成"
    echo "5. 使用命令检查: dialmger --list --more"
    echo ""
    print_separator
    log_success "安装完成！"
    echo ""
}

# 主安装函数
main_install() {
    print_title
    
    # 检查root权限
    check_root
    
    # 检测系统版本
    check_os
    
    # 检查网络
    check_network
    
    # 选择安装模式
    INSTALL_MODE=""
    if [ $# -eq 0 ]; then
        show_menu
        case $choice in
            1)
                INSTALL_MODE="quick"
                ;;
            2)
                INSTALL_MODE="custom"
                ;;
            3)
                INSTALL_MODE="deps"
                ;;
            4)
                log_info "退出安装"
                exit 0
                ;;
            *)
                log_error "无效选择"
                exit 1
                ;;
        esac
    else
        INSTALL_MODE="$1"
    fi
    
    # 记录开始时间
    START_TIME=$(date +%s)
    
    # 执行安装
    case $INSTALL_MODE in
        "quick")
            log_info "执行快速安装..."
            get_network_interfaces
            install_dependencies
            create_directories
            quick_install
            install_scripts
            configure_systemd
            start_service
            wait_and_verify
            ;;
        "custom")
            log_info "执行自定义安装..."
            get_network_interfaces
            get_pppoe_accounts
            install_dependencies
            create_directories
            create_pppoe_config
            install_scripts
            configure_systemd
            start_service
            wait_and_verify
            ;;
        "deps")
            log_info "仅安装依赖..."
            install_dependencies
            create_directories
            log_success "依赖安装完成"
            echo "请手动创建配置文件: /etc/borui/config/pppoe.json"
            echo "然后安装脚本: cp dialmtor.py /usr/local/bin/dialmtor"
            echo "            cp dialmger.py /usr/local/bin/dialmger"
            exit 0
            ;;
        *)
            log_error "未知的安装模式: $INSTALL_MODE"
            exit 1
            ;;
    esac
    
    # 计算安装时间
    END_TIME=$(date +%s)
    DURATION=$((END_TIME - START_TIME))
    MINUTES=$((DURATION / 60))
    SECONDS=$((DURATION % 60))
    
    # 显示摘要
    show_summary
    
    echo ""
    log_info "安装耗时: ${MINUTES}分${SECONDS}秒"
    echo ""
    
    # 询问是否启动管理菜单
    read -p "是否启动管理菜单? (y/n): " -n 1 -r
    echo
    if [[ $REPLY =~ ^[Yy]$ ]]; then
        pppoe-manager
    fi
}

# 处理命令行参数
if [ $# -gt 0 ]; then
    case $1 in
        "-h"|"--help")
            echo "用法: $0 [选项]"
            echo "选项:"
            echo "  -h, --help     显示帮助信息"
            echo "  -q, --quick    快速安装模式"
            echo "  -c, --custom   自定义安装模式"
            echo "  -d, --deps     仅安装依赖"
            echo "  无参数         显示交互式菜单"
            exit 0
            ;;
        "-q"|"--quick")
            main_install "quick"
            ;;
        "-c"|"--custom")
            main_install "custom"
            ;;
        "-d"|"--deps")
            main_install "deps"
            ;;
        *)
            log_error "未知参数: $1"
            echo "使用 $0 --help 查看帮助"
            exit 1
            ;;
    esac
else
    main_install
fi

exit 0/tmp/install_pppoe.sh