@echo off
set "SCRIPT_DIR=%~dp0"
set "SCRIPT_DIR=%SCRIPT_DIR:~0,-1%"
set "PKG_DIR=%SCRIPT_DIR%\.."
if exist "%PKG_DIR%\scripts\bootstrap.mjs" goto :pkg_found
for /d %%P in ("%ProgramFiles%\nodejs\node_modules\@kespace\ks-cli*") do (
  if exist "%%P\scripts\bootstrap.mjs" set "PKG_DIR=%%P" & goto :pkg_found
)
for /d %%P in ("%ProgramFiles%\nodejs\node_modules\@ke\ks-cli*") do (
  if exist "%%P\scripts\bootstrap.mjs" set "PKG_DIR=%%P" & goto :pkg_found
)
for /d %%P in ("%APPDATA%\npm\node_modules\@kespace\ks-cli*") do (
  if exist "%%P\scripts\bootstrap.mjs" set "PKG_DIR=%%P" & goto :pkg_found
)
for /d %%P in ("%APPDATA%\npm\node_modules\@ke\ks-cli*") do (
  if exist "%%P\scripts\bootstrap.mjs" set "PKG_DIR=%%P" & goto :pkg_found
)
:pkg_found
set "KS_AGENT="
if exist "%PKG_DIR%\package.json" (
  where node >nul 2>&1
  if not errorlevel 1 (
    for /f "usebackq delims=" %%A in (`node -pe "try{JSON.parse(require('fs').readFileSync(process.argv[1],'utf8')).ksAgent||''}catch{''}" "%PKG_DIR%\package.json" 2^>nul`) do set "KS_AGENT=%%A"
  )
)
if "%KS_AGENT%"=="" (
  echo %PKG_DIR% | findstr /i "hermes" >nul && set "KS_AGENT=hermes"
)
if "%KS_AGENT%"=="" (
  echo %PKG_DIR% | findstr /i "openclaw" >nul && set "KS_AGENT=openclaw"
)
if "%KS_AGENT%"=="" (
  echo %PKG_DIR% | findstr /i "codex" >nul && set "KS_AGENT=codex"
)
if "%KS_AGENT%"=="" set "KS_AGENT=cc"
set "KS_SKILL_DIR=kespace"
set "KS_LEGACY_SKILL_DIR=ks"
if /i "%KS_AGENT%"=="hermes" set "KS_SKILL_DIR=kespace-hermes"
if /i "%KS_AGENT%"=="openclaw" set "KS_SKILL_DIR=kespace-openclaw"
if /i "%KS_AGENT%"=="codex" set "KS_SKILL_DIR=kespace-codex"
if /i "%KS_AGENT%"=="hermes" set "KS_LEGACY_SKILL_DIR=ks-hermes"
if /i "%KS_AGENT%"=="openclaw" set "KS_LEGACY_SKILL_DIR=ks-openclaw"
if /i "%KS_AGENT%"=="codex" set "KS_LEGACY_SKILL_DIR=ks-codex"
set "KS_COMMAND=kespace"
set "KS_LEGACY_COMMAND=ks"
if /i "%KS_AGENT%"=="hermes" set "KS_COMMAND=kespace-hermes"
if /i "%KS_AGENT%"=="openclaw" set "KS_COMMAND=kespace-openclaw"
if /i "%KS_AGENT%"=="codex" set "KS_COMMAND=kespace-codex"
if /i "%KS_AGENT%"=="hermes" set "KS_LEGACY_COMMAND=ks-hermes"
if /i "%KS_AGENT%"=="openclaw" set "KS_LEGACY_COMMAND=ks-openclaw"
if /i "%KS_AGENT%"=="codex" set "KS_LEGACY_COMMAND=ks-codex"
if /i "%~1"=="install" (
  if exist "%PKG_DIR%\scripts\postinstall.mjs" (
    where node >nul 2>&1
    if not errorlevel 1 (
      node "%PKG_DIR%\scripts\postinstall.mjs"
      exit /b %ERRORLEVEL%
    ) else (
      call "%PKG_DIR%\scripts\win-node.cmd" "%PKG_DIR%\scripts\postinstall.mjs"
      exit /b %ERRORLEVEL%
    )
  )
  echo ks: installer not found - package is incomplete: "%PKG_DIR%"
  exit /b 1
)
if exist "%PKG_DIR%\scripts\bootstrap.mjs" (
  where node >nul 2>&1
  if not errorlevel 1 (
    node "%PKG_DIR%\scripts\bootstrap.mjs"
  ) else (
    call "%PKG_DIR%\scripts\win-node.cmd" "%PKG_DIR%\scripts\bootstrap.mjs"
  )
  if errorlevel 1 if not exist "%USERPROFILE%\.agents\bin\%KS_COMMAND%.exe" if not exist "%USERPROFILE%\.agents\bin\%KS_LEGACY_COMMAND%.exe" exit /b 1
)
if not exist "%USERPROFILE%\.agents\bin\%KS_COMMAND%.exe" (
  if exist "%USERPROFILE%\.agents\bin\%KS_LEGACY_COMMAND%.exe" (
    "%USERPROFILE%\.agents\bin\%KS_LEGACY_COMMAND%.exe" %*
    exit /b %ERRORLEVEL%
  ) else (
    echo ks: binary not found. Run: "%PKG_DIR%\scripts\repair.cmd"
    exit /b 1
  )
)
"%USERPROFILE%\.agents\bin\%KS_COMMAND%.exe" %*
