@echo off
if not "%~1"=="__gbk__" (
    chcp 936 >nul
    "%~f0" __gbk__
    exit /b
)
setlocal EnableDelayedExpansion
title 一键 DD 系统重装工具

set "SCRIPT_URL_GITHUB=https://raw.githubusercontent.com/bin456789/reinstall/main/reinstall.bat"
set "SCRIPT_URL_CN=https://cnb.cool/bin456789/reinstall/-/git/raw/main/reinstall.bat"
set "SCRIPT_NAME=reinstall.bat"

cd /d "%~dp0"

rem 需要管理员权限
fltmc >nul 2>&1
if errorlevel 1 (
    echo 请以管理员身份运行此脚本
    pause
    exit /b 1
)

call :auto_download
if errorlevel 1 (
    pause
    exit /b 1
)

:main_menu
cls
echo ========================================
echo 一键 DD 系统重装工具
echo ========================================
echo.
echo 1) 安装 Linux 系统
echo 2) 安装 Windows 系统
echo 3) 特殊功能 (DD镜像/Alpine/netboot.xyz)
echo 4) 取消重装
echo 5) 查看系统要求
echo 6) 退出
echo.
if exist "%SCRIPT_NAME%" echo 重装脚本已就绪
echo.
set "choice="
set /p "choice=请选择 [1-6]: "
if "%choice%"=="1" goto menu_linux
if "%choice%"=="2" goto menu_windows
if "%choice%"=="3" goto menu_special
if "%choice%"=="4" goto do_reset
if "%choice%"=="5" goto show_requirements
if "%choice%"=="6" goto done
echo 无效选择
pause
goto main_menu

:do_reset
call "%SCRIPT_NAME%" reset
pause
goto main_menu

:done
pause
endlocal
exit /b 0

:auto_download
call :defender_tips
if errorlevel 1 exit /b 1
if exist "%SCRIPT_NAME%" exit /b 0

:download_menu
echo 重装脚本未下载
echo.
echo 请选择下载源:
echo 1) 国内镜像 (推荐国内服务器)
echo 2) GitHub 官方源
echo.
set "dl="
set /p "dl=请选择 [1-2]: "
set "URL=%SCRIPT_URL_CN%"
if "%dl%"=="2" set "URL=%SCRIPT_URL_GITHUB%"
del /q "%SCRIPT_NAME%" 2>nul
echo 正在下载...
certutil -urlcache -f -split "%URL%" "%SCRIPT_NAME%" >nul 2>&1
if exist "%SCRIPT_NAME%" (
    echo 下载完成: %SCRIPT_NAME%
    exit /b 0
)

echo 下载失败，请检查网络连接
echo 请确认已关闭 Windows Defender 实时保护后重试
goto download_menu

:defender_tips
rem 检测 Defender 实时保护，必须关闭后才能继续
set "rtp="
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-MpComputerStatus).RealTimeProtectionEnabled" 2^>nul') do set "rtp=%%a"
if /i not "%rtp%"=="True" exit /b 0
echo.
echo 检测到 Windows Defender 实时保护已开启，可能会拦截下载
echo   1. 打开 Windows 安全中心
echo   2. 病毒和威胁防护 - 管理设置
echo   3. 先关闭“篡改保护”，再关闭“实时保护”
echo   4. 或在“排除项”中添加本脚本所在文件夹
echo.
set "open_wd="
set /p "open_wd=是否现在打开 Windows 安全中心? [Y/n]: "
if /i not "!open_wd!"=="n" (
    start windowsdefender:
    echo 请在安全中心关闭实时保护后按任意键继续...
    pause >nul
    goto defender_tips
)
echo 未关闭 Windows Defender，退出
exit /b 1

