@echo off
setlocal EnableExtensions
title LatticeSearch User Installer

set "SOURCE_DIR=%~dp0.."
for %%I in ("%SOURCE_DIR%") do set "SOURCE_DIR=%%~fI"
if not defined LS_INSTALL_DIR set "LS_INSTALL_DIR=%LOCALAPPDATA%\LatticeSearch"
if not defined LS_RIGHTCLICK set "LS_RIGHTCLICK=1"
if not defined LS_AUTOSTART set "LS_AUTOSTART=0"
if not defined LS_DESKTOP set "LS_DESKTOP=1"
if not defined LS_ACCESS_MODE set "LS_ACCESS_MODE=personal"
if not defined LS_ALLOW_FOLDERS set "LS_ALLOW_FOLDERS="

echo LatticeSearch user install
echo Source: "%SOURCE_DIR%"
echo Target: "%LS_INSTALL_DIR%"
echo.

mkdir "%LS_INSTALL_DIR%" 2>nul
if errorlevel 1 (
  echo ERROR: Could not create install directory.
  exit /b 1
)

echo Copying application files...
REM NOTE: do NOT exclude *.pyc — in the protected build the .pyc/.pyd ARE the shipped modules.
robocopy "%SOURCE_DIR%" "%LS_INSTALL_DIR%" /E /XD ".git" ".agents" ".codex" "__pycache__" >nul
if errorlevel 8 (
  echo ERROR: File copy failed. Robocopy exit code %ERRORLEVEL%.
  exit /b 1
)

echo Checking for Python (installing only if missing)...
powershell -NoProfile -ExecutionPolicy Bypass -File "%LS_INSTALL_DIR%\installer\bootstrap_python.ps1" -InstallDir "%LS_INSTALL_DIR%"
if errorlevel 1 (
  echo ERROR: Python setup did not complete. See the message above.
  exit /b 1
)

set "DATA_DIR=%LS_INSTALL_DIR%\data"
mkdir "%DATA_DIR%" 2>nul

echo Writing access configuration...
powershell -NoProfile -ExecutionPolicy Bypass -File "%LS_INSTALL_DIR%\installer\write_access.ps1" -DataDir "%DATA_DIR%" -Mode "%LS_ACCESS_MODE%" -Allow "%LS_ALLOW_FOLDERS%"
if errorlevel 1 exit /b 1

echo Creating launcher...
(
  echo @echo off
  echo title LatticeSearch
  echo cd /d "%%~dp0"
  echo set "LATTICE_HOME=%%~dp0data"
  echo set "KMP_DUPLICATE_LIB_OK=TRUE"
  echo set "PYTHONIOENCODING=utf-8"
  echo set "LS_ENTRY=server.py"
  echo if exist "%%~dp0server.pyc" set "LS_ENTRY=server.pyc"
  echo start "" "http://127.0.0.1:8765"
  echo where py ^>nul 2^>nul
  echo if not errorlevel 1 ^(
  echo   py -3 %%LS_ENTRY%% --port 8765
  echo ^) else ^(
  echo   python %%LS_ENTRY%% --port 8765
  echo ^)
  echo pause
) > "%LS_INSTALL_DIR%\LatticeSearch-Launch.bat"

set "STARTMENU=%APPDATA%\Microsoft\Windows\Start Menu\Programs\LatticeSearch"
mkdir "%STARTMENU%" 2>nul
echo Creating shortcuts...
powershell -NoProfile -ExecutionPolicy Bypass -Command "$ws=New-Object -ComObject WScript.Shell; $s=$ws.CreateShortcut((Join-Path $env:STARTMENU 'LatticeSearch.lnk')); $s.TargetPath=(Join-Path $env:LS_INSTALL_DIR 'LatticeSearch-Launch.bat'); $s.WorkingDirectory=$env:LS_INSTALL_DIR; $s.Save(); if($env:LS_DESKTOP -eq '1'){ $d=[Environment]::GetFolderPath('Desktop'); $x=$ws.CreateShortcut((Join-Path $d 'LatticeSearch.lnk')); $x.TargetPath=(Join-Path $env:LS_INSTALL_DIR 'LatticeSearch-Launch.bat'); $x.WorkingDirectory=$env:LS_INSTALL_DIR; $x.Save() }"

if "%LS_RIGHTCLICK%"=="1" (
  echo Installing per-user right-click menu integration...
  pushd "%LS_INSTALL_DIR%"
  where py >nul 2>nul
  if not errorlevel 1 (
    py -3 integrate_windows.py install
  ) else (
    python integrate_windows.py install
  )
  popd
)

if "%LS_AUTOSTART%"=="1" (
  echo Adding startup entry...
  reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Run" /v LatticeSearch /t REG_SZ /d "\"%LS_INSTALL_DIR%\LatticeSearch-Launch.bat\"" /f >nul
) else (
  reg delete "HKCU\Software\Microsoft\Windows\CurrentVersion\Run" /v LatticeSearch /f >nul 2>nul
)

echo.
echo Installed LatticeSearch for the current user.
exit /b 0
