@echo off
REM =====================================================================
REM  ISQL SDK uninstaller - removes the PATH entry this SDK added.
REM
REM  It removes ONLY the "<home>\bin" entry it put there, leaving the
REM  rest of your PATH untouched. It does NOT delete your database:
REM  <home>\db is your data, so removing it is left to you deliberately.
REM
REM  Usage:  uninstall.bat [installed-home-dir] [/system]
REM =====================================================================
setlocal EnableExtensions

set "DEST="
set "SCOPE=User"
for %%A in (%*) do (
  if /i "%%~A"=="/system" ( set "SCOPE=Machine"
  ) else if not defined DEST ( set "DEST=%%~A" )
)

if not defined DEST (
  set /p "DEST=  Installed ISQL home directory [C:\ISQL]: "
)
if not defined DEST set "DEST=C:\ISQL"
set "DEST=%DEST:"=%"
if "%DEST:~-1%"=="\" set "DEST=%DEST:~0,-1%"

if /i "%SCOPE%"=="Machine" (
  net session >nul 2>&1
  if errorlevel 1 (
    echo  /system needs an elevated prompt. Re-run as Administrator.
    exit /b 1
  )
)

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 '' -and $_ -ne $d };" ^
  "[Environment]::SetEnvironmentVariable('PATH',($parts -join ';'),$s);" ^
  "Write-Host ('  Removed from '+$s+' PATH (if present): '+$d)"

echo.
echo  PATH entry removed. Your database at "%DEST%\db" was NOT deleted -
echo  delete the directory yourself if you no longer need the data.
echo  Open a NEW command prompt for the change to take effect.
endlocal
