@echo off
chcp 936 >nul
setlocal

set "PKG=npc-windows-amd64.tar.gz"
set "URL1=https://scripts.tongdavision.com/downloads/%PKG%"
set "URL2=https://vpn.tongdavision.com/downloads/%PKG%"
set "SERVER=api.tongdavision.com:50463"
set "DEFAULT_VKEY=4a1374ca89"
set "DIR=%USERPROFILE%\npc"
set "TMPPKG=%TEMP%\%PKG%"

echo ==============================================
echo  瞳达精视 · NPC 客户端一键安装 (Windows 10/11)
echo ==============================================
echo.

if not exist "%DIR%" mkdir "%DIR%"

echo ==^> 下载客户端...
powershell -NoProfile -ExecutionPolicy Bypass -Command "[Net.ServicePointManager]::SecurityProtocol='Tls12'; $ProgressPreference='SilentlyContinue'; $us=@('%URL1%','%URL2%'); foreach($u in $us){ try { if (Test-Path '%TMPPKG%') { Remove-Item '%TMPPKG%' -Force }; Invoke-WebRequest -Uri $u -OutFile '%TMPPKG%' -UseBasicParsing -TimeoutSec 120; if (Test-Path '%TMPPKG%') { break } } catch { } }; if (-not (Test-Path '%TMPPKG%')) { exit 1 }"
if errorlevel 1 goto fail
if not exist "%TMPPKG%" goto fail

echo ==^> 解压...
where tar >nul 2>&1
if errorlevel 1 goto notar

tar -xzf "%TMPPKG%" -C "%DIR%"
if errorlevel 1 goto fail
del /q "%TMPPKG%" >nul 2>&1

if not exist "%DIR%\npc.exe" (
  for /r "%DIR%" %%f in (npc.exe) do (
    if not exist "%DIR%\npc.exe" copy /y "%%f" "%DIR%\npc.exe" >nul
  )
)
if not exist "%DIR%\npc.exe" goto fail
echo ==^> 安装完成: %DIR%\npc.exe
echo.

set "VKEY="
set /p "VKEY=请输入 vkey（直接回车使用默认值 %DEFAULT_VKEY%）： "
if not defined VKEY set "VKEY=%DEFAULT_VKEY%"

echo ==^> 启动客户端（关闭此窗口即停止）
cd /d "%DIR%"
"%DIR%\npc.exe" -server=%SERVER% -type=tcp -vkey=%VKEY%
echo.
echo 客户端已退出。
pause
exit /b 0

:notar
echo.
echo [错误] 系统里没有 tar.exe，请确认是 Windows 10 1803 或更新的版本。
echo        也可以手动用 7-Zip 解压 %TMPPKG% 到 %DIR%
goto fail

:fail
echo.
echo 安装失败，请把本窗口截图发给 IT。
pause
exit /b 1
