﻿@echo off
setlocal
chcp 65001 >nul
title AI-fanstapi ChatGPT Chinese UI Installer
echo AI-fanstapi ChatGPT Chinese UI installer
echo Version: zh-launcher-20260726-chatgptzh14
echo.
set "BOOTSTRAP_URL=https://vip.rapidapi.top/downloads/zh-win-bootstrap-bba252f4377d8216-AI-fanstapi-Chinese-Windows-Installer.ps1"
set "BOOTSTRAP_SHA256=a6e62775d7fa42d31f5fdf72e2374cef50fad2c66f2e3e0416acc52c9e4286db"
set "PAYLOAD_URL=https://vip.rapidapi.top/downloads/zh-payload-3f95aab2417133a6-AI-fanstapi-ChatGPT-Chinese-Payload.zip"
set "PAYLOAD_SHA256=ca3773a18379f673a7d32c96af3c98ced16e8f3ae7b0b2f57be03a0bb9a564d9"
set "WORK_BASE=%LOCALAPPDATA%"
if not defined WORK_BASE set "WORK_BASE=%TEMP%"
set "WORK_DIR=%WORK_BASE%\AI-fanstapi\ChatGPTZhInstaller"
if not exist "%WORK_DIR%" mkdir "%WORK_DIR%" >nul 2>nul
if not exist "%WORK_DIR%" (
  echo [error] cannot create work directory:
  echo [error] %WORK_DIR%
  set "FINAL_EXIT_CODE=1"
  goto :WaitThenExit
)
set "BOOTSTRAP_PS1=%WORK_DIR%\AI-fanstapi-ChatGPT-Zh-Installer-20260726-chatgptzh14.ps1"
set "POWERSHELL_EXE=%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe"
if not exist "%POWERSHELL_EXE%" set "POWERSHELL_EXE=powershell.exe"
if exist "%BOOTSTRAP_PS1%" del /f /q "%BOOTSTRAP_PS1%" >nul 2>nul
echo [prepare] downloading launcher...
set "BOOTSTRAP_FULL_URL=%BOOTSTRAP_URL%?cb=%RANDOM%%RANDOM%%RANDOM%"
if exist "%SystemRoot%\System32\curl.exe" (
  "%SystemRoot%\System32\curl.exe" -fL --retry 3 --connect-timeout 30 -H "Cache-Control: no-cache" -o "%BOOTSTRAP_PS1%" "%BOOTSTRAP_FULL_URL%"
) else (
  "%POWERSHELL_EXE%" -NoLogo -NoProfile -ExecutionPolicy Bypass -Command "$ProgressPreference='SilentlyContinue'; $u='%BOOTSTRAP_FULL_URL%'; [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12; Invoke-WebRequest -UseBasicParsing -Headers @{'Cache-Control'='no-cache'} -Uri $u -OutFile '%BOOTSTRAP_PS1%'"
)
if errorlevel 1 (
  echo [prepare] curl download failed, retrying with PowerShell...
  "%POWERSHELL_EXE%" -NoLogo -NoProfile -ExecutionPolicy Bypass -Command "$ProgressPreference='SilentlyContinue'; $u='%BOOTSTRAP_FULL_URL%'; [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12; Invoke-WebRequest -UseBasicParsing -Headers @{'Cache-Control'='no-cache'} -Uri $u -OutFile '%BOOTSTRAP_PS1%'"
)
if errorlevel 1 (
  echo [error] launcher download failed.
  set "FINAL_EXIT_CODE=1"
  goto :WaitThenExit
)
"%POWERSHELL_EXE%" -NoLogo -NoProfile -Command "$actual=(Get-FileHash -Algorithm SHA256 -LiteralPath '%BOOTSTRAP_PS1%').Hash.ToLowerInvariant(); if ($actual -ne '%BOOTSTRAP_SHA256%') { Write-Error ('launcher SHA256 mismatch: ' + $actual); exit 1 }"
if errorlevel 1 (
  echo [error] launcher verification failed.
  set "FINAL_EXIT_CODE=1"
  goto :WaitThenExit
)
findstr /C:"Version: zh-bootstrap" "%BOOTSTRAP_PS1%" >nul
if errorlevel 1 (
  echo [error] downloaded launcher is not zh-bootstrap.
  echo [debug] %BOOTSTRAP_PS1%
  type "%BOOTSTRAP_PS1%"
  set "FINAL_EXIT_CODE=1"
  goto :WaitThenExit
)
findstr /C:"Expand-ZipSafely" "%BOOTSTRAP_PS1%" >nul
if errorlevel 1 (
  echo [error] downloaded launcher does not contain safe unzip code.
  echo [debug] %BOOTSTRAP_PS1%
  type "%BOOTSTRAP_PS1%"
  set "FINAL_EXIT_CODE=1"
  goto :WaitThenExit
)
echo [prepare] launcher downloaded.
echo.
set "FANSTAPI_ZH_BOOTSTRAP=%BOOTSTRAP_PS1%"
set "FANSTAPI_ZH_PAYLOAD_URL=%PAYLOAD_URL%"
set "FANSTAPI_ZH_PAYLOAD_SHA256=%PAYLOAD_SHA256%"
call :RunBootstrapFile
set "RUN_RC=%ERRORLEVEL%"
if not "%RUN_RC%"=="0" (
  echo.
  echo [prepare] PowerShell -File failed with code %RUN_RC%; retrying once...
  call :RunBootstrapCommand
  set "RUN_RC=%ERRORLEVEL%"
)
if not "%RUN_RC%"=="0" (
  echo.
  echo [error] installer failed or Windows blocked PowerShell. Exit code: %RUN_RC%
  echo [help] If the line above says "Access is denied.", this computer refused to run PowerShell.
  echo [help] Common causes: antivirus, AppLocker, company policy, or running from a restricted folder.
  echo [help] Try right-clicking this file and choose "Run as administrator", or allow Windows PowerShell.
  set "FINAL_EXIT_CODE=%RUN_RC%"
  goto :WaitThenExit
)
echo.
echo [ok] ChatGPT Chinese UI installer finished.
set "FINAL_EXIT_CODE=0"
goto :WaitThenExit

:RunBootstrapFile
"%POWERSHELL_EXE%" -NoLogo -NoProfile -ExecutionPolicy Bypass -File "%BOOTSTRAP_PS1%" -PackageUrl "%PAYLOAD_URL%" -ExpectedSha256 "%PAYLOAD_SHA256%"
exit /b %ERRORLEVEL%

:RunBootstrapCommand
"%POWERSHELL_EXE%" -NoLogo -NoProfile -ExecutionPolicy Bypass -Command "$script=$env:FANSTAPI_ZH_BOOTSTRAP; & $script -PackageUrl $env:FANSTAPI_ZH_PAYLOAD_URL -ExpectedSha256 $env:FANSTAPI_ZH_PAYLOAD_SHA256"
exit /b %ERRORLEVEL%

:WaitThenExit
set "EXIT_CODE=%FINAL_EXIT_CODE%"
if "%EXIT_CODE%"=="" set "EXIT_CODE=0"
if "%EXIT_CODE%"=="0" (
  echo.
  echo This window will close in 10 seconds.
  timeout /t 10 /nobreak >nul 2>nul
) else (
  echo.
  echo This window will close in 30 seconds. Exit code: %EXIT_CODE%
  timeout /t 30 /nobreak >nul 2>nul
)
exit /b %EXIT_CODE%

