#!/bin/bash

set -e
LANGUAGE="zh"

function set_language() {
    if [ "$LANGUAGE" == "zh" ]; then
        LANGUAGE="en"
    else
        LANGUAGE="zh"
    fi
}

# 一级菜单
function main_menu() {
    if [ "$LANGUAGE" == "zh" ]; then
        echo "==================== 主菜单 ===================="
        echo "1. 常用工具"
        echo "2. 自用工具"
        echo "99. 切换为 English"
        echo "0. 退出"
        echo "================================================"
    else
        echo "==================== Main Menu ===================="
        echo "1. Common Tools"
        echo "2. Private Tools"
        echo "99. Switch to 中文"
        echo "0. Exit"
        echo "==================================================="
    fi
}

# 常用工具菜单
function common_menu() {
    if [ "$LANGUAGE" == "zh" ]; then
        echo "==================== 常用工具 ===================="
        echo "1. 安装 3x-ui"
        echo "2. 流媒体解锁测试"
        echo "3. 安装 WARP"
        echo "4. IP 测试"
        echo "5. NodeQuality测试"
        echo "6. tool 工具箱"
        echo "7. SSH 工具集"
        echo "8. VPS-DDNS 脚本"
        echo "9. 一键 DD Debian 12"
        echo "10. 安装 CasaOS"
        echo "11. 添加 Swap"
        echo "12. 安装 Realm"
        echo "13. 网络质量体检"
        echo "14. 搭建kms服务器"
        echo "15. 启动并设置开机自启KMS服务"
        echo "0. 返回主菜单"
        echo "==================================================="
    else
        echo "==================== Common Tools ===================="
        echo "1. Install 3x-ui"
        echo "2. Stream Unlock Test"
        echo "3. Install WARP"
        echo "4. IP Check"
        echo "5. NodeQuality Tool"
        echo "6. Tool Toolbox"
        echo "7. SSH Toolkit"
        echo "8. VPS-DDNS Script"
        echo "9. One-click DD Debian 12"
        echo "10. Install CasaOS"
        echo "11. Add Swap"
        echo "12. Install Realm"
        echo "13. Network Quality Check"
        echo "14. Building A Kms Server"
        echo "15. Start Kms Server"
        echo "0. Back to Main Menu"
        echo "======================================================"
    fi
}

# 自用工具菜单（重新排序）
function private_menu() {
    if [ "$LANGUAGE" == "zh" ]; then
        echo "==================== 自用工具 ===================="
        echo "1. 安装 Soga"
        echo "2. 配置 Soga"
        echo "3. docker安装XrayR"
        echo "4. 获取reality指纹"
        echo "5. 升级系统到 Debian 12"
        echo "6. 安装 Docker 和 Docker Compose"
        echo "7. 清理磁盘"
        echo "8. 关闭防火墙"
        echo "9. 修改软件源"
        echo "10. 管理Cloudflare"
        echo "11. 配置ipc"
        echo "12. 安装Speedtest"
        echo "13. 配置NPC"
        echo "14. 修改主机hostname"
        echo "15. 一键搭建lighttpd"
        echo "16. 一键更换dns"
        echo "17. 一键安装tcping"
        echo "18. 文本编辑"
        echo "19. ASF挂卡"
        echo "20. 一键设置定时重启NPC服务"
        echo "0. 返回主菜单"
        echo "==================================================="
    else
        echo "==================== Private Tools ===================="
        echo "1. Install Soga"
        echo "2. Configure Soga"
        echo "3. Install XrayR in Docker"
        echo "4. Get reality fingerprint"
        echo "5. Upgrade to Debian 12"
        echo "6. Install Docker & Docker Compose"
        echo "7. Clean Disk"
        echo "8. Disable Firewall"
        echo "9. Change Package Source"
        echo "10. Managing Cloudflare"
        echo "11. Configuring IPC"
        echo "12. Install Speedtest"
        echo "13. Configuring NPC"
        echo "14. Modify the hostname"
        echo "15. One-click setup of lighttpd"
        echo "16. One-click Change DNS"
        echo "17. One-click installation of tcping"
        echo "18. Text Editing"
        echo "19. ASF"
        echo "20. One-click setup NPC service restart"
        echo "0. Back to Main Menu"
        echo "======================================================="
    fi
}

