#!/bin/sh
# GL-iNet MT-3000/MT-6000 OP24固件专用脚本
# 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"
}

# MT系列专用：架构配置
add_arch() {
    if ! wget -O /etc/opkg/arch.conf $HTTP_HOST/mtarch/arch.conf; then
        echo "下载 arch.conf 失败，脚本终止。"
        exit 1
    fi
}

# OP24专用：切换到阿里云软件源
change_opkg_source() {
    if [[ "$gl_name" == *3000* ]]; then
        if ! wget -O /etc/opkg/distfeeds.conf $HTTP_HOST/mt-3000/distfeeds-24.conf; then
            echo "换源失败，脚本终止。"
            exit 1
        fi
    fi
}

# 安装应用商店和主题
install_istore_os_style() {
    do_install_argon_skin
    opkg install ttyd
    do_install_filetransfer
    is-opkg install app-meta-sftp
    is-opkg install 'app-meta-ddnsto'
    is-opkg install 'app-meta-diskman'
    FILE_PATH="/etc/openwrt_release"
    NEW_DESCRIPTION="Openwrt like iStoreOS Style by wukongdaily"
    CONTENT=$(cat $FILE_PATH)
    UPDATED_CONTENT=$(echo "$CONTENT" | sed "s/DISTRIB_DESCRIPTION='[^']*'/DISTRIB_DESCRIPTION='$NEW_DESCRIPTION'/")
    echo "$UPDATED_CONTENT" >$FILE_PATH
}

# 设置风扇工作温度
setup_cpu_fans() {
    uci set glfan.@globals[0].temperature=50
    uci set glfan.@globals[0].warn_temperature=50
    uci set glfan.@globals[0].integration=4
    uci set glfan.@globals[0].differential=20
    uci commit glfan
    /etc/init.d/gl_fan restart
}

# 恢复原厂OPKG配置
recovery_opkg_settings() {
    echo "# add your custom package feeds here" >/etc/opkg/customfeeds.conf
    mt3000_opkg="$HTTP_HOST/mt-3000/distfeeds-24.conf"
    wget -O /etc/opkg/distfeeds.conf ${mt3000_opkg}
}

# 更新首页风格
update_luci_app_quickstart() {
    if [ -f "/bin/is-opkg" ]; then
        is-opkg update
        is-opkg install luci-i18n-quickstart-zh-cn --force-depends >/dev/null 2>&1
        opkg install iptables-mod-tproxy
        opkg install iptables-mod-socket
        opkg install iptables-mod-iprange
        do_install_new_quickstart
        yellow "恭喜您!现在你的路由器已经变成iStoreOS风格啦!"
        green "现在您可以访问8080端口 查看是否生效 http://192.168.8.1:8080"
        green "更多up主项目和动态 请务必收藏我的导航站 https://tvhelper.cpolar.cn"
    else
        red "请先执行第一项 一键iStoreOS风格化"
    fi
}

# 安装文件管理器
do_install_filemanager() {
    echo "为避免bug,安装文件管理器之前,需要先iStore商店"
    do_istore_generic
    echo "接下来尝试安装文件管理器..."
    is-opkg install 'app-meta-linkease'
    echo "重新登录web页面,然后您可以访问: http://192.168.8.1/cgi-bin/luci/admin/services/linkease/file/?path=/root"
}

# ============================================================================
# 一键执行函数
# ============================================================================
run_quick_install() {
    sh /usr/bin/init_luci.sh web
    add_arch
    change_opkg_source
    if [[ "$gl_name" == *3000* ]]; then
        setup_cpu_fans
    fi
    do_istore_generic
    install_istore_os_style
    update_luci_app_quickstart
    setup_base_init
    do_install_ui_helper
    show_completion_message
}

# ============================================================================
# 主程序
# ============================================================================
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)
    result=$gl_name"一键iStoreOS风格化(新版)"
    result=$(echo "$result" | sed 's/ like iStoreOS//')
    echo "***********************************************************************"
    echo "*      一键安装工具箱(for gl-inet Router)"
    echo "*      OP24固件专用 by @wukongdaily"
    echo "**********************************************************************"
    echo "*      当前的路由器型号: $gl_name" | sed 's/ like iStoreOS//'
    echo
    echo "********仅支持op24固件的机型*********************************************"
    green "*******GL-iNet MT-3000 op24"
    green "*******GL-iNet MT-6000 op24"
    echo "******************下次调用 直接输入快捷键 g  *****************************"
    echo
    light_magenta " 1. $result"
    echo
    echo " 2. 设置风扇开始工作的温度(仅限MT3000)"
    echo " 3. 安装Argon紫色主题"
    echo " 4. 安装文件管理器"
    light_magenta " 5. 隐藏首页非必要UI元素"
    light_magenta " 6. 安装个性化UI辅助插件(by VMatrices)"
    echo
    echo " Q. 退出本程序"
    echo
    read -p "请选择一个选项: " choice

    case $choice in
    1)
        run_quick_install
        ;;
    2)
        case "$gl_name" in
        *3000*)
            set_glfan_temp
            ;;
        *)
            echo "当前的路由器型号: $gl_name" | sed 's/ like iStoreOS//'
            echo "并非MT3000 它没有风扇 无需设置"
            ;;
        esac
        ;;
    3)
        do_install_argon_skin
        ;;
    4)
        do_install_filemanager
        ;;
    5)
        hide_ui_elements
        ;;
    6)
        do_install_ui_helper
        ;;
    q | Q)
        echo "退出"
        exit 0
        ;;
    *)
        echo "无效选项，请重新选择。"
        ;;
    esac

    read -p "按 Enter 键继续..."
done