@echo off
setlocal EnableExtensions
title LatticeSearch Uninstaller

if not defined LS_INSTALL_DIR (
  if exist "%LOCALAPPDATA%\LatticeSearch" (
    set "LS_INSTALL_DIR=%LOCALAPPDATA%\LatticeSearch"
  ) else (
    set "LS_INSTALL_DIR=%ProgramFiles%\LatticeSearch"
  )
)

echo LatticeSearch uninstall
echo Install directory: "%LS_INSTALL_DIR%"
echo.

echo Removing right-click integration...
if exist "%LS_INSTALL_DIR%\integrate_windows.py" (
  pushd "%LS_INSTALL_DIR%"
  where py >nul 2>nul
  if not errorlevel 1 (
    py -3 integrate_windows.py uninstall
  ) else (
    python integrate_windows.py uninstall
  )
  popd
) else if exist "%~dp0..\integrate_windows.py" (
  pushd "%~dp0.."
  python integrate_windows.py uninstall
  popd
)

echo Removing startup entries...
reg delete "HKCU\Software\Microsoft\Windows\CurrentVersion\Run" /v LatticeSearch /f >nul 2>nul
net session >nul 2>nul
if "%ERRORLEVEL%"=="0" reg delete "HKLM\Software\Microsoft\Windows\CurrentVersion\Run" /v LatticeSearch /f >nul 2>nul

echo Removing shortcuts...
del "%APPDATA%\Microsoft\Windows\Start Menu\Programs\LatticeSearch\LatticeSearch.lnk" >nul 2>nul
rd "%APPDATA%\Microsoft\Windows\Start Menu\Programs\LatticeSearch" >nul 2>nul
del "%USERPROFILE%\Desktop\LatticeSearch.lnk" >nul 2>nul
if "%ERRORLEVEL%"=="0" (
  del "%ProgramData%\Microsoft\Windows\Start Menu\Programs\LatticeSearch\LatticeSearch.lnk" >nul 2>nul
  rd "%ProgramData%\Microsoft\Windows\Start Menu\Programs\LatticeSearch" >nul 2>nul
  del "%PUBLIC%\Desktop\LatticeSearch.lnk" >nul 2>nul
)

echo.
choice /C YN /N /M "Remove installed program files from '%LS_INSTALL_DIR%'? Data is kept unless it is inside this directory. [Y/N] "
if errorlevel 2 goto done
if exist "%LS_INSTALL_DIR%" (
  choice /C YN /N /M "Confirm deletion of installed files. This cannot be undone. [Y/N] "
  if errorlevel 2 goto done
  if /I "%LS_INSTALL_DIR%"=="%LOCALAPPDATA%\LatticeSearch" (
    rmdir /S /Q "%LS_INSTALL_DIR%"
  ) else if /I "%LS_INSTALL_DIR%"=="%ProgramFiles%\LatticeSearch" (
    rmdir /S /Q "%LS_INSTALL_DIR%"
  ) else (
    echo Refusing to delete a custom path automatically: "%LS_INSTALL_DIR%"
  )
)

:done
echo Uninstall complete.
exit /b 0