:show_requirements
cls
echo ========================================
echo 系统配置要求
echo ========================================
echo.
echo Alpine:
echo   内存: 256 MB
echo   硬盘: 1 GB
echo.
echo Debian / Kali:
echo   内存: 256 MB
echo   硬盘: 1 ~ 1.5 GB
echo.
echo Ubuntu:
echo   内存: 512 MB
echo   硬盘: 2 GB
echo.
echo 其他 Linux 发行版:
echo   内存: 512 MB
echo   硬盘: 5 GB
echo.
echo 飞牛 fnOS / FygoOS:
echo   内存: 512 MB
echo   硬盘: 10 GB
echo.
echo Windows (DD):
echo   内存: 512 MB
echo   硬盘: 取决于镜像
echo.
echo Windows (ISO):
echo   Vista/7/8.x: 内存 512 MB, 硬盘 25 GB
echo   10/11: 内存 1 GB, 硬盘 25 GB
echo.
pause
goto main_menu

:menu_special
cls
echo ========================================
echo 选择特殊功能
echo ========================================
echo.
echo 1) DD Raw 镜像到硬盘
echo 2) 引导到 Alpine Live OS
echo 3) 引导到 netboot.xyz
echo 4) 返回主菜单
echo.
set "choice="
set /p "choice=请选择 [1-4]: "
if "%choice%"=="1" goto sp_dd
if "%choice%"=="2" goto sp_alpine
if "%choice%"=="3" goto sp_netboot
if "%choice%"=="4" goto main_menu
echo 无效选择
pause
goto menu_special

:sp_dd
echo.
set "dd_img="
set /p "dd_img=请输入镜像链接: "
if not defined dd_img goto menu_special
echo 警告: 此操作将清除硬盘所有数据!
call :confirm_execute
if errorlevel 1 (
    echo 已取消
    pause
    goto main_menu
)
call "%SCRIPT_NAME%" dd --img "%dd_img%"
goto done

:sp_alpine
echo.
echo 引导到 Alpine Live OS（内存系统，不会删除数据）
set "ap_user="
set /p "ap_user=请设置用户名 (默认 root): "
if not defined ap_user set "ap_user=root"
set "ap_pass="
set /p "ap_pass=请设置 %ap_user% 密码 (不输入则使用随机密码): "
if not defined ap_pass (
    call :gen_password
    set "ap_pass=!WIN_PASS!"
)
call "%SCRIPT_NAME%" alpine --hold 1 --username "%ap_user%" --password "%ap_pass%"
goto done

:sp_netboot
echo.
echo 引导到 netboot.xyz
call "%SCRIPT_NAME%" netboot.xyz
goto done

:confirm_execute
set "ans="
set /p "ans=确认执行? [y/n]: "
if /i "%ans%"=="y" exit /b 0
if /i "%ans%"=="n" exit /b 1
echo 请输入 y 执行或 n 取消
goto confirm_execute

:gen_password
set "WIN_PASS="
for /f "delims=" %%p in ('powershell -NoProfile -Command "$c=48..57+65..90+97..122; -join (1..16 | ForEach-Object { [char]($c | Get-Random) })"') do set "WIN_PASS=%%p"
if not defined WIN_PASS set "WIN_PASS=Aa1%RANDOM%%RANDOM%%RANDOM%"
echo 已生成随机密码: %WIN_PASS%
exit /b 0

