#!/bin/bash

# 设置颜色变量
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
NC='\033[0m' # 恢复默认颜色

# 模块URL配置（修正错误的URL）
MODULE_URLS=(
    "https://alist.lhcay.cn/d/fnos/代码托管/webdav挂载/auto_mount_webdav.sh"  # 1. 安装WebDAV挂载
    "https://alist.lhcay.cn/d/fnos/代码托管/宝塔面板/install_latest.sh"  # 2. 安装宝塔开心版11.3.0
    "https://alist.lhcay.cn/d/fnos/代码托管/宝塔面板/update_panel.sh"  # 3. 更新宝塔开心版
    "https://alist.lhcay.cn/d/fnos/代码托管/modules/service_monitoring_module.sh"  # 4. 服务状态监控
    "https://alist.lhcay.cn/d/fnos/代码托管/modules/file_management_module.sh"  # 5. 文件管理
    "https://alist.lhcay.cn/d/fnos/代码托管/modules/security_hardening_module.sh"  # 6. 系统安全加固
    "https://alist.lhcay.cn/d/fnos/代码托管/modules/backup_recovery_module.sh"  # 7. 备份与恢复
    "https://alist.lhcay.cn/d/fnos/代码托管/modules/package_management_module.sh"  # 8. 软件包管理
    "https://alist.lhcay.cn/d/fnos/代码托管/modules/performance_optimization_module.sh"  # 9. 性能优化
    "https://alist.lhcay.cn/d/fnos/代码托管/modules/module10.sh"  # 10. 模块10
    "https://alist.lhcay.cn/d/fnos/代码托管/modules/module11.sh"  # 11. 模块11
    "https://alist.lhcay.cn/d/fnos/代码托管/modules/module12.sh"  # 12. 模块12
    "https://alist.lhcay.cn/d/fnos/代码托管/modules/module13.sh"  # 13. 模块13（修正URL）
    "https://alist.lhcay.cn/d/fnos/代码托管/modules/module14.sh"  # 14. 模块14
    "https://alist.lhcay.cn/d/fnos/代码托管/modules/module15.sh"  # 15. 模块15
    "https://alist.lhcay.cn/d/fnos/代码托管/modules/module16.sh"  # 16. 模块16
    "https://alist.lhcay.cn/d/fnos/代码托管/modules/module17.sh"  # 17. 模块17（修正URL）
    "https://alist.lhcay.cn/d/fnos/代码托管/modules/module18.sh"  # 18. 模块18
    "https://alist.lhcay.cn/d/fnos/代码托管/modules/module19.sh"  # 19. 模块19
    "https://alist.lhcay.cn/d/fnos/代码托管/modules/module20.sh"  # 20. 模块20
)

# 模块名称（用于显示）
MODULE_NAMES=(
    "安装WebDAV挂载"
    "安装宝塔开心版11.3.0"
    "更新宝塔开心版"
    "服务状态监控"
    "文件管理"
    "系统安全加固"
    "备份与恢复"
    "软件包管理"
    "性能优化"
    "模块10"
    "模块11"
    "模块12"
    "模块13"
    "模块14"
    "模块15"
    "模块16"
    "模块17"
    "模块18"
    "模块19"
    "模块20"
)

# 显示标题
function show_title() {
    clear
    echo -e "${PURPLE}"
    echo -e "║${YELLOW}          ██████╗   █████╗  ███╗ ██╗ ██████╗   █████╗  ███╗ ██╗          ${PURPLE}║"
    echo -e "║${YELLOW}          ██╔══██╗ ██╔══██╗ ████╗██║ ██╔══██╗ ██╔══██╗ ████╗██║          ${PURPLE}║"
    echo -e "║${YELLOW}          ██║  ██║ ███████║ ██╔████║ ██║  ██║ ███████║ ██╔████║          ${PURPLE}║"
    echo -e "║${YELLOW}          ██║  ██║ ██╔══██║ ██║╚███║ ██║  ██║ ██╔══██║ ██║╚███║          ${PURPLE}║"
    echo -e "║${YELLOW}          ██████╔╝ ██║  ██║ ██║ ╚██║ ██████╔╝ ██║  ██║ ██║ ╚██║          ${PURPLE}║"
    echo -e "║${YELLOW}          ╚═════╝  ╚═╝  ╚═╝ ╚═╝  ╚═╝ ╚═════╝  ╚═╝  ╚═╝ ╚═╝  ╚═╝          ${PURPLE}║"   
    echo -e "${CYAN}                   蛋蛋脚本管理工具箱                         ${PURPLE}"
    echo -e "  ${RED}※${NC} 当前用户: $(whoami) ${RED}※${NC}                               "
}

# 显示菜单
function show_menu() {
    echo -e " ${YELLOW}请选择操作:${GREEN}"
    for i in {0..19}; do
        printf "  ${GREEN}%2d.${NC} %s\n" $((i+1)) "${MODULE_NAMES[$i]}"
    done
    echo -e "  ${GREEN}21.${NC} 退出脚本"
    echo -e "${NC}"
}

# 加载并运行模块

function load_and_run_module() {
    local choice=$1
    local index=$((choice-1))
    local url="${MODULE_URLS[$index]}"
    local module_name="${MODULE_NAMES[$index]}"
    local tmp_file=$(mktemp /tmp/module_XXXXX.sh)  # 创建临时文件

    # 检查URL有效性
    if [[ "$url" != http* ]]; then
        echo -e "${RED}✖ 错误：无效的URL地址！${NC}"
        return 1
    fi

    echo -e "${GREEN}▶ 正在执行模块 ${choice}: ${module_name}${NC}"
    echo -e "${CYAN}URL: ${url}${NC}"

    # 下载脚本到临时文件
    if ! curl -sSL -o "$tmp_file" "$url"; then
        echo -e "${RED}✖ 脚本下载失败！${NC}"
        return 1
    fi

    # 赋予执行权限并运行
    chmod +x "$tmp_file"
    if ! "$tmp_file"; then
        echo -e "${RED}✖ 脚本执行失败！${NC}"
    else
        echo -e "${GREEN}✔ 脚本执行完成！${NC}"
    fi

    # 清理临时文件
    rm -f "$tmp_file"
}

# 主程序循环
function main() {
    while true; do
        show_title
        show_menu
        
        read -p "请输入选项编号 (1-21): " choice
        
        # 检查输入是否为空
        if [ -z "$choice" ]; then
            echo -e "${RED}✖ 请输入选项编号！${NC}"
            sleep 1
            continue
        fi
        
        # 退出选项
        if [ "$choice" == "21" ]; then
            echo -e "${GREEN}▶ 感谢使用，再见！${NC}"
            exit 0
        fi
        
        # 检查输入是否为数字
        if ! [[ "$choice" =~ ^[0-9]+$ ]]; then
            echo -e "${RED}✖ 请输入有效的数字！${NC}"
            sleep 1
            continue
        fi
        
        # 处理选项
        if [ "$choice" -ge 1 ] && [ "$choice" -le 20 ]; then
            load_and_run_module "$choice"
        else
            echo -e "${RED}✖ 无效的选项，请输入1-20的数字！${NC}"
        fi
        
        echo ""
        read -p "按Enter键返回主菜单..."
    done
}

# 启动主程序
main