#!/bin/sh
# GL-iNet 路由器一键安装工具箱 - 主脚本
# by @wukongdaily

# 定义颜色输出函数
red() { printf "\033[31m\033[01m%s\033[0m\n" "$1"; }
green() { printf "\033[32m\033[01m%s\033[0m\n" "$1"; }
yellow() { printf "\033[33m\033[01m%s\033[0m\n" "$1"; }
blue() { printf "\033[34m\033[01m%s\033[0m\n" "$1"; }
light_magenta() { printf "\033[95m\033[01m%s\033[0m\n" "$1"; }
light_yellow() { printf "\033[93m\033[01m%s\033[0m\n" "$1"; }
cyan() { printf "\033[38;2;0;255;255m%s\033[0m\n" "$1"; }

HTTP_HOST="https://cafe.cpolar.cn/wkdaily/gl/raw/branch/main"
SCRIPT_DIR="/tmp/gl-scripts"

##获取软路由型号信息
get_router_name() {
    model_info=$(cat /tmp/sysinfo/model)
    echo "$model_info"
}

## 下载并运行脚本
download_and_run_script() {
    local script_name="$1"
    mkdir -p "$SCRIPT_DIR"
    local script_path="$SCRIPT_DIR/$script_name"
    
    green "正在下载 $script_name..."
    wget -O "$script_path" "$HTTP_HOST/$script_name" 2>/dev/null || {
        red "下载 $script_name 失败"
        return 1
    }
    chmod +x "$script_path"
    green "正在启动 $script_name..."
    sh "$script_path"
}

## 自动检测路由器型号并调用对应脚本
auto_detect_and_run() {
    gl_name=$(get_router_name)
    echo "检测到路由器型号: $gl_name"
    
    case "$gl_name" in
        *BE6500* | *be6500*)
            download_and_run_script "be6500.sh"
        ;;
        *BE9300* | *be9300*)
            download_and_run_script "be9300.sh"
        ;;
        *BE3600* | *be3600*)
            download_and_run_script "be3600.sh"
        ;;
        *MT5000* | *mt5000*)
            download_and_run_script "mt5000.sh"
        ;;
        *MT3600* | *mt3600*)
            download_and_run_script "mt3600.sh"
        ;;
        *E5800* | *e5800* | *Mudi* | *mudi*)
            download_and_run_script "mudi7.sh"
        ;;
        *MT-3000* | *mt3000* | *MT3000*)
            if grep -q "25\." /etc/openwrt_release 2>/dev/null; then
                download_and_run_script "gl-inet-op25.sh"
                elif grep -q "24\." /etc/openwrt_release 2>/dev/null; then
                download_and_run_script "gl-inet-op24.sh"
            else
                download_and_run_script "gl-inet.sh"
            fi
        ;;
        *MT-6000* | *mt6000* | *MT6000*)
            if grep -q "25\." /etc/openwrt_release 2>/dev/null; then
                download_and_run_script "gl-inet-op25.sh"
                elif grep -q "24\." /etc/openwrt_release 2>/dev/null; then
                download_and_run_script "gl-inet-op24.sh"
            else
                download_and_run_script "gl-inet.sh"
            fi
        ;;
        *MT-2500* | *mt2500* | *MT2500*)
            download_and_run_script "gl-inet.sh"
        ;;
        *)
            red "抱歉，暂不支持该机型: $gl_name"
            echo "您可以手动选择机型运行对应脚本"
            return 1
        ;;
    esac
}

## 手动选择机型
select_model() {
    echo
    echo "请选择您的路由器型号:"
    echo
    green " 1. GL-iNet BE-6500"
    green " 2. GL-iNet BE-9300"
    green " 3. GL-iNet BE-3600"
    green " 4. GL-iNet MT-5000"
    green " 5. GL-iNet MT-3600"
    green " 6. GL-iNet Mudi 7 (GL-E5800)"
    echo
    green " 7. GL-iNet MT-2500A"
    green " 8. GL-iNet MT-3000"
    green " 9. GL-iNet MT-6000"
    echo
    green "10. GL-iNet MT-3000 (OP24固件)"
    green "11. GL-iNet MT-6000 (OP24固件)"
    green "12. GL-iNet MT-3000/MT-6000 (OP25固件)"
    echo
    green "13. MT-3000 换分区助手 (U盘扩容)"
    echo
    echo " Q. 退出"
    echo
    read -p "请输入选项: " choice
    
    case $choice in
        1)
            download_and_run_script "be6500.sh"
        ;;
        2)
            download_and_run_script "be9300.sh"
        ;;
        3)
            download_and_run_script "be3600.sh"
        ;;
        4)
            download_and_run_script "mt5000.sh"
        ;;
        5)
            download_and_run_script "mt3600.sh"
        ;;
        6)
            download_and_run_script "mudi7.sh"
        ;;
        7 | 8 | 9)
            download_and_run_script "gl-inet.sh"
        ;;
        10 | 11)
            download_and_run_script "gl-inet-op24.sh"
        ;;
        12)
            download_and_run_script "gl-inet-op25.sh"
        ;;
        13)
            download_and_run_script "mt-3000/mt3000.sh"
        ;;
        q | Q)
            echo "退出"
            exit 0
        ;;
        *)
            red "无效选项，请重新选择"
        ;;
    esac
}

## 主菜单
show_main_menu() {
    gl_name=$(get_router_name)
    clear
    echo "***********************************************************************"
    echo "*                                                                     *"
    echo "*           GL-iNet 路由器一键安装工具箱 by @wukongdaily              *"
    echo "*                                                                     *"
    echo "***********************************************************************"
    echo
    green "当前路由器型号: $gl_name"
    echo
    echo "支持以下操作:"
    echo
    light_magenta " A. 自动检测并运行对应脚本 (推荐)"
    light_magenta " B. 手动选择机型"
    echo
    echo " Q. 退出"
    echo
}

## 主循环
while true; do
    show_main_menu
    echo
    read -p "请输入选项: " main_choice
    
    case $main_choice in
        a | A)
            auto_detect_and_run
        ;;
        b | B)
            select_model
        ;;
        q | Q)
            echo "退出"
            exit 0
        ;;
        *)
            red "无效选项，请重新选择"
        ;;
    esac
    
    echo
    read -p "按 Enter 键返回主菜单..."
done