:menu_linux
cls
echo ========================================
echo 选择 Linux 发行版
echo ========================================
echo.
echo 1) Debian (9, 10, 11, 12, 13)
echo 2) Ubuntu (18.04, 20.04, 22.04, 24.04, 26.04, 26.10)
echo 3) CentOS Stream (9, 10)
echo 4) Rocky (8, 9, 10)
echo 5) AlmaLinux (8, 9, 10)
echo 6) Fedora (44, 45)
echo 7) Alpine (3.21, 3.22, 3.23, 3.24)
echo 8) Arch Linux (滚动更新)
echo 9) Kali (滚动更新)
echo 10) openSUSE (16.0, 16.1, Tumbleweed)
echo 11) openEuler (20.03, 22.03, 24.03, 26.09)
echo 12) Oracle (8, 9, 10)
echo 13) Anolis (7, 8, 23)
echo 14) OpenCloudOS (8, 9, 23)
echo 15) Gentoo (滚动更新)
echo 16) NixOS (26.05)
echo 17) RedHat (qcow2 镜像)
echo 18) Alibaba Cloud Linux
echo 19) TencentOS Server
echo 20) 飞牛 fnOS (1)
echo 21) FygoOS (1)
echo 22) 安同 OS (滚动更新)
echo 23) 返回主菜单
echo.
set "choice="
set /p "choice=请选择 [1-23]: "
set "system="
set "version="
set "img_url="
set "ver_prompt="
if "%choice%"=="1" ( set "system=debian" & set "ver_prompt=9, 10, 11, 12, 13" & goto linux_version )
if "%choice%"=="2" ( set "system=ubuntu" & set "ver_prompt=18.04, 20.04, 22.04, 24.04, 26.04, 26.10" & goto linux_version )
if "%choice%"=="3" ( set "system=centos" & set "ver_prompt=9, 10" & goto linux_version )
if "%choice%"=="4" ( set "system=rocky" & set "ver_prompt=8, 9, 10" & goto linux_version )
if "%choice%"=="5" ( set "system=almalinux" & set "ver_prompt=8, 9, 10" & goto linux_version )
if "%choice%"=="6" ( set "system=fedora" & set "ver_prompt=44, 45" & goto linux_version )
if "%choice%"=="7" ( set "system=alpine" & set "ver_prompt=3.21, 3.22, 3.23, 3.24" & goto linux_version )
if "%choice%"=="8" ( set "system=arch" & goto linux_common )
if "%choice%"=="9" ( set "system=kali" & goto linux_common )
if "%choice%"=="10" ( set "system=opensuse" & set "ver_prompt=16.0, 16.1, tumbleweed" & goto linux_version )
if "%choice%"=="11" ( set "system=openeuler" & set "ver_prompt=20.03, 22.03, 24.03, 26.09" & goto linux_version )
if "%choice%"=="12" ( set "system=oracle" & set "ver_prompt=8, 9, 10" & goto linux_version )
if "%choice%"=="13" ( set "system=anolis" & set "ver_prompt=7, 8, 23" & goto linux_version )
if "%choice%"=="14" ( set "system=opencloudos" & set "ver_prompt=8, 9, 23" & goto linux_version )
if "%choice%"=="15" ( set "system=gentoo" & goto linux_common )
if "%choice%"=="16" ( set "system=nixos" & set "version=26.05" & goto linux_common )
if "%choice%"=="17" goto linux_redhat
if "%choice%"=="18" goto linux_alinux
if "%choice%"=="19" goto linux_tencent
if "%choice%"=="20" ( set "system=fnos" & set "version=1" & goto linux_common )
if "%choice%"=="21" ( set "system=fygoos" & set "version=1" & goto linux_common )
if "%choice%"=="22" ( set "system=aosc" & goto linux_common )
if "%choice%"=="23" goto main_menu
echo 无效选择
pause
goto menu_linux

:linux_version
echo 可用版本: %ver_prompt%
set "version="
set /p "version=请输入版本号 (默认最新): "
goto linux_common

:linux_redhat
echo 请提供 RedHat qcow2 镜像链接
set "img_url="
set /p "img_url=镜像链接: "
if not defined img_url (
    echo 镜像链接不能为空
    pause
    goto menu_linux
)
set "system=redhat"
goto linux_common

:linux_alinux
echo Alibaba Cloud Linux 版本:
echo 2) Alibaba Cloud Linux 2
echo 3) Alibaba Cloud Linux 3
set "al_ver="
set /p "al_ver=请选择版本 [2-3]: "
if "%al_ver%"=="2" set "img_url=https://alinux2.oss-cn-hangzhou.aliyuncs.com/aliyun_2_1903_x64_20G_nocloud_alibase_20230103.qcow2"
if "%al_ver%"=="3" set "img_url=https://alinux3.oss-cn-hangzhou.aliyuncs.com/aliyun_3_x64_20G_nocloud_alibase_20260122.qcow2"
if not defined img_url (
    echo 无效版本
    pause
    goto menu_linux
)
set "system=redhat"
goto linux_common

