@echo off
REM ==== Auto-elevate to Administrator ====
net session >nul 2>&1
if %errorLevel% neq 0 (
    echo Requesting administrator permission...
    echo This is needed to set environment variables.
    powershell -Command "Start-Process '%~dpnx0' -Verb RunAs"
    exit /b
)

chcp 437 >nul
title yangjinming.org All-in-One: Claude + OpenClaw

echo.
echo ============================================================
echo   yangjinming.org All-in-One Installer
echo   Claude Code + OpenClaw (Claude integrated)
echo ============================================================
echo.
echo This script will:
echo   * Install Claude Code CLI
echo   * Install OpenClaw CLI
echo   * Configure OpenClaw to use Claude as default model
echo   * Set up environment variables for both
echo.
echo Pre-requisite: Node.js 18+ must be installed.
echo.

where node >nul 2>&1
if errorlevel 1 (
    echo [ERROR] Node.js not found.
    echo Please install Node.js 18 or newer from:
    echo   https://nodejs.org/
    echo.
    pause
    exit /b 1
)

set /p USER_KEY="Enter your API Key (sk-...): "
if "%USER_KEY%"=="" (
    echo [ERROR] No API Key provided. Cancelling.
    pause
    exit /b 1
)

echo.
echo [1/5] Installing Claude Code CLI...
call npm install -g @anthropic-ai/claude-code --registry=https://registry.npmmirror.com

echo.
echo [2/5] Installing OpenClaw CLI...
call npm install -g openclaw --registry=https://registry.npmmirror.com
if errorlevel 1 (
    echo [INFO] Trying alternate package name...
    call npm install -g @openclaw/cli --registry=https://registry.npmmirror.com
)

echo.
echo [3/5] Setting Claude Code environment variables...
setx ANTHROPIC_BASE_URL "https://yangjinming.org/anthropic" >nul 2>&1
if errorlevel 1 (
    echo [INFO] setx failed, using PowerShell fallback...
    powershell -Command "[Environment]::SetEnvironmentVariable('ANTHROPIC_BASE_URL', 'https://yangjinming.org/anthropic', 'User')"
)
setx ANTHROPIC_API_KEY "%USER_KEY%" >nul 2>&1
if errorlevel 1 (
    powershell -Command "[Environment]::SetEnvironmentVariable('ANTHROPIC_API_KEY', '%USER_KEY%', 'User')"
)
setx ANTHROPIC_AUTH_TOKEN "%USER_KEY%" >nul 2>&1
if errorlevel 1 (
    powershell -Command "[Environment]::SetEnvironmentVariable('ANTHROPIC_AUTH_TOKEN', '%USER_KEY%', 'User')"
)

echo.
echo [4/5] Preparing OpenClaw config...
if not exist "%USERPROFILE%\.openclaw" mkdir "%USERPROFILE%\.openclaw" >nul 2>&1
if exist "%USERPROFILE%\.openclaw\openclaw.json" (
    copy "%USERPROFILE%\.openclaw\openclaw.json" "%USERPROFILE%\.openclaw\openclaw.json.backup" >nul
    echo Existing config backed up.
)

echo.
echo [5/5] Writing OpenClaw config with yangjinming Claude provider...
(
echo {
echo   "models": {
echo     "mode": "merge",
echo     "providers": {
echo       "yangjinming": {
echo         "baseUrl": "https://yangjinming.org/anthropic",
echo         "apiKey": "%USER_KEY%",
echo         "api": "anthropic-messages",
echo         "models": [
echo           { "id": "claude-opus-4-8", "name": "Claude Opus 4.8", "contextWindow": 200000, "maxTokens": 8192 },
echo           { "id": "claude-sonnet-4-6", "name": "Claude Sonnet 4.6", "contextWindow": 200000, "maxTokens": 8192 },
echo           { "id": "claude-haiku-4-5-20251001", "name": "Claude Haiku 4.5", "contextWindow": 200000, "maxTokens": 8192 }
echo         ]
echo       }
echo     }
echo   },
echo   "agents": {
echo     "defaults": {
echo       "model": { "primary": "yangjinming/claude-sonnet-4-6" }
echo     }
echo   }
echo }
) > "%USERPROFILE%\.openclaw\openclaw.json"

echo.
echo ============================================================
echo   [SUCCESS] Everything is set up!
echo ============================================================
echo.
echo What was done:
echo   * Claude Code CLI installed and configured
echo   * OpenClaw CLI installed
echo   * Claude (Opus/Sonnet/Haiku) registered in OpenClaw
echo   * Default OpenClaw model = claude-sonnet-4-6
echo.
echo NEXT STEPS:
echo   1. Close THIS CMD window.
echo   2. Open a NEW CMD window.
echo   3. Try any of these:
echo.
echo      claude                          (Claude Code in terminal)
echo      openclaw chat --local           (Chat with Claude via OpenClaw)
echo      openclaw gateway run            (Run OpenClaw as background gateway)
echo.
echo Console:  https://yangjinming.org/console.html
echo Manual:   https://yangjinming.org/coding-agent.html
echo.
pause
