@echo off
REM =====================================================================
REM  kcpp / ICPP installer  (32-bit / x86)
REM
REM  Deploys the kcpp compiler toolchain and the ICPP interpreter, and
REM  optionally puts them on your PATH.
REM
REM     <home>\bin\        kcpp.exe, icpp.exe, klink.exe, khex16.exe,
REM                        kice.exe, isqldll.dll
REM     <home>\lib\        ICPP libraries (stringutil.icpp)
REM     <home>\doc\        ICPP_USER_MANUAL.md, builtin list
REM     <home>\examples\   language / html / llm / fpga samples
REM
REM  Usage:  install.bat [target-home-dir] [options]
REM            /nopath    do not touch PATH
REM            /system    register on the SYSTEM PATH (needs admin)
REM                       default is the per-user PATH (no admin needed)
REM =====================================================================
setlocal EnableExtensions EnableDelayedExpansion

set "SRC=%~dp0"
if "%SRC:~-1%"=="\" set "SRC=%SRC:~0,-1%"

set "DEST="
set "PATHMODE=user"
for %%A in (%*) do (
  if /i "%%~A"=="/nopath"  ( set "PATHMODE=none"
  ) else if /i "%%~A"=="/system" ( set "PATHMODE=machine"
  ) else if not defined DEST ( set "DEST=%%~A" )
)

if not defined DEST (
  echo.
  echo  Enter the directory to install kcpp / ICPP into.
  set /p "DEST=  Install directory [C:\kcpp]: "
)
if not defined DEST set "DEST=C:\kcpp"
set "DEST=%DEST:"=%"
if "%DEST:~-1%"=="\" set "DEST=%DEST:~0,-1%"

echo.
echo  Installing kcpp / ICPP ^(x86^)
echo    from : %SRC%
echo    to   : %DEST%
echo.

for %%D in ("" "\bin" "\lib" "\doc" "\examples") do (
  if not exist "%DEST%%%~D" mkdir "%DEST%%%~D" 2>nul
)
if not exist "%DEST%\bin" (
  echo  ERROR: could not create "%DEST%". Check the path and your permissions.
  exit /b 1
)

copy /y "%SRC%\bin\*"   "%DEST%\bin\"  >nul
copy /y "%SRC%\lib\*"   "%DEST%\lib\"  >nul
copy /y "%SRC%\doc\*"   "%DEST%\doc\"  >nul
xcopy /e /i /y /q "%SRC%\examples" "%DEST%\examples" >nul
copy /y "%SRC%\LICENSE"        "%DEST%\" >nul 2>&1
copy /y "%SRC%\NOTICE"         "%DEST%\" >nul 2>&1
copy /y "%SRC%\THIRD_PARTY.md" "%DEST%\" >nul 2>&1
copy /y "%SRC%\README.txt"     "%DEST%\" >nul 2>&1

echo  Files copied.

REM --- PATH registration -----------------------------------------------
REM  Deliberately NOT "setx PATH %PATH%;..." - %PATH% is the MERGED user+
REM  system value, so that copies every system entry into the user PATH,
REM  and setx truncates at 1024 characters. This reads the raw scoped
REM  value, appends only if absent, and writes it back via .NET, which has
REM  no length limit and broadcasts the change.
if "%PATHMODE%"=="none" (
  echo  PATH not modified ^(/nopath^).
  goto :done
)
set "SCOPE=User"
if "%PATHMODE%"=="machine" set "SCOPE=Machine"
if "%PATHMODE%"=="machine" (
  net session >nul 2>&1
  if errorlevel 1 (
    echo.
    echo  /system needs an elevated prompt. Re-run as Administrator,
    echo  or drop /system to register on the per-user PATH instead.
    goto :done
  )
)
powershell -NoProfile -ExecutionPolicy Bypass -Command ^
  "$s='%SCOPE%'; $d='%DEST%\bin';" ^
  "$cur=[Environment]::GetEnvironmentVariable('PATH',$s); if($null -eq $cur){$cur=''};" ^
  "$parts=$cur.Split(';') | Where-Object { $_ -ne '' };" ^
  "if($parts -contains $d){ Write-Host ('  PATH already contains '+$d) }" ^
  "else { $new=(($parts+$d) -join ';'); [Environment]::SetEnvironmentVariable('PATH',$new,$s); Write-Host ('  Added to '+$s+' PATH: '+$d) }"
if errorlevel 1 (
  echo  Could not update PATH automatically. Add this directory yourself:
  echo      %DEST%\bin
)

:done
echo.
echo  Try it:
echo      "%DEST%\bin\icpp.exe" "%DEST%\examples\language\01_core_language.icpp"
echo.
echo  Open a NEW command prompt for the PATH change to take effect.
echo  See "%DEST%\doc\ICPP_USER_MANUAL.md" for the full guide.
echo  Done.
endlocal