:linux_tencent
echo TencentOS Server 版本:
echo 3) TencentOS Server 3
echo 4) TencentOS Server 4
set "tc_ver="
set /p "tc_ver=请选择版本 [3-4]: "
if "%tc_ver%"=="3" set "img_url=https://mirrors.cloud.tencent.com/tencentos/3.3/images/qcow2/x86_64/TencentOS-Server-GenericCloud-3.3-20251223.0.x86_64.qcow2"
if "%tc_ver%"=="4" set "img_url=https://mirrors.cloud.tencent.com/tencentos/4.4/images/qcow2/x86_64/TencentOS-Server-GenericCloud-4.4-20260126.0.x86_64.qcow2"
if not defined img_url (
    echo 无效版本
    pause
    goto menu_linux
)
set "system=redhat"
goto linux_common

:linux_common
if not defined system goto menu_linux
rem Debian 云镜像安装：EOL 版本默认开启，其它默认关闭
set "use_ci=0"
set "ci_answer="
if /i not "%system%"=="debian" goto linux_ask_user
set "ci_default=0"
if "%version%"=="9" set "ci_default=1"
if "%version%"=="10" set "ci_default=1"
if "%version%"=="11" set "ci_default=1"
if "%ci_default%"=="1" goto linux_ci_eol
set /p "ci_answer=是否使用云镜像安装 --ci?（默认关闭，适合 CPU 较慢的机器）[y/N]: "
if /i "%ci_answer%"=="y" set "use_ci=1"
goto linux_ask_user
:linux_ci_eol
set /p "ci_answer=是否使用云镜像安装 --ci?（EOL 版本默认开启，安装更快）[Y/n]: "
if /i not "%ci_answer%"=="n" set "use_ci=1"
:linux_ask_user
set "linux_user="
set /p "linux_user=请设置用户名 (默认 root): "
if not defined linux_user set "linux_user=root"
set "linux_pass="
set /p "linux_pass=请设置 %linux_user% 密码 (不输入则使用随机密码): "
if not defined linux_pass (
    call :gen_password
    set "linux_pass=!WIN_PASS!"
)

:linux_ssh_port
set "ssh_port="
set /p "ssh_port=请设置 SSH 端口 (默认 22): "
if not defined ssh_port goto linux_confirm
echo %ssh_port%| findstr /r "^[0-9][0-9]*$" >nul
if errorlevel 1 goto linux_ssh_bad
if %ssh_port% LSS 1 goto linux_ssh_bad
if %ssh_port% GTR 65535 goto linux_ssh_bad
goto linux_confirm
:linux_ssh_bad
echo 无效的端口号，请输入 1-65535 之间的数字
goto linux_ssh_port

:linux_confirm
echo.
echo 警告: 此操作将清除硬盘所有数据!
if /i "%system%"=="debian" (
    if "%use_ci%"=="1" (echo 云镜像安装: 是) else (echo 云镜像安装: 否)
)
echo 最终生效的用户名: %linux_user%
echo 最终生效的密码: %linux_pass%
call :confirm_execute
if errorlevel 1 (
    echo 已取消
    pause
    goto main_menu
)
set ARGS=%system%
if defined version set ARGS=%ARGS% %version%
if "%use_ci%"=="1" set ARGS=%ARGS% --ci
if defined img_url set ARGS=%ARGS% --img "%img_url%"
set ARGS=%ARGS% --username "%linux_user%" --password "%linux_pass%"
if defined ssh_port set ARGS=%ARGS% --ssh-port %ssh_port%
echo 执行命令: %SCRIPT_NAME% %ARGS%
call "%SCRIPT_NAME%" %ARGS%
goto done

:menu_windows
cls
echo ========================================
echo 选择 Windows 系统
echo ========================================
echo.
echo 1) Windows Vista / 7
echo 2) Windows 8 / 8.1
echo 3) Windows 10
echo 4) Windows 11
echo 5) Windows Server 2008 / 2008 R2
echo 6) Windows Server 2012 / 2012 R2
echo 7) Windows Server 2016 / 2019
echo 8) Windows Server 2022 / 2025
echo 9) 自定义 ISO / 磁力链接
echo 10) 返回主菜单
echo.
set "choice="
set /p "choice=请选择 [1-10]: "
set "WIN_IMAGE="
set "WIN_ISO="
if "%choice%"=="1" goto win_g1
if "%choice%"=="2" goto win_g2
if "%choice%"=="3" goto win_g3
if "%choice%"=="4" goto win_g4
if "%choice%"=="5" goto win_g5
if "%choice%"=="6" goto win_g6
if "%choice%"=="7" goto win_g7
if "%choice%"=="8" goto win_g8
if "%choice%"=="9" goto win_custom_iso
if "%choice%"=="10" goto main_menu
echo 无效选择
pause
goto menu_windows

