@echo off
REM =====================================================================
REM  ISQL SDK installer  (64-bit / x64)
REM
REM  Deploys the ISQL embedded-database SDK to a target home directory,
REM  creates the database directory tree, and optionally puts the tools
REM  on your PATH.
REM
REM     <home>\db\          EMPTY - the engine creates the catalog and
REM                         system tables here on first connection
REM     <home>\bin\         isqldll.dll, isql_shell.exe
REM     <home>\lib\         isqldll.lib (import), isqllib.lib (static)
REM     <home>\include\h\   isql.h  <- include this one
REM     <home>\include\     newpwd.*, vlr.h, vlrengine.h
REM     <home>\examples\    isql_example.c, isql_shell.c, build_*.bat
REM     <home>\doc\         HOWTO_BUILD_C_APP.md, VLR_*.txt
REM     <home>\isql.cfg     engine configuration
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 ISQL home directory to create.
  echo  ^(The database will live in ^<home^>\db, which the engine fills on first run.^)
  set /p "DEST=  Home directory [C:\ISQL]: "
)
if not defined DEST set "DEST=C:\ISQL"
REM strip any quotes the user typed, and a trailing backslash
set "DEST=%DEST:"=%"
if "%DEST:~-1%"=="\" set "DEST=%DEST:~0,-1%"

echo.
echo  Installing ISQL SDK ^(x64^)
echo    from : %SRC%
echo    to   : %DEST%
echo.

REM --- create the directory tree (db is intentionally left EMPTY) ---
for %%D in ("" "\db" "\bin" "\lib" "\include" "\include\h" "\examples" "\doc") 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
)

REM --- copy payload into place ---
copy /y "%SRC%\bin\*"           "%DEST%\bin\"       >nul
copy /y "%SRC%\lib\*"           "%DEST%\lib\"       >nul
copy /y "%SRC%\include\h\*"     "%DEST%\include\h\" >nul
copy /y "%SRC%\include\*.c"     "%DEST%\include\"   >nul 2>&1
copy /y "%SRC%\include\*.h"     "%DEST%\include\"   >nul 2>&1
copy /y "%SRC%\examples\*"      "%DEST%\examples\"  >nul
copy /y "%SRC%\doc\*"           "%DEST%\doc\"       >nul
copy /y "%SRC%\db\*"            "%DEST%\db\"        >nul 2>&1
copy /y "%SRC%\isql.cfg"        "%DEST%\"           >nul 2>&1
copy /y "%SRC%\README.txt"      "%DEST%\"           >nul 2>&1

REM --- the DLL must sit beside any executable that loads it ---
copy /y "%DEST%\bin\isqldll.dll" "%DEST%\examples\" >nul 2>&1

echo  Directory layout created and 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 (so re-running is safe), and writes it
REM  back via .NET, which has 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  Database directory "%DEST%\db" is empty; the engine creates the
echo  catalog and system tables there on the first connection.
echo.
echo  Next steps:
echo    1. Build and run the self-checking example:
echo         "%DEST%\examples\build_example.bat"
echo         "%DEST%\examples\isql_example.exe"  "%DEST%"
echo    2. Try the interactive shell:
echo         "%DEST%\examples\build_shell.bat"
echo         "%DEST%\bin\isql_shell.exe"  "%DEST%"
echo.
echo  Open a NEW command prompt for the PATH change to take effect.
echo  See "%DEST%\doc\HOWTO_BUILD_C_APP.md" for the full guide.
echo  Done.
endlocal
