@echo off
setlocal EnableExtensions
title RustDesk lanplm installer

REM ============================================================
REM  RustDesk -> rd.lanplm.ru (self-hosted OSS)
REM  Download MSI from dist.lanplm.ru (curl only),
REM  silent install, then write RustDesk2.toml directly.
REM  Run as Administrator (right click -> Run as administrator).
REM ============================================================

REM ---- admin check (no self-elevation) ----------------------
net session >nul 2>&1
if errorlevel 1 (
  echo.
  echo ERROR: run this script as Administrator
  echo        ^(right click -^> "Run as administrator"^).
  goto :end
)

REM ---- settings ---------------------------------------------
set "URL=https://dist.lanplm.ru/Programs/rustdesk-1.5.0-x86_64.msi"
set "HOST=rd.lanplm.ru"
set "RELAY=rd.lanplm.ru"
set "KEY=GAeiIknUAgnT4rk3k8y1r4nzM8KftBjpqm4PzJrXlb4="

set "MSI=%TEMP%\rustdesk-1.5.0-x86_64.msi"
set "LOG=%TEMP%\rustdesk-install.log"

echo.
echo ============================================================
echo  RustDesk -^> %HOST%
echo ============================================================

REM ---- 1. download (curl only) ------------------------------
echo [1/5] Downloading MSI from dist.lanplm.ru...
where curl >nul 2>&1
if errorlevel 1 (
  echo ERROR: curl not found ^(Windows 10 1803+ required^).
  goto :end
)
if exist "%MSI%" del /q "%MSI%"
curl -fSL --retry 3 -o "%MSI%" "%URL%"
if errorlevel 1 ( echo ERROR: download failed. & goto :end )
if not exist "%MSI%" ( echo ERROR: download failed. & goto :end )

REM ---- 2. silent install ------------------------------------
echo [2/5] Installing...
msiexec /i "%MSI%" /qn /norestart WIXUI_DONTVALIDATEPATH=1 /l*v "%LOG%"
set "RC=%errorlevel%"
if "%RC%"=="0" (
  echo    installed OK
) else if "%RC%"=="3010" (
  echo    installed OK ^(reboot pending^)
) else (
  echo    WARNING: msiexec exit code %RC% ^(log: %LOG%^)
)

set "RD=%ProgramFiles%\RustDesk\RustDesk.exe"
if not exist "%RD%" set "RD=%ProgramFiles(x86)%\RustDesk\RustDesk.exe"

REM ---- 3. write config --------------------------------------
echo [3/5] Writing RustDesk2.toml...
call :writecfg "%APPDATA%\RustDesk\config"
call :writecfg "%ProgramData%\RustDesk\config"
call :writecfg "%SystemRoot%\ServiceProfiles\LocalService\AppData\Roaming\RustDesk\config"
call :writecfg "%SystemRoot%\System32\config\systemprofile\AppData\Roaming\RustDesk\config"

REM ---- 4. restart service -----------------------------------
echo [4/5] Restarting RustDesk service...
sc query RustDesk >nul 2>&1
if not errorlevel 1 (
  net stop RustDesk >nul 2>&1
  timeout /t 2 /nobreak >nul
  net start RustDesk >nul 2>&1
)

REM ---- 5. cleanup / done ------------------------------------
if exist "%MSI%" del /q "%MSI%"
echo [5/5] Done.
echo.
if exist "%RD%" (
  echo RustDesk installed: "%RD%"
  echo Check: Settings -^> Network must show %HOST% and the key.
) else (
  echo RustDesk.exe not found - install likely failed, see %LOG%
)
goto :end

REM ===========================================================
:writecfg
set "D=%~1"
if not exist "%D%" md "%D%" >nul 2>&1
if not exist "%D%" ( echo    skip ^(no access^): %D% & exit /b )
> "%D%\RustDesk2.toml" (
  echo rendezvous_server = "%HOST%"
  echo nat_type = 1
  echo serial = 0
  echo:
  echo [options]
  echo custom-rendezvous-server = "%HOST%"
  echo key = "%KEY%"
  echo relay-server = "%RELAY%"
)
echo    wrote: %D%\RustDesk2.toml
exit /b

:end
echo.
pause
endlocal