:win_g1
call :choose_image "Windows Vista / 7" "Windows Vista Starter" "Windows Vista HomeBasic" "Windows Vista HomePremium" "Windows Vista Business" "Windows Vista Ultimate" "Windows Vista Enterprise" "Windows 7 Starter" "Windows 7 HomeBasic" "Windows 7 HomePremium" "Windows 7 Professional" "Windows 7 Ultimate" "Windows 7 Enterprise"
if errorlevel 2 goto menu_windows
goto win_after_image

:win_g2
call :choose_image "Windows 8 / 8.1" "Windows 8" "Windows 8 Pro" "Windows 8 Enterprise" "Windows 8.1" "Windows 8.1 Pro" "Windows 8.1 Enterprise"
if errorlevel 2 goto menu_windows
goto win_after_image

:win_g3
call :choose_image "Windows 10" "Windows 10 Home" "Windows 10 Home Single Language" "Windows 10 Pro" "Windows 10 Pro Education" "Windows 10 Pro for Workstations" "Windows 10 Education" "Windows 10 Enterprise" "Windows 10 Enterprise 2015 LTSB" "Windows 10 Enterprise 2016 LTSB" "Windows 10 Enterprise LTSC 2019" "Windows 10 Enterprise LTSC 2021" "Windows 10 IoT Enterprise" "Windows 10 IoT Enterprise 2015 LTSB" "Windows 10 IoT Enterprise 2016 LTSB" "Windows 10 IoT Enterprise LTSC 2019" "Windows 10 IoT Enterprise LTSC 2021"
if errorlevel 2 goto menu_windows
goto win_after_image

:win_g4
call :choose_image "Windows 11" "Windows 11 Home" "Windows 11 Home Single Language" "Windows 11 Pro" "Windows 11 Pro Education" "Windows 11 Pro for Workstations" "Windows 11 Education" "Windows 11 Enterprise" "Windows 11 Enterprise LTSC 2024" "Windows 11 IoT Enterprise" "Windows 11 IoT Enterprise LTSC 2024" "Windows 11 IoT Enterprise Subscription" "Windows 11 IoT Enterprise Subscription LTSC 2024"
if errorlevel 2 goto menu_windows
goto win_after_image

:win_g5
call :choose_image "Windows Server 2008 / 2008 R2" "Windows Longhorn ServerWeb" "Windows Longhorn ServerWebCore" "Windows Longhorn ServerStandard" "Windows Longhorn ServerStandardCore" "Windows Longhorn ServerEnterprise" "Windows Longhorn ServerEnterpriseCore" "Windows Longhorn ServerDatacenter" "Windows Longhorn ServerDatacenterCore" "Windows Server 2008 R2 ServerWeb" "Windows Server 2008 R2 ServerWebCore" "Windows Server 2008 R2 ServerStandard" "Windows Server 2008 R2 ServerStandardCore" "Windows Server 2008 R2 ServerEnterprise" "Windows Server 2008 R2 ServerEnterpriseCore" "Windows Server 2008 R2 ServerDatacenter" "Windows Server 2008 R2 ServerDatacenterCore"
if errorlevel 2 goto menu_windows
goto win_after_image

:win_g6
call :choose_image "Windows Server 2012 / 2012 R2" "Windows Server 2012 ServerStandard" "Windows Server 2012 ServerStandardCore" "Windows Server 2012 ServerDatacenter" "Windows Server 2012 ServerDatacenterCore" "Windows Server 2012 R2 ServerStandard" "Windows Server 2012 R2 ServerStandardCore" "Windows Server 2012 R2 ServerDatacenter" "Windows Server 2012 R2 ServerDatacenterCore"
if errorlevel 2 goto menu_windows
goto win_after_image

