@echo off
setlocal EnableExtensions DisableDelayedExpansion

title unlimitedcodex GPT-5.5 XHigh Codex setup
set "UCX_BASE_URL=https://claude.kadirr.dev"
set "UCX_MODEL=gpt-5.5"
set "UCX_CODEX_DIR=%USERPROFILE%\.codex"
set "UCX_CONFIG_PATH=%UCX_CODEX_DIR%\config.toml"
set "UCX_AUTH_PATH=%UCX_CODEX_DIR%\auth.json"

echo.
echo unlimitedcodex GPT-5.5 XHigh setup
echo ----------------------------------
echo Use this file only for GPT-5.5 XHigh Weekly or Monthly.
echo Model: %UCX_MODEL%
echo Base URL: %UCX_BASE_URL%
echo Existing config files will be backed up.
echo.

where powershell >nul 2>nul || (
  echo PowerShell is required. Use the manual setup guide instead.
  pause
  exit /b 1
)

if not exist "%UCX_CODEX_DIR%" mkdir "%UCX_CODEX_DIR%" >nul 2>nul
if not exist "%UCX_CODEX_DIR%" (
  echo Could not create %UCX_CODEX_DIR%.
  pause
  exit /b 1
)

for /f "usebackq delims=" %%I in (`powershell -NoProfile -Command "Get-Date -Format yyyyMMdd-HHmmss"`) do set "UCX_TS=%%I"
if not defined UCX_TS set "UCX_TS=backup"
if exist "%UCX_CONFIG_PATH%" copy /Y "%UCX_CONFIG_PATH%" "%UCX_CONFIG_PATH%.bak-%UCX_TS%" >nul
if exist "%UCX_AUTH_PATH%" copy /Y "%UCX_AUTH_PATH%" "%UCX_AUTH_PATH%.bak-%UCX_TS%" >nul

echo Paste the API key delivered for your GPT-5.5 XHigh package.
echo Input is hidden. Press Enter after pasting.
for /f "usebackq delims=" %%K in (`powershell -NoProfile -Command "$secure = Read-Host 'unlimitedcodex API key' -AsSecureString; $bstr = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($secure); try { [Runtime.InteropServices.Marshal]::PtrToStringBSTR($bstr) } finally { [Runtime.InteropServices.Marshal]::ZeroFreeBSTR($bstr) }"`) do set "UCX_API_KEY=%%K"

if not defined UCX_API_KEY (
  echo API key cannot be empty. No changes were completed.
  pause
  exit /b 1
)

(
echo model_provider = "OpenAI"
echo model = "%UCX_MODEL%"
echo review_model = "%UCX_MODEL%"
echo model_reasoning_effort = "xhigh"
echo disable_response_storage = true
echo network_access = "enabled"
echo.
echo [model_providers.OpenAI]
echo name = "OpenAI"
echo base_url = "%UCX_BASE_URL%"
echo wire_api = "responses"
echo requires_openai_auth = true
echo.
echo [features]
echo goals = true
echo.
echo [sandbox_workspace_write]
echo network_access = true
) > "%UCX_CONFIG_PATH%"

powershell -NoProfile -ExecutionPolicy Bypass -Command "$ErrorActionPreference = 'Stop'; [ordered]@{ auth_mode = 'apikey'; OPENAI_API_KEY = $env:UCX_API_KEY } | ConvertTo-Json | Set-Content -LiteralPath $env:UCX_AUTH_PATH -Encoding UTF8"
if errorlevel 1 (
  echo Failed to write auth.json.
  pause
  exit /b 1
)

set "UCX_API_KEY="
echo.
echo GPT-5.5 XHigh setup completed.
echo Restart Codex completely, then run one small prompt first.
echo Keep auth.json private and never send the full key to support.
echo.
pause
endlocal
