@echo off
setlocal EnableExtensions
REM ============================================================
REM  RustDesk unattended HOST setup  ->  self-hosted 209.38.91.37
REM  Run on the machine you want to CONTROL (media PC / work laptop).
REM  Double-click it; approve the UAC prompt.
REM
REM  Password: leave RUSTDESK_PW blank to auto-generate a strong random one
REM  (printed at the end - RECORD IT). Or set a fixed value (LETTERS+NUMBERS).
REM  NOTE: this file is hosted publicly, so do NOT bake a real password here.
REM ============================================================

set "RUSTDESK_PW="
set "RUSTDESK_CFG=9JiI6ISawFmIsISPVV0RaN0Zrt2VahDe1YmM25GatRnV0l1MrIkbFd3Z0tidDlkRtVENttkVHJiOikXZrJCLiczMuETOugzMukDMyIiOikXYsVmciwiI3MjLxkjL4MjL5AjMiojI0N3boJye"
set "WORK=%PUBLIC%\rdsetup"

REM --- self-elevate (copy local first; mapped/G: drives vanish under UAC) ---
net session >nul 2>&1
if %errorlevel% neq 0 (
  if not exist "%WORK%" mkdir "%WORK%"
  copy /y "%~f0" "%WORK%\setup-rustdesk-host.bat" >nul
  if exist "%~dp0rustdesk.exe" copy /y "%~dp0rustdesk.exe" "%WORK%\rustdesk.exe" >nul
  powershell -Command "Start-Process -Verb RunAs -FilePath '%WORK%\setup-rustdesk-host.bat'"
  exit /b
)

if not exist "%WORK%" mkdir "%WORK%"
cd /d "%WORK%"

REM --- generate a strong random password if none was set above ---
if not defined RUSTDESK_PW (
  echo Generating a strong random password for this PC...
  powershell -NoProfile -Command "-join ((48..57)+(65..90)+(97..122)|Get-Random -Count 20|%%{[char]$_})" > "%WORK%\pw.txt"
  set /p RUSTDESK_PW=<"%WORK%\pw.txt"
  del "%WORK%\pw.txt" >nul 2>&1
)

REM --- get installer: co-located copy if present, else download latest ---
if not exist "%WORK%\rustdesk.exe" (
  echo Downloading RustDesk...
  powershell -Command "[Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12; $u=(Invoke-RestMethod 'https://api.github.com/repos/rustdesk/rustdesk/releases/latest').assets | Where-Object {$_.name -like '*-x86_64.exe'} | Select-Object -First 1 -ExpandProperty browser_download_url; Invoke-WebRequest -UseBasicParsing $u -OutFile '%WORK%\rustdesk.exe'"
)
if not exist "%WORK%\rustdesk.exe" (
  echo ERROR: could not get rustdesk.exe - no internet or GitHub blocked.
  echo Put a rustdesk.exe next to this batch and run again.
  pause
  exit /b 1
)

echo Installing RustDesk service...
"%WORK%\rustdesk.exe" --silent-install

REM --- wait until the installed client appears (up to ~60s) ---
set "RD=%ProgramFiles%\RustDesk\rustdesk.exe"
set /a n=0
:waitinstall
if exist "%RD%" goto installed
set /a n+=1
if %n% geq 30 (
  echo ERROR: RustDesk did not finish installing.
  pause
  exit /b 1
)
timeout /t 2 /nobreak >nul
goto waitinstall
:installed
echo Installed. Giving the service a moment to start...
timeout /t 8 /nobreak >nul

echo Applying self-hosted server config...
"%RD%" --config "%RUSTDESK_CFG%"
timeout /t 2 /nobreak >nul

echo Setting permanent password...
"%RD%" --password "%RUSTDESK_PW%"
timeout /t 2 /nobreak >nul

echo Restarting RustDesk service so the new server takes effect...
net stop RustDesk >nul 2>&1
timeout /t 2 /nobreak >nul
net start RustDesk >nul 2>&1
timeout /t 5 /nobreak >nul

echo Launching RustDesk...
start "" "%RD%"
timeout /t 5 /nobreak >nul

REM --- poll for the ID until it appears (up to ~45s) ---
echo Waiting for this PC's RustDesk ID...
set /a n=0
:waitid
"%RD%" --get-id > "%WORK%\id.txt" 2>nul
set "RDID="
set /p RDID=<"%WORK%\id.txt"
if defined RDID goto gotid
set /a n+=1
if %n% geq 15 goto noid
timeout /t 3 /nobreak >nul
goto waitid

:gotid
echo.
echo ==================== DONE ====================
echo  This PC's RustDesk ID: %RDID%
echo  ID server : 209.38.91.37
echo  Password  : %RUSTDESK_PW%
echo  From home: open RustDesk, type the ID above, then this password.
echo =============================================
pause
exit /b 0

:noid
echo.
echo WARNING: installed + configured, but no ID came back yet.
echo Open the RustDesk app on this PC - it should show the ID, and
echo Settings - Network - ID Server should read 209.38.91.37.
pause
exit /b 0