:win_g7
call :choose_image "Windows Server 2016 / 2019" "Windows Server 2016 ServerStandard" "Windows Server 2016 ServerStandardCore" "Windows Server 2016 ServerDatacenter" "Windows Server 2016 ServerDatacenterCore" "Windows Server 2019 ServerStandard" "Windows Server 2019 ServerStandardCore" "Windows Server 2019 ServerDatacenter" "Windows Server 2019 ServerDatacenterCore"
if errorlevel 2 goto menu_windows
goto win_after_image

:win_g8
call :choose_image "Windows Server 2022 / 2025" "Windows Server 2022 ServerStandard" "Windows Server 2022 ServerStandardCore" "Windows Server 2022 ServerDatacenter" "Windows Server 2022 ServerDatacenterCore" "Windows Server 2025 ServerStandard" "Windows Server 2025 ServerStandardCore" "Windows Server 2025 ServerDatacenter" "Windows Server 2025 ServerDatacenterCore"
if errorlevel 2 goto menu_windows
goto win_after_image

:win_custom_iso
echo 支持 http(s) 链接或 magnet 磁力链接
set "WIN_ISO="
set /p "WIN_ISO=请输入 ISO 链接: "
if not defined WIN_ISO (
    echo ISO 链接不能为空
    pause
    goto menu_windows
)
echo 请输入映像名称（不知道可随便填，重启后按 SSH 错误提示重新输入）
echo 示例: Windows 11 Pro / Windows Server 2022 ServerDatacenter
set "WIN_IMAGE="
set /p "WIN_IMAGE=映像名称: "
if not defined WIN_IMAGE (
    echo 映像名称不能为空
    pause
    goto menu_windows
)
goto win_after_image

rem 用法: call :choose_image "标题" "映像1" "映像2" ...
rem 选中后写入 WIN_IMAGE；返回 0 选中，2 返回上级
:choose_image
setlocal EnableDelayedExpansion
set "ci_title=%~1"
shift
set /a ci_n=0
echo.
echo ========================================
echo !ci_title!
echo ========================================
:ci_print
if "%~1"=="" goto ci_prompt
set /a ci_n+=1
set "ci_item_!ci_n!=%~1"
echo !ci_n!^) %~1
shift
goto ci_print
:ci_prompt
set /a ci_total=ci_n+1
echo !ci_total!^) 返回上级
echo.
set "ci_choice="
set /p "ci_choice=请选择 [1-!ci_total!]: "
if not defined ci_choice goto ci_prompt
set /a ci_c=ci_choice 2>nul
if !ci_c! LSS 1 goto ci_invalid
if !ci_c! GTR !ci_total! goto ci_invalid
if !ci_c! EQU !ci_total! goto ci_back
for %%v in (!ci_c!) do set "ci_sel=!ci_item_%%v!"
endlocal & set "WIN_IMAGE=%ci_sel%"
exit /b 0
:ci_back
endlocal & set "WIN_IMAGE="
exit /b 2
:ci_invalid
echo 无效选择
goto ci_prompt

:win_after_image
if not defined WIN_IMAGE goto menu_windows
set "WIN_LANG="
call :choose_lang
if not defined WIN_LANG set "WIN_LANG=zh-cn"
set "WIN_USER="
set /p "WIN_USER=请设置用户名 (默认 administrator): "
if not defined WIN_USER set "WIN_USER=administrator"
set "WIN_PASS="
set /p "WIN_PASS=请设置 %WIN_USER% 密码 (不输入则使用随机密码): "
if not defined WIN_PASS call :gen_password

:win_rdp
set "WIN_RDP="
set /p "WIN_RDP=请设置 RDP 端口 (默认 3389): "
if not defined WIN_RDP goto win_confirm
echo %WIN_RDP%| findstr /r "^[0-9][0-9]*$" >nul
if errorlevel 1 goto win_rdp_bad
if %WIN_RDP% LSS 1 goto win_rdp_bad
if %WIN_RDP% GTR 65535 goto win_rdp_bad
goto win_confirm
:win_rdp_bad
echo 无效的端口号，请输入 1-65535 之间的数字
goto win_rdp

