@echo off
setlocal EnableExtensions

set "KIMI_CU_VERSION=latest"

set "KIMI_CU_POWERSHELL=%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe"
if not exist "%KIMI_CU_POWERSHELL%" (
  echo KimiCU installer could not find Windows PowerShell: %KIMI_CU_POWERSHELL% 1>&2
  exit /b 9009
)

set "KIMI_CU_SETUP_PS1=%~dpn0.ps1"
set "KIMI_CU_REMOVE_SETUP_PS1="
if exist "%KIMI_CU_SETUP_PS1%" goto run_setup

set "KIMI_CU_SETUP_PS1=%TEMP%\kimi-cu-setup-%RANDOM%-%RANDOM%.ps1"
"%KIMI_CU_POWERSHELL%" -NoProfile -ExecutionPolicy Bypass -Command "Invoke-WebRequest -Uri 'https://cdn.kimi.com/kimi-computer-use-windows/%KIMI_CU_VERSION%/setup_windows.ps1' -OutFile '%KIMI_CU_SETUP_PS1%'"
if errorlevel 1 exit /b %ERRORLEVEL%
set "KIMI_CU_REMOVE_SETUP_PS1=1"

:run_setup
"%KIMI_CU_POWERSHELL%" -NoProfile -ExecutionPolicy Bypass -File "%KIMI_CU_SETUP_PS1%" %*
set "KIMI_CU_EXIT_CODE=%ERRORLEVEL%"
if defined KIMI_CU_REMOVE_SETUP_PS1 del /q "%KIMI_CU_SETUP_PS1%" >nul 2>&1
exit /b %KIMI_CU_EXIT_CODE%
