#!/bin/sh
# GL-iNet MT-3000/MT-6000 OP25固件专用脚本
# by @wukongdaily

HTTP_HOST="https://cafe.cpolar.cn/wkdaily/gl/raw/branch/main"
LIB_COMMON="/tmp/lib-common.sh"

# 设置全局命令 g
cp -f "$0" /usr/bin/g
chmod +x /usr/bin/g

# 下载并加载公共库
load_common_lib() {
    rm -f "$LIB_COMMON"
    wget -O "$LIB_COMMON" "$HTTP_HOST/lib/lib-common.sh" 2>/dev/null || {
        echo "下载公共库失败"
        exit 1
    }
    . "$LIB_COMMON"
}

# 安装argon紫色主题
do_install_argon_skin(){
    echo "正在安装argon紫色主题"
    wget -O /tmp/argon.run "$HTTP_HOST/theme/apk/argon/25-argon.run"
    sh /tmp/argon.run --target /tmp/abc --noexec
    apk add --allow-untrusted /tmp/abc/*.apk
    echo "argon紫色主题安装完成"
}

# 安装iStore商店
do_install_istore(){
    echo "正在安装iStore商店"
    wget -O /tmp/istore.run "$HTTP_HOST/theme/25-luci-app-store.run"
    sh /tmp/istore.run
    echo "iStore商店安装完成"
}

# 安装首页和网络向导
do_install_quickstart(){
    echo "正在安装首页和网络向导"
    is-opkg install luci-i18n-quickstart-zh-cn
    echo "首页和网络向导安装完成"
}

# 设置默认风扇开始工作的温度
do_setup_default_temp(){
    echo "正在设置默认风扇开始工作的温度"
    temp=50
    uci set glfan.@globals[0].temperature="$temp"
    uci set glfan.@globals[0].warn_temperature="$temp"
    uci set glfan.@globals[0].integration=4
    uci set glfan.@globals[0].differential=20
    uci commit glfan
    /etc/init.d/gl_fan restart
}

# 安装个性化辅助UI
do_install_ui_helper_apk(){
    echo "正在安装UI助手APK"
    wget -O /tmp/uihelper.apk "$HTTP_HOST/theme/apk/uihelper/uihelper.apk"
    apk add --allow-untrusted /tmp/uihelper.apk
    echo "UI助手APK安装完成"
}

# ============================================================================
# 一键执行函数
# ============================================================================
run_quick_install() {
    sh /usr/bin/init_luci.sh web
    add_author_info
    do_setup_default_temp
    do_install_argon_skin
    do_install_istore
    do_install_quickstart
    echo "安装完毕！请使用8080端口访问luci界面：http://192.168.8.1:8080"
}

# ============================================================================
# 主程序
# ============================================================================
load_common_lib

# 检查是否传入 --quick 参数
if [ "$1" = "--quick" ] || [ "$1" = "-q" ]; then
    run_quick_install
    exit 0
fi

while true; do
    clear
    gl_name=$(get_router_name)
    echo "***********************************************************************"
    echo "*      一键安装工具箱(for gl-inet Router)"
    echo "*      OP25固件专用 by @wukongdaily"
    echo "**********************************************************************"
    echo "*      当前的路由器型号: $gl_name"
    echo
    green "*******GL-iNet MT-3000 op25"
    green "*******GL-iNet MT-6000 op25"
    echo
    light_magenta " 1. 一键安装风格化"
    echo
    echo " 2. 安装Argon紫色主题"
    echo " 3. 安装iStore商店"
    echo " 4. 安装首页和网络向导"
    echo
    echo " Q. 退出本程序"
    echo
    read -p "请选择一个选项: " choice
    
    case $choice in
        1)
            run_quick_install
        ;;
        2)
            do_install_argon_skin
        ;;
        3)
            do_install_istore
        ;;
        4)
            do_install_quickstart
        ;;
        5)
            do_install_ui_helper_apk
        ;;
        q | Q)
            echo "退出"
            exit 0
        ;;
        *)
            echo "无效选项，请重新选择。"
        ;;
    esac
    
    read -p "按 Enter 键继续..."
done