@echo off
setlocal enabledelayedexpansion

:: ============================================================================
::  webSlinger Native Components Uninstaller
::  Copyright (c) 2025 Gradient Research LLC
::  All rights reserved.
:: ============================================================================

echo.
echo ======================================================================
echo   webSlinger Native Components Uninstaller
echo ======================================================================
echo.
echo This will remove all webSlinger native components:
echo   - taskSpinner (scheduled task and files)
echo   - keyBunker (credential bridge and files)
echo   - Bundled Python runtime
echo   - Registry entries
echo   - Start Menu shortcuts
echo.
echo Your Chrome extensions will NOT be affected.
echo.

set /p "CONFIRM=Are you sure you want to uninstall? (Y/N): "
if /I not "%CONFIRM%"=="Y" (
    echo.
    echo Uninstall cancelled.
    echo.
    pause
    exit /b 0
)

echo.
set "INSTALL_DIR=%LOCALAPPDATA%\webSlinger"

:: ----------------------------------------------------------------------------
:: Step 1: Remove scheduled task
:: ----------------------------------------------------------------------------
echo [1/5] Removing scheduled task...

schtasks /delete /tn "webSlinger_TaskSpinner" /f >nul 2>&1

if %ERRORLEVEL% EQU 0 (
    echo   Removed scheduled task: webSlinger_TaskSpinner
) else (
    echo   Scheduled task not found (already removed or never created).
)
echo.

:: ----------------------------------------------------------------------------
:: Step 2: Remove registry entries
:: ----------------------------------------------------------------------------
echo [2/5] Removing registry entries...

REG DELETE "HKCU\SOFTWARE\Google\Chrome\NativeMessagingHosts\com.webslinger.taskspinner" /f >nul 2>&1
if %ERRORLEVEL% EQU 0 (
    echo   Removed: com.webslinger.taskspinner
) else (
    echo   Registry entry com.webslinger.taskspinner not found.
)

REG DELETE "HKCU\SOFTWARE\Google\Chrome\NativeMessagingHosts\com.keybunker.helper" /f >nul 2>&1
if %ERRORLEVEL% EQU 0 (
    echo   Removed: com.keybunker.helper
) else (
    echo   Registry entry com.keybunker.helper not found.
)
echo.

:: ----------------------------------------------------------------------------
:: Step 3: Remove Start Menu entries
:: ----------------------------------------------------------------------------
echo [3/5] Removing Start Menu entries...

set "START_MENU=%APPDATA%\Microsoft\Windows\Start Menu\Programs\webSlinger"
if exist "%START_MENU%" (
    rmdir /S /Q "%START_MENU%" >nul 2>&1
    echo   Removed Start Menu folder: webSlinger
) else (
    echo   Start Menu folder not found.
)
echo.

:: ----------------------------------------------------------------------------
:: Step 4: Remove installed files
:: ----------------------------------------------------------------------------
echo [4/5] Removing installed files...

if exist "%INSTALL_DIR%" (
    rmdir /S /Q "%INSTALL_DIR%" >nul 2>&1
    if not exist "%INSTALL_DIR%" (
        echo   Removed: %INSTALL_DIR%
    ) else (
        echo   WARNING: Could not fully remove %INSTALL_DIR%
        echo   Some files may be in use. Please close Chrome and try again.
    )
) else (
    echo   Install directory not found.
)
echo.

:: ----------------------------------------------------------------------------
:: Step 5: Verify removal
:: ----------------------------------------------------------------------------
echo [5/5] Verifying removal...

set "CLEAN=1"

if exist "%INSTALL_DIR%" (
    echo   WARNING: Install directory still exists.
    set "CLEAN=0"
)

REG QUERY "HKCU\SOFTWARE\Google\Chrome\NativeMessagingHosts\com.webslinger.taskspinner" >nul 2>&1
if %ERRORLEVEL% EQU 0 (
    echo   WARNING: taskSpinner registry entry still exists.
    set "CLEAN=0"
)

REG QUERY "HKCU\SOFTWARE\Google\Chrome\NativeMessagingHosts\com.keybunker.helper" >nul 2>&1
if %ERRORLEVEL% EQU 0 (
    echo   WARNING: keyBunker registry entry still exists.
    set "CLEAN=0"
)

if "%CLEAN%"=="1" (
    echo   All components removed successfully.
)

:: ============================================================================
:: Summary
:: ============================================================================
echo.
echo ======================================================================
echo   Uninstall Complete
echo ======================================================================
echo.
echo   Restart Chrome to complete the removal of native messaging hosts.
echo.
echo ======================================================================
echo.

pause
endlocal
