@echo off
setlocal EnableExtensions
title Udalenka27 - Agent and Router setup

:: Request administrator rights.
fltmc >nul 2>&1
if errorlevel 1 (
  echo Administrator rights are required. Windows will ask for permission...
  powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "Start-Process -FilePath '%~f0' -Verb RunAs"
  exit /b
)

set "BASE=%~dp0"
set "LOCAL_AGENT=%BASE%Udalenka27_Agent_1C.exe"
set "LOCAL_ROUTER=%BASE%Udalenka27_Router.exe"
set "WORK=%ProgramData%\Udalenka27\Setup"
set "AGENT=%WORK%\Udalenka27_Agent_1C.exe"
set "ROUTER=%WORK%\Udalenka27_Router.exe"
set "DOWNLOAD_BASE=https://udalenka27.ru/downloads"
set "AGENT_URL=%DOWNLOAD_BASE%/Udalenka27_Agent_1C.exe"
set "ROUTER_URL=%DOWNLOAD_BASE%/Udalenka27_Router.exe"
set "ROUTER_DIR=%ProgramFiles%\Udalenka27\Router"
set "ROUTER_TARGET=%ROUTER_DIR%\Udalenka27 Router.exe"
set "LOG_DIR=%ProgramData%\Udalenka27"
set "LOG=%LOG_DIR%\install-agent-router.log"

if not exist "%LOG_DIR%" mkdir "%LOG_DIR%" >nul 2>&1
if not exist "%WORK%" mkdir "%WORK%" >nul 2>&1
echo.>>"%LOG%"
echo [%date% %time%] Setup started>>"%LOG%"

echo.
echo ================================================
echo   Udalenka27 Agent + Router
echo ================================================
echo.

if exist "%LOCAL_AGENT%" (
  echo [1/4] Using local Agent installer...
  copy /Y "%LOCAL_AGENT%" "%AGENT%" >nul
) else (
  echo [1/4] Downloading Udalenka27 Agent...
  call :DOWNLOAD "%AGENT_URL%" "%AGENT%"
  if errorlevel 1 goto :DOWNLOAD_FAIL
)
if not exist "%AGENT%" goto :FAIL

if exist "%LOCAL_ROUTER%" (
  echo [2/4] Using local Router file...
  copy /Y "%LOCAL_ROUTER%" "%ROUTER%" >nul
) else (
  echo [2/4] Downloading Udalenka27 Router...
  call :DOWNLOAD "%ROUTER_URL%" "%ROUTER%"
  if errorlevel 1 goto :DOWNLOAD_FAIL
)
if not exist "%ROUTER%" goto :FAIL

for %%F in ("%AGENT%") do if %%~zF LSS 100000 goto :FAIL
for %%F in ("%ROUTER%") do if %%~zF LSS 100000 goto :FAIL

echo [3/4] Installing Udalenka27 Agent...
"%AGENT%" -fullinstall >>"%LOG%" 2>&1
if errorlevel 1 (
  echo ERROR: Agent installation failed. Check "%LOG%".
  goto :FAIL
)
echo       Done.

echo [4/4] Registering Udalenka27 Router...
if not exist "%ROUTER_DIR%" mkdir "%ROUTER_DIR%" >nul 2>&1
copy /Y "%ROUTER%" "%ROUTER_TARGET%" >nul
if errorlevel 1 goto :FAIL
"%ROUTER_TARGET%" -install >>"%LOG%" 2>&1
if errorlevel 1 (
  echo ERROR: Router registration failed. Check "%LOG%".
  goto :FAIL
)
echo       Done.

echo [%date% %time%] Setup completed successfully>>"%LOG%"
echo.
echo ================================================
echo   INSTALLATION COMPLETED
echo ================================================
echo.
echo Agent is installed as a Windows service.
echo Router is registered for MeshCentral connections.
echo No desktop shortcut is created.
echo Log: %LOG%
echo.
pause
exit /b 0

:DOWNLOAD
set "URL=%~1"
set "OUT=%~2"
del /Q "%OUT%" >nul 2>&1
powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "$ProgressPreference='SilentlyContinue'; [Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12; Invoke-WebRequest -UseBasicParsing -Uri '%URL%' -OutFile '%OUT%'" >>"%LOG%" 2>&1
if exist "%OUT%" exit /b 0
where curl.exe >nul 2>&1
if errorlevel 1 exit /b 1
curl.exe -L --fail --silent --show-error "%URL%" -o "%OUT%" >>"%LOG%" 2>&1
if exist "%OUT%" exit /b 0
exit /b 1

:DOWNLOAD_FAIL
echo ERROR: Could not download the required files. Check Internet access.
goto :FAIL

:FAIL
echo.
echo Installation was not completed. Log: %LOG%
echo.
pause
exit /b 1
