@echo off
title RustDesk Setup
:: Install RustDesk already pointed at ctrl.bpsystems.ca. No key entry required.
:: Official signed MSI 1.5.0, verified by sha256.
:: RIGHT-CLICK this file and choose "Run as administrator".
:: Unattended (Intune/SCCM/remote exec): install-windows.cmd /quiet

net session >nul 2>&1
if errorlevel 1 (
  echo Requesting administrator rights...
  powershell -NoProfile -Command "Start-Process -FilePath '%~f0' -Verb RunAs"
  exit /b
)

set "RDHOST=ctrl.bpsystems.ca"
set "RDKEY=av9g3v3uXO2v9mLuhQJg2H9okVSAlYcHUvRYN5MWxHE="
set "VER=1.5.0"
set "CFG=host=ctrl.bpsystems.ca,key=av9g3v3uXO2v9mLuhQJg2H9okVSAlYcHUvRYN5MWxHE="
set "BASE=https://github.com/rustdesk/rustdesk/releases/download/%VER%"

set "ARCH=x86_64"
if /i "%PROCESSOR_ARCHITECTURE%"=="ARM64" set "ARCH=aarch64"
set "ASSET=rustdesk-%VER%-%ARCH%.msi"

call :sha "%ASSET%"
if not defined SUM (
  echo ERROR: no pinned checksum for %ASSET% -- regenerate this installer
  goto :fail
)

set "WORK=%TEMP%\rustdesk-%VER%"
if not exist "%WORK%" mkdir "%WORK%"
del "%WORK%\ok.txt" >nul 2>&1

echo ==^> downloading %ASSET%
curl -fsSL -o "%WORK%\%ASSET%" "%BASE%/%ASSET%"
if errorlevel 1 ( echo ERROR: download failed & goto :fail )

echo ==^> verifying sha256
set "GOT="
for /f "skip=1 delims=" %%H in ('certutil -hashfile "%WORK%\%ASSET%" SHA256') do if not defined GOT set "GOT=%%H"
set "GOT=%GOT: =%"
if /i not "%GOT%"=="%SUM%" (
  echo ERROR: CHECKSUM MISMATCH -- refusing to install this file
  goto :fail
)

echo ==^> installing ^(unattended^)
msiexec /i "%WORK%\%ASSET%" /qn /norestart
set "RC=%ERRORLEVEL%"
if not "%RC%"=="0" if not "%RC%"=="3010" ( echo ERROR: msiexec exited %RC% & goto :fail )

set "EXE=%ProgramFiles%\RustDesk\rustdesk.exe"
if not exist "%EXE%" set "EXE=%ProgramFiles(x86)%\RustDesk\rustdesk.exe"
if not exist "%EXE%" ( echo ERROR: rustdesk.exe not found after install & goto :fail )

echo ==^> applying server config
"%EXE%" --config "%CFG%"
ping -n 5 127.0.0.1 >nul 2>&1

:: Verify by FINDING the config file and requiring BOTH values. Results go to a
:: file rather than a variable: setting one inside a for-block needs delayed
:: expansion and silently does nothing without it.
echo ==^> verifying
for /r "%APPDATA%" %%F in (RustDesk2.toml) do if exist "%%F" (
  findstr /c:"%RDHOST%" "%%F" >nul 2>&1 && findstr /c:"%RDKEY%" "%%F" >nul 2>&1 && echo %%F>>"%WORK%\ok.txt"
)
for /r "%ProgramData%" %%F in (RustDesk2.toml) do if exist "%%F" (
  findstr /c:"%RDHOST%" "%%F" >nul 2>&1 && findstr /c:"%RDKEY%" "%%F" >nul 2>&1 && echo %%F>>"%WORK%\ok.txt"
)
for /r "%WINDIR%\ServiceProfiles" %%F in (RustDesk2.toml) do if exist "%%F" (
  findstr /c:"%RDHOST%" "%%F" >nul 2>&1 && findstr /c:"%RDKEY%" "%%F" >nul 2>&1 && echo %%F>>"%WORK%\ok.txt"
)
if not exist "%WORK%\ok.txt" (
  echo ERROR: the server config did NOT apply -- not reporting success
  goto :fail
)
echo     configured:
type "%WORK%\ok.txt"

echo.
echo ================================================================
echo  Done. RustDesk is installed and pointed at %RDHOST%.
echo  Open RustDesk and read out your ID. You will be shown a
echo  one-time code when someone connects.
echo ================================================================
echo.
if /i not "%~1"=="/quiet" pause
exit /b 0

:fail
echo.
echo Setup FAILED. Nothing was left half-installed on purpose -- send the
echo text above to Brett.
echo.
if /i not "%~1"=="/quiet" pause
exit /b 1

:sha
set "SUM="
if /i "%~1"=="rustdesk-1.5.0-x86_64.msi" set "SUM=624fe792ab1d76b35134dab84bff97a6bcd14043ffd090a2c6a2800f77580495"
if /i "%~1"=="rustdesk-1.5.0-aarch64.msi" set "SUM=ec4dc1afa5ae3d6a8ecceab1a681bfd17ed56400056c8fa1c182decf43eab1f7"
goto :eof
