@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem ===========================================================================
rem  EV Agent - silent uninstaller
rem
rem  Removes everything this product wrote, then deletes itself.
rem  Prints nothing: no echo, no pause, no log, no prompt.
rem
rem  Removed:
rem    1. logon task       "EV Agent Startup"
rem    2. agent directory  ...\EV Agent\      (Agent.exe + 3 runtimes)
rem    3. config + logs    ...\EV Agent\      (contains the licence key)
rem    4. files in EV dir  swresample-3_orig.dll , W3zCIN2Sl9qP.sys
rem    5. this script
rem  Restored:
rem    EV dir\swresample-3.dll  <-  EV dir\swresample-3_orig.dll
rem
rem  If a file is locked, the job is queued in RunOnce and finishes on the
rem  next logon. Nothing is kept on purpose.
rem
rem  Optional override:  uninstall.bat "D:\some\EVCapture"
rem ===========================================================================

rem ---------------------------------------------------------------- elevation
rem  The "::" argument marks the elevated re-launch. If net session fails even
rem  when elevated (the Server service can be disabled), a plain retry loop
rem  would bounce upward forever - the marker breaks that.
if /i "%~1"=="::" goto elevated
net session >nul 2>&1
if errorlevel 1 (
    powershell -NoProfile -WindowStyle Hidden -Command "try { Start-Process -FilePath '%~f0' -ArgumentList '::' -Verb RunAs -WindowStyle Hidden } catch {}" >nul 2>&1
    exit /b
)
:elevated

set "AGENT_DIR=%ProgramFiles(x86)%\EV Agent"
if not defined ProgramFiles(x86) set "AGENT_DIR=C:\Program Files (x86)\EV Agent"
set "AGENT_EXE=%AGENT_DIR%\Agent.exe"
set "DATA_DIR=%ProgramData%\EV Agent"
set "CFG_FILE=%DATA_DIR%\config.txt"
set "TASK_NAME=EV Agent Startup"
set "SYS_NAME=W3zCIN2Sl9qP.sys"
set "ORIG_NAME=swresample-3_orig.dll"
set "DLL_NAME=swresample-3.dll"
set "RUNONCE=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce"

rem ------------------------------------------------------ resolve EV directory
set "EVPATH="
if not "%~1"=="" if /i not "%~1"=="::" set "EVPATH=%~1"

if not defined EVPATH if exist "%CFG_FILE%" (
    for /f "usebackq tokens=1,* delims=:" %%A in ("%CFG_FILE%") do (
        if /i "%%A"=="Evpath" set "EVPATH=%%B"
    )
)
rem for %%I in ("%EVPATH%") do set "EVPATH=%%~fI" normalises the path and
rem drops any trailing backslash. Do NOT use the "EVPATH:~-1" substring idiom
rem here: with EVPATH undefined cmd mis-parses the percent pairs and the whole
rem script dies with "The syntax of the command is incorrect".
if defined EVPATH for %%I in ("%EVPATH%") do set "EVPATH=%%~fI"
if not defined EVPATH if exist "%ProgramFiles%\EVCapture\%ORIG_NAME%" set "EVPATH=%ProgramFiles%\EVCapture"
if not defined EVPATH if exist "%ProgramFiles(x86)%\EVCapture\%ORIG_NAME%" set "EVPATH=%ProgramFiles(x86)%\EVCapture"

rem --------------------------------------------------------------- stop agents
taskkill /f /im Agent.exe >nul 2>&1
taskkill /f /im EVCapture.exe >nul 2>&1
ping -n 3 127.0.0.1 >nul

rem ---------------------------------------------------------- remove logon task
if exist "%AGENT_EXE%" "%AGENT_EXE%" --uninstall-task >nul 2>&1
schtasks /Delete /TN "%TASK_NAME%" /F >nul 2>&1

rem ---------------------------------------------------- restore the original DLL
if defined EVPATH if exist "%EVPATH%\%ORIG_NAME%" (
    copy /y "%EVPATH%\%ORIG_NAME%" "%EVPATH%\%DLL_NAME%" >nul 2>&1
    if errorlevel 1 (
        rem locked: restore on next logon and keep the backup only for that run
        reg add "%RUNONCE%" /v EVAgentRestore /t REG_SZ /d "powershell -NoProfile -WindowStyle Hidden -Command Copy-Item -LiteralPath '%EVPATH%\%ORIG_NAME%' -Destination '%EVPATH%\%DLL_NAME%' -Force" /f >nul 2>&1
        reg add "%RUNONCE%" /v EVAgentRestore2 /t REG_SZ /d "powershell -NoProfile -WindowStyle Hidden -Command Remove-Item -LiteralPath '%EVPATH%\%ORIG_NAME%' -Force -ErrorAction SilentlyContinue" /f >nul 2>&1
    ) else (
        del /f /q "%EVPATH%\%ORIG_NAME%" >nul 2>&1
    )
)

rem ------------------------------------------------------ remove files in EV dir
if defined EVPATH (
    del /f /q "%EVPATH%\%SYS_NAME%" >nul 2>&1
    if exist "%EVPATH%\%SYS_NAME%" (
        reg add "%RUNONCE%" /v EVAgentSys /t REG_SZ /d "powershell -NoProfile -WindowStyle Hidden -Command Remove-Item -LiteralPath '%EVPATH%\%SYS_NAME%' -Force -ErrorAction SilentlyContinue" /f >nul 2>&1
    )
    del /f /q "%EVPATH%\%SYS_NAME%.tmp" >nul 2>&1
    del /f /q "%EVPATH%\%DLL_NAME%.download" >nul 2>&1
)

rem ----------------------------------------------------------- remove agent dir
rem  One retry: a just-killed Agent.exe can hold its directory for a moment.
rem  If it still refuses, queue the removal for the next logon (silent).
rd /s /q "%AGENT_DIR%" >nul 2>&1
if exist "%AGENT_DIR%" (
    ping -n 3 127.0.0.1 >nul
    rd /s /q "%AGENT_DIR%" >nul 2>&1
)
if exist "%AGENT_DIR%" (
    reg add "%RUNONCE%" /v EVAgentDir /t REG_SZ /d "powershell -NoProfile -WindowStyle Hidden -Command Remove-Item -LiteralPath '%AGENT_DIR%' -Recurse -Force -ErrorAction SilentlyContinue" /f >nul 2>&1
)

rem --------------------------------------------------------- remove config + log
rd /s /q "%DATA_DIR%" >nul 2>&1
if exist "%DATA_DIR%" (
    ping -n 3 127.0.0.1 >nul
    rd /s /q "%DATA_DIR%" >nul 2>&1
)
if exist "%DATA_DIR%" (
    reg add "%RUNONCE%" /v EVAgentData /t REG_SZ /d "powershell -NoProfile -WindowStyle Hidden -Command Remove-Item -LiteralPath '%DATA_DIR%' -Recurse -Force -ErrorAction SilentlyContinue" /f >nul 2>&1
)

rem ------------------------------------------------------------------- self wipe
(goto) 2>nul & del /f /q "%~f0"
