@echo off
REM ============================================================
REM  Limpiador Datex Remoto  (DESINSTALACION COMPLETA Y ROBUSTA)
REM  Se AUTOELEVA: doble clic y pulsa "Si" en el aviso de Windows.
REM ============================================================

REM --- Autoelevar a administrador si no lo somos ---
net session >nul 2>&1
if %errorlevel% neq 0 (
  echo Solicitando permisos de administrador...
  powershell -Command "Start-Process -Verb RunAs -FilePath '%~f0'" >nul 2>&1
  exit /b
)

chcp 65001 >nul
setlocal enabledelayedexpansion
echo.
echo ===== LIMPIADOR DATEX REMOTO (administrador) =====
echo.

REM --- Localizar la instalacion por el registro (si existe) ---
set "INSTLOC="
for /f "tokens=2,*" %%a in ('reg query "HKLM\Software\DatexRemoto\InstallState\DatexRemoto" /v InstallLocation 2^>nul ^| find "InstallLocation"') do set "INSTLOC=%%b"
if not defined INSTLOC for /f "tokens=2,*" %%a in ('reg query "HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall\{54E86BC2-6C85-41F3-A9EB-1A94AC9B1F93}_is1" /v InstallLocation 2^>nul ^| find "InstallLocation"') do set "INSTLOC=%%b"

echo [1/8] Desinstalacion nativa de la app (si esta instalada)...
if defined INSTLOC if exist "!INSTLOC!\DatexRemoto.exe" "!INSTLOC!\DatexRemoto.exe" --uninstall >nul 2>&1
if exist "%ProgramFiles%\DatexRemoto\DatexRemoto.exe" "%ProgramFiles%\DatexRemoto\DatexRemoto.exe" --uninstall >nul 2>&1

echo [2/8] Deteniendo y quitando servicios...
sc stop   DatexRemoto      >nul 2>&1
sc delete DatexRemoto      >nul 2>&1
sc stop   DATEXINSTALADOR  >nul 2>&1
sc delete DATEXINSTALADOR  >nul 2>&1

echo [3/8] Cerrando procesos (app, bandeja, servidor)...
taskkill /F /IM DatexRemoto.exe >nul 2>&1
taskkill /F /IM rustdesk.exe    >nul 2>&1

echo [4/8] Esperando a que se liberen los archivos...
ping -n 5 127.0.0.1 >nul

echo [5/8] Borrando el programa (instalado y portable)...
if defined INSTLOC rd /s /q "!INSTLOC!"        >nul 2>&1
rd /s /q "%ProgramFiles%\DatexRemoto"      >nul 2>&1
rd /s /q "%ProgramFiles(x86)%\DatexRemoto" >nul 2>&1
rd /s /q "%LOCALAPPDATA%\datexremoto"      >nul 2>&1

echo [6/8] Borrando configuracion antigua...
rd /s /q "%APPDATA%\DatexRemoto"     >nul 2>&1
rd /s /q "%APPDATA%\RustDesk"        >nul 2>&1
rd /s /q "%APPDATA%\DATEXINSTALADOR" >nul 2>&1
rd /s /q "%windir%\System32\config\systemprofile\AppData\Roaming\DatexRemoto" >nul 2>&1

echo [7/8] Borrando accesos directos y registro...
del /f /q "%PUBLIC%\Desktop\DatexRemoto.lnk"      >nul 2>&1
del /f /q "%USERPROFILE%\Desktop\DatexRemoto.lnk" >nul 2>&1
rd  /s /q "%ProgramData%\Microsoft\Windows\Start Menu\Programs\DatexRemoto" >nul 2>&1
del /f /q "%ProgramData%\Microsoft\Windows\Start Menu\Programs\Startup\DatexRemoto Tray.lnk" >nul 2>&1
reg delete "HKLM\Software\DatexRemoto" /f >nul 2>&1
reg delete "HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall\DatexRemoto" /f >nul 2>&1
reg delete "HKLM\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\DatexRemoto" /f >nul 2>&1
reg delete "HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall\{54E86BC2-6C85-41F3-A9EB-1A94AC9B1F93}_is1" /f >nul 2>&1
reg delete "HKLM\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{54E86BC2-6C85-41F3-A9EB-1A94AC9B1F93}_is1" /f >nul 2>&1
reg delete "HKCR\.datexremoto" /f >nul 2>&1
reg delete "HKCR\datexremoto"  /f >nul 2>&1

echo [8/8] Comprobacion final:
set "QUEDA="
if exist "%ProgramFiles%\DatexRemoto\DatexRemoto.exe" ( set "QUEDA=1" & echo    - AUN existe: "%ProgramFiles%\DatexRemoto" )
if defined INSTLOC if exist "!INSTLOC!\DatexRemoto.exe" ( set "QUEDA=1" & echo    - AUN existe: "!INSTLOC!" )
if exist "%LOCALAPPDATA%\datexremoto\DatexRemoto.exe" ( set "QUEDA=1" & echo    - AUN existe: "%LOCALAPPDATA%\datexremoto" )
echo.
echo ============================================================
if defined QUEDA (
  echo   *** NO se pudo borrar todo: habia archivos EN USO. ***
  echo   REINICIA el equipo y vuelve a ejecutar este limpiador UNA vez.
) else (
  echo   EQUIPO LIMPIO. Ahora abre DatexRemoto.exe y pulsa INSTALAR.
)
echo ============================================================
echo.
pause