# 常用工具执行
function run_common() {
    case "$1" in
        1) bash <(curl -Ls https://raw.githubusercontent.com/mhsanaei/3x-ui/master/install.sh) ;;
        2) bash <(curl -L -s https://raw.githubusercontent.com/1-stream/RegionRestrictionCheck/main/check.sh) ;;
        3) wget -N https://gitlab.com/fscarmen/warp/-/raw/main/menu.sh && bash menu.sh d ;;
        4) bash <(curl -Ls IP.Check.Place) ;;
        5) bash <(curl -sL https://run.NodeQuality.com) ;;
        6) curl -sS -O https://raw.githubusercontent.com/jacktain/kejilion/main/kejilion.sh && chmod +x kejilion.sh && ./kejilion.sh ;;
        7) curl -fsSL https://raw.githubusercontent.com/eooce/ssh_tool/main/ssh_tool.sh -o ssh_tool.sh && chmod +x ssh_tool.sh && ./ssh_tool.sh ;;
        8) curl -skLo ue-ddns.sh ddns.03k.org && sh ue-ddns.sh ;;
        9) curl -fLO https://raw.githubusercontent.com/bohanyang/debi/master/debi.sh && chmod a+rx debi.sh && sudo ./debi.sh --cdn --network-console --ethx --bbr --version 12 --user root --password ng057720 ;;
        10) curl -fsSL https://get.casaos.io | sudo bash ;;
        11) wget https://raw.githubusercontent.com/zhucaidan/swap.sh/main/swap.sh && bash swap.sh ;;
        12) wget -N --no-check-certificate https://git.io/realm.sh && chmod +x realm.sh && ./realm.sh ;;
        13) bash <(curl -Ls https://Check.Place) -N ;;
        14) bash <(curl -Ls https://sh.digac.icu/shell/one-key-kms-debian.sh) -N ;;
        15) bash -c "$(curl -fsSL https://sh.digac.icu/shell/start_kms.sh)" ;;
        0) return ;;
        *) echo -e "\n❌ 无效输入 / Invalid choice." ;;
    esac
}

# 自用工具执行（重新排序）
function run_private() {
    case "$1" in
        1) bash <(curl -Ls https://raw.githubusercontent.com/vaxilu/soga/refs/heads/master/install.sh) ;;
        2) bash -c "$(curl -fsSL https://sh.digac.icu/shell/soga-conf-fl.sh)" ;;
        3) bash -c "$(curl -fsSL https://sh.digac.icu/shell/docker_vless_reality-1.sh)" ;;
        4) bash <(curl -Ls https://sh.digac.icu/shell/installautofingerprint.sh) ;;
        5) bash -c "$(curl -fsSL https://sh.digac.icu/shell/debian12.sh)" ;;
        6) bash -c "$(curl -fsSL https://sh.digac.icu/shell/install-docker.sh)" ;;
        7) bash -c "$(curl -fsSL https://sh.digac.icu/shell/clean-linux.sh)" ;;
        8) bash -c "$(curl -fsSL https://sh.digac.icu/shell/close-firewall.sh)" ;;
        9) bash -c "$(curl -fsSL https://sh.digac.icu/shell/change-source.sh)" ;;
        10) bash -c "$(curl -fsSL https://sh.digac.icu/shell/cloudflare.sh)" ;;
        11) bash -c "$(curl -fsSL https://sh.digac.icu/shell/get-ipc.sh)" ;;
        12) bash -c "$(curl -fsSL https://sh.digac.icu/shell/speedtest.sh)" ;;
        13) bash -c "$(curl -fsSL https://sh.digac.icu/shell/con_npc.sh)" ;;
        14) bash -c "$(curl -fsSL https://sh.digac.icu/shell/hostname.sh)" ;;
        15) bash -c "$(curl -fsSL https://sh.digac.icu/shell/lighttpd.sh)" ;;
        16) bash -c "$(curl -fsSL https://sh.digac.icu/shell/changedns.sh)" ;;
        17) bash -c "$(curl -fsSL https://sh.digac.icu/shell/tcping.sh)" ;;
        18) bash -c "$(curl -fsSL https://sh.digac.icu/shell/textedit.sh)" ;;
        19) bash -c "$(curl -fsSL https://sh.digac.icu/shell/asf.sh)" ;;
        20) bash -c "$(curl -fsSL https://sh.digac.icu/shell/oneclick_npcrestart_setup.sh)" ;;
        0) return ;;
        *) echo -e "\n❌ 无效输入 / Invalid choice." ;;
    esac
}

# 主循环
while true; do
    main_menu
    if [ "$LANGUAGE" == "zh" ]; then
        read -p "请输入编号: " main_choice
    else
        read -p "Enter your choice: " main_choice
    fi

    case "$main_choice" in
        1) # 常用工具
            while true; do
                common_menu
                read -p "选择编号: " choice
                run_common "$choice"
                [ "$choice" == "0" ] && break
                echo -e "\n✅ Done. Press Enter to continue..."
                read
            done ;;
        2) # 自用工具
            while true; do
                private_menu
                read -p "选择编号: " choice
                run_private "$choice"
                [ "$choice" == "0" ] && break
                echo -e "\n✅ Done. Press Enter to continue..."
                read
            done ;;
        99) set_language ;;
        0) echo -e "\n👋 Bye!"; exit 0 ;;
        *) echo -e "\n❌ 无效输入 / Invalid choice." ;;
    esac
done
