@echo off
setlocal enabledelayedexpansion

echo.
echo ========================================
echo    Process Monitor - Quick Install
echo ========================================
echo.

set INSTALL_DIR=%LOCALAPPDATA%\ProcessMonitor
set BIN_DIR=%INSTALL_DIR%\bin

echo [1/4] Killing old process...
taskkill /IM ProcessMonitor.exe /F >nul 2>&1

echo.
echo [2/4] Installing...
if not exist "!INSTALL_DIR!" mkdir "!INSTALL_DIR!"
if not exist "!BIN_DIR!" mkdir "!BIN_DIR!"

cd /d "%~dp0"

if not exist "ProcessMonitor.exe" (
    echo ERROR: ProcessMonitor.exe not found!
    pause
    exit /b 1
)

copy /Y ProcessMonitor.exe "!BIN_DIR!\" >nul
copy /Y config.json "!INSTALL_DIR!\" >nul
copy /Y start.vbs "!INSTALL_DIR!\" >nul
echo Install OK

echo.
echo [3/4] Configuring auto-start...

echo @echo off > "!INSTALL_DIR!\stop.bat"
echo taskkill /IM ProcessMonitor.exe /F ^>nul 2^>^&1 >> "!INSTALL_DIR!\stop.bat"
echo echo Stopped. >> "!INSTALL_DIR!\stop.bat"

reg delete "HKCU\Software\Microsoft\Windows\CurrentVersion\Run" /v "ProcessMonitor" /f >nul 2>&1
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Run" /v "ProcessMonitor" /t REG_SZ /d "wscript.exe \"!INSTALL_DIR!\start.vbs\"" /f >nul 2>&1

schtasks /delete /tn "ProcessMonitor" /f >nul 2>&1

echo Config OK

echo.
echo [4/4] Starting...
wscript.exe "!INSTALL_DIR!\start.vbs"

timeout /t 2 /nobreak >nul
tasklist /FI "IMAGENAME eq ProcessMonitor.exe" 2>nul | find /I "ProcessMonitor.exe" >nul
if !errorlevel! equ 0 (
    echo Process is running OK
) else (
    echo WARNING: Process may not have started
    echo Check: !BIN_DIR!\monitor.log
)

echo.
echo ========================================
echo           Install Complete!
echo ========================================
echo.
echo   Dir:  !INSTALL_DIR!
echo   Log:  !BIN_DIR!\monitor.log
echo   Stop: !INSTALL_DIR!\stop.bat
echo.
echo   Web:  https://app.onbeat.com.cn/moservice/
echo   Code: gogo3567489
echo.
echo ========================================
pause
