@echo off
REM Depto de Engenharia e Automacao - Master Automacao - Autor: Derlivon Silva
REM Registra tarefa do Windows que roda sentinel.php a cada 1 hora.
REM Execute UMA VEZ no servidor 24h, depois esquece.

setlocal
cd /d "%~dp0"

net session >nul 2>&1
if %errorlevel% neq 0 (
    powershell -NoProfile -Command "Start-Process cmd -ArgumentList '/c','\"%~f0\"' -Verb RunAs"
    exit /b
)

REM Onde estao os arquivos do painel
set "PAINEL=%~dp0"
REM Tira a barra final
if "%PAINEL:~-1%"=="\" set "PAINEL=%PAINEL:~0,-1%"

REM Acha o PHP — tenta caminhos comuns
set "PHP="
if exist "C:\Fire-Updates\php\php.exe" set "PHP=C:\Fire-Updates\php\php.exe"
if not defined PHP if exist "C:\PHP\php.exe" set "PHP=C:\PHP\php.exe"
if not defined PHP if exist "C:\Apache24\php\php.exe" set "PHP=C:\Apache24\php\php.exe"
if not defined PHP if exist "C:\Apache24_ox\php\php.exe" set "PHP=C:\Apache24_ox\php\php.exe"
if not defined PHP if exist "C:\xampp\php\php.exe" set "PHP=C:\xampp\php\php.exe"

if not defined PHP (
    echo ERRO: php.exe nao encontrado. Edite este .cmd e defina o caminho na variavel PHP=
    pause
    exit /b 1
)

echo PHP detectado: %PHP%
echo Painel detectado: %PAINEL%
echo.

REM Cria a tarefa — roda a cada 1 hora, como SYSTEM
schtasks /Create /F /SC HOURLY /MO 1 /RU SYSTEM /TN PainelBackup-Sentinel /TR "\"%PHP%\" \"%PAINEL%\sentinel.php\""

if %errorlevel% equ 0 (
    echo.
    echo OK: Tarefa PainelBackup-Sentinel criada. Vai rodar a cada 1 hora.
    echo.
    echo Conferir status:    schtasks /Query /TN PainelBackup-Sentinel /V
    echo Executar agora:     schtasks /Run /TN PainelBackup-Sentinel
    echo Apagar tarefa:      schtasks /Delete /TN PainelBackup-Sentinel /F
    echo.
    echo Antes de funcionar, copie emails-exemplo.php pra emails.php e configure o SMTP.
) else (
    echo ERRO ao criar tarefa.
)

echo.
pause
