#!/bin/sh
# GL-iNet BE6500 一键iStoreOS风格化脚本
# by @wukongdaily

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

# 下载并加载公共库
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"
}

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

# 安装应用商店和主题
install_istore_os_style() {
    do_install_argon_skin
    opkg install luci-i18n-ttyd-zh-cn
    opkg install openssh-sftp-server
    do_install_filetransfer
    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
}

# 首页和网络向导
do_quickstart() {
    download_lib_quickstart
    download_luci_quickstart
    opkg install /tmp/ipk_downloads/*.ipk
    do_install_new_quickstart
}

# ============================================================================
# 一键执行函数
# ============================================================================
run_quick_install() {
    sh /usr/bin/init_luci.sh web
    add_arch_64bit
    install_istore_os_style
    setup_base_init
    do_istore
    do_quickstart
    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-iNet Be6500 一键iStoreOS风格化(新版)"
    echo "***********************************************************************"
    echo "*      一键安装工具箱(for gl-inet be6500)  by @wukongdaily"
    echo "**********************************************************************"
    echo "*******支持的机型列表***************************************************"
    green "*******GL-iNet BE-6500********"
    green "请确保您的固件版本在4.7.2以上"
    echo

    light_magenta " 1. $result"
    echo
    light_magenta " 2. 安装argon紫色主题"
    echo
    light_magenta " 3. 单独安装iStore商店"
    echo
    light_magenta " 4. 隐藏首页无用元素"
    echo
    light_magenta " 5. 设置风扇工作温度"
    echo
    light_magenta " 6. 安装个性化UI辅助插件(by VMatrices)"
    echo
    light_magenta " 7. 安装新首页"
    echo
    light_magenta " 8. 恢复出厂设置"
    echo
    echo " Q. 退出本程序"
    echo
    read -p "请选择一个选项: " choice

    case $choice in
    1)
        run_quick_install
        ;;
    2)
        do_install_argon_skin
        ;;
    3)
        do_istore
        ;;
    4)
        hide_ui_elements
        ;;
    5)
        set_glfan_temp
        ;;
    6)
        do_install_ui_helper
        ;;
    7)
        do_install_new_quickstart
        ;;
    8)
        recovery
        ;;
    q | Q)
        echo "退出"
        exit 0
        ;;
    *)
        echo "无效选项，请重新选择。"
        ;;
    esac

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