:win_confirm
echo.
echo 警告: 此操作将清除硬盘所有数据!
echo 映像名称: %WIN_IMAGE%
if defined WIN_ISO (
    echo ISO 链接: %WIN_ISO%
) else (
    echo ISO 来源: 自动查找官方 ISO
)
echo 语言: %WIN_LANG%
echo 用户名: %WIN_USER%
echo 密码: %WIN_PASS%
call :confirm_execute
if errorlevel 1 (
    echo 已取消
    pause
    goto main_menu
)
set ARGS=windows --image-name "%WIN_IMAGE%" --lang "%WIN_LANG%" --username "%WIN_USER%" --password "%WIN_PASS%"
if defined WIN_ISO set ARGS=%ARGS% --iso "%WIN_ISO%"
if defined WIN_RDP set ARGS=%ARGS% --rdp-port %WIN_RDP%
echo 执行命令: %SCRIPT_NAME% %ARGS%
call "%SCRIPT_NAME%" %ARGS%
goto done

rem 选择 Windows 语言，结果写入 WIN_LANG；回车默认 zh-cn
:choose_lang
setlocal EnableDelayedExpansion
set /a cl_n=0
echo.
echo ========================================
echo 选择系统语言
echo ========================================
for %%L in (
    "ar-sa 阿拉伯语"
    "bg-bg 保加利亚语"
    "cs-cz 捷克语"
    "da-dk 丹麦语"
    "de-de 德语"
    "el-gr 希腊语"
    "en-gb 英语(国际)"
    "en-us 英语"
    "es-es 西班牙语"
    "es-mx 西班牙语(拉美)"
    "et-ee 爱沙尼亚语"
    "fi-fi 芬兰语"
    "fr-ca 法语(加拿大)"
    "fr-fr 法语"
    "he-il 希伯来语"
    "hr-hr 克罗地亚语"
    "hu-hu 匈牙利语"
    "it-it 意大利语"
    "ja-jp 日语"
    "ko-kr 韩语"
    "lt-lt 立陶宛语"
    "lv-lv 拉脱维亚语"
    "nb-no 挪威语"
    "nl-nl 荷兰语"
    "pl-pl 波兰语"
    "pt-pt 葡萄牙语"
    "pt-br 葡萄牙语(巴西)"
    "ro-ro 罗马尼亚语"
    "ru-ru 俄语"
    "sk-sk 斯洛伐克语"
    "sl-si 斯洛文尼亚语"
    "sr-latn 塞尔维亚语(拉丁)"
    "sv-se 瑞典语"
    "th-th 泰语"
    "tr-tr 土耳其语"
    "uk-ua 乌克兰语"
    "zh-cn 简体中文"
    "zh-tw 繁体中文"
    "zh-hk 繁体中文(香港)"
) do (
    set /a cl_n+=1
    set "cl_item_!cl_n!=%%~L"
    echo !cl_n!^) %%~L
)
set /a cl_total=cl_n+1
echo !cl_total!^) 手动输入语言代码
echo.

:cl_prompt
set "cl_choice="
set /p "cl_choice=请选择 [1-!cl_total!] (回车默认 zh-cn): "
if not defined cl_choice goto cl_default
set /a cl_c=cl_choice 2>nul
if !cl_c! LSS 1 goto cl_invalid
if !cl_c! GTR !cl_total! goto cl_invalid
if !cl_c! EQU !cl_total! goto cl_manual
for %%v in (!cl_c!) do set "cl_sel=!cl_item_%%v!"
for /f "tokens=1 delims= " %%a in ("!cl_sel!") do set "cl_sel=%%a"
endlocal & set "WIN_LANG=%cl_sel%"
exit /b 0

:cl_default
endlocal & set "WIN_LANG=zh-cn"
exit /b 0

:cl_manual
set "cl_manual="
set /p "cl_manual=请输入语言代码 (如 zh-cn): "
if not defined cl_manual (
    echo 语言代码不能为空
    goto cl_prompt
)
endlocal & set "WIN_LANG=%cl_manual%"
exit /b 0

:cl_invalid
echo 无效选择
goto cl_prompt




