@echo off
setlocal EnableExtensions
cd /d "%~dp0"
title EpAgent Uninstall
echo ============================================
echo  EpAgent uninstall  (full clean)
echo  - Stop / remove scheduled task EpAgent
echo  - Kill processes under ProgramData\EpAgent
echo  - Delete C:\ProgramData\EpAgent
echo  - Does NOT touch system Python
echo ============================================
echo.

:: Prefer local uninstall.ps1 (oneshot zip / agent folder)
if exist "%~dp0uninstall.ps1" (
  powershell.exe -NoProfile -ExecutionPolicy Bypass -File "%~dp0uninstall.ps1"
  set ERR=%ERRORLEVEL%
  if not "%ERR%"=="0" (
    echo.
    echo Uninstall failed. code=%ERR%
    pause
    exit /b %ERR%
  )
  echo.
  echo Uninstall OK.
  timeout /t 4 >nul
  exit /b 0
)

:: Standalone download: elevate + inline PowerShell (no system Python touched)
net session >nul 2>&1
if %errorLevel% neq 0 (
  echo Requesting Administrator...
  powershell.exe -NoProfile -Command "Start-Process -LiteralPath '%~f0' -Verb RunAs"
  exit /b
)

set DIR=C:\ProgramData\EpAgent
echo Stopping scheduled task EpAgent...
schtasks /End /TN EpAgent >nul 2>&1
schtasks /Delete /TN EpAgent /F >nul 2>&1

echo Killing EpAgent processes under ProgramData\EpAgent only...
powershell.exe -NoProfile -ExecutionPolicy Bypass -Command ^
  "$ErrorActionPreference='Continue'; ^
   $needle='\ProgramData\EpAgent\'; $myPid=$PID; ^
   try { Unregister-ScheduledTask -TaskName 'EpAgent' -Confirm:$false -ErrorAction SilentlyContinue } catch {}; ^
   Get-CimInstance Win32_Process -ErrorAction SilentlyContinue | ForEach-Object { ^
     if ([int]$_.ProcessId -eq $myPid) { return }; ^
     $cl=[string]$_.CommandLine; $ex=[string]$_.ExecutablePath; $hit=$false; ^
     if ($ex -and ($ex -like ('*'+$needle+'*'))) { $hit=$true }; ^
     if ($cl -and ($cl -like ('*'+$needle+'*'))) { $hit=$true }; ^
     if ($cl -and ($cl -like '*hide_run.vbs*') -and ($cl -like '*EpAgent*')) { $hit=$true }; ^
     if ($hit) { try { Stop-Process -Id $_.ProcessId -Force -ErrorAction SilentlyContinue; Write-Host ('killed pid='+$_.ProcessId) } catch {} } ^
   }; ^
   Get-Process -Name 'EpAgent','python','pythonw','wscript','cmd' -ErrorAction SilentlyContinue | ForEach-Object { ^
     try { if ($_.Id -ne $myPid -and $_.Path -and ($_.Path -like ('*'+$needle+'*'))) { Stop-Process -Id $_.Id -Force -ErrorAction SilentlyContinue; Write-Host ('killed '+$_.Name+' pid='+$_.Id) } } catch {} ^
   }; ^
   Start-Sleep -Seconds 2"

echo Removing %DIR% (full clean)...
powershell.exe -NoProfile -ExecutionPolicy Bypass -Command ^
  "$p='C:\ProgramData\EpAgent'; $ok=$false; ^
   if (-not (Test-Path -LiteralPath $p)) { Write-Host 'already gone'; exit 0 }; ^
   for ($i=1; $i -le 6; $i++) { ^
     try { Remove-Item -LiteralPath $p -Recurse -Force -ErrorAction Stop; Write-Host 'removed OK'; $ok=$true; break } catch { Start-Sleep -Seconds 2 } ^
   }; ^
   Get-ChildItem -LiteralPath $env:ProgramData -Directory -ErrorAction SilentlyContinue | Where-Object { $_.Name -like 'EpAgent.old.*' } | ForEach-Object { try { Remove-Item -LiteralPath $_.FullName -Recurse -Force -ErrorAction SilentlyContinue } catch {} }; ^
   if (Test-Path -LiteralPath $p) { Write-Host 'FAILED: folder locked. Reboot then delete C:\ProgramData\EpAgent'; exit 1 }; ^
   exit 0"
if errorlevel 1 (
  echo.
  echo Uninstall incomplete. Reboot and delete C:\ProgramData\EpAgent manually.
  pause
  exit /b 1
)

echo.
echo Uninstall OK. System Python was not touched.
timeout /t 4 >nul
endlocal
exit /b 0
