@echo off
setlocal EnableExtensions
title Repair DeoDap Watcher

REM ===========================================================================
REM  ONE-CLICK REPAIR, for a fleet nobody can reach centrally.
REM
REM  The machines are spread across dozens of separate networks (89% of them
REM  share a private IP with another machine), so there is no push channel: not
REM  AD, not WinRM, not by name, not by IP. The only route to a machine whose
REM  agent is dead is the person sitting at it.
REM
REM  So this is built to be SENT — WhatsApp, email, a share — and double-clicked
REM  by a non-technical employee. It elevates itself, repairs the supervisor,
REM  and says in plain words whether it worked.
REM
REM  It repairs the SUPERVISOR, not the tracker. DeoDapWatcherService relaunches
REM  the agent within ~2 seconds of it stopping, so where the service is healthy
REM  the tracker cannot stay down. Starting the tracker by hand would fix today
REM  and nothing else.
REM
REM  It does NOT install, uninstall, or delete anything.
REM ===========================================================================

net session >nul 2>&1
if %errorlevel% neq 0 (
  echo Asking for administrator permission...
  powershell -NoProfile -Command "Start-Process -FilePath '%~f0' -Verb RunAs" >nul 2>&1
  exit /b
)

echo(
echo  ================================================
echo    DeoDap Watcher - Repair
echo  ================================================
echo(

set "APP=%ProgramFiles%\DeoDap Watcher\DeoDap Watcher.exe"
if not exist "%APP%" set "APP=%ProgramFiles(x86)%\DeoDap Watcher\DeoDap Watcher.exe"

if not exist "%APP%" (
  echo  [X] DeoDap Watcher is NOT installed on this computer.
  echo(
  echo      Please tell IT: "app not installed".
  echo      Do not install it yourself - old data on this PC may need
  echo      clearing first, or tracking will silently record nothing.
  echo(
  pause
  exit /b 2
)

echo  Found: %APP%
echo  Rebuilding the background service that keeps the tracker running...
echo(

set "SVCPS=%ProgramFiles%\DeoDap Watcher\resources\install-service.ps1"
if not exist "%SVCPS%" set "SVCPS=%ProgramFiles(x86)%\DeoDap Watcher\resources\install-service.ps1"

if exist "%SVCPS%" (
  powershell -NoProfile -ExecutionPolicy Bypass -File "%SVCPS%" -AppPath "%APP%"
) else (
  echo  [!] Service installer missing - this is an old build.
  echo      Tell IT: "needs reinstall of 1.4.16".
)

REM Make sure it is set to start on its own at every boot, then start it now.
sc config DeoDapWatcherService start= auto >nul 2>&1
sc start  DeoDapWatcherService          >nul 2>&1

echo(
echo  Waiting for the tracker to come back (up to 30 seconds)...

set "OK="
for /L %%i in (1,1,15) do (
  if not defined OK (
    timeout /t 2 /nobreak >nul
    REM Let the SERVICE start the tracker. If we started it ourselves we would
    REM prove nothing about whether this machine can recover on its own.
    tasklist /FI "IMAGENAME eq DeoDap Watcher.exe" 2>nul | find /I "DeoDap Watcher.exe" >nul && set "OK=1"
  )
)

echo(
if defined OK (
  echo  ================================================
  echo    [OK]  Fixed. Tracking is running again.
  echo          It will now restart itself automatically.
  echo  ================================================
) else (
  echo  ================================================
  echo    [!]  The service was repaired, but the tracker
  echo         did not start within 30 seconds.
  echo(
  echo         Please RESTART the computer once.
  echo         If it is still not working after that,
  echo         tell IT: "service ok, agent not starting".
  echo  ================================================
)
echo(
pause
