@echo off
setlocal EnableExtensions
title Atlas Connect - Bench Setup
color 0B

echo ============================================================
echo   ATLAS CONNECT - BENCH PC SETUP
echo   Installs / configures Atlas Connect (RustDesk) so this PC
echo   can be reached remotely through the Atlas relay.
echo ============================================================
echo.

REM ---- Self-elevate to Administrator (needed for install + service) ----
net session >nul 2>&1
if %errorlevel% neq 0 (
    echo Requesting administrator rights...
    powershell -NoProfile -Command "Start-Process -FilePath '%~f0' -Verb RunAs"
    exit /b
)

set "HOST=atlas-server.taile9cc75.ts.net"
set "RELAY=atlas-server.taile9cc75.ts.net:21117"
set "KEY=+k6POx27eTeXClOJhuy+7yzKEkBVvH1NywXWJt4EvnU===="
set "MSI_URL=https://%HOST%/releases/public/atlas-connect/AtlasConnect-1.4.9-x86_64.msi"
set "EXE_URL=https://%HOST%/releases/public/atlas-connect/AtlasConnect-1.4.9-x86_64.exe"

REM ---- Locate the Atlas Connect / RustDesk binary ----
set "APP="
for %%P in (
    "%ProgramFiles%\RustDesk\AtlasConnect.exe"
    "%ProgramFiles%\RustDesk\rustdesk.exe"
    "%ProgramFiles(x86)%\RustDesk\AtlasConnect.exe"
    "%ProgramFiles(x86)%\RustDesk\rustdesk.exe"
    "%LocalAppData%\Programs\RustDesk\AtlasConnect.exe"
) do if exist "%%P" set "APP=%%P"
if not defined APP (
    where AtlasConnect.exe >nul 2>&1 && set "APP=AtlasConnect.exe"
)
if not defined APP (
    where rustdesk.exe >nul 2>&1 && set "APP=rustdesk.exe"
)

if not defined APP (
    echo [!] Atlas Connect is not installed. Downloading the installer...
    powershell -NoProfile -Command "(New-Object System.Net.WebClient).DownloadFile('%MSI_URL%', '%TEMP%\AtlasConnect.msi')"
    if not exist "%TEMP%\AtlasConnect.msi" (
        echo [X] Download failed. Is this PC on the tailnet / internet?
        pause
        exit /b 1
    )
    echo [*] Installing silently (this may take a minute)...
    msiexec /i "%TEMP%\AtlasConnect.msi" /qn /norestart
    for %%P in (
        "%ProgramFiles%\RustDesk\AtlasConnect.exe"
        "%ProgramFiles%\RustDesk\rustdesk.exe"
        "%ProgramFiles(x86)%\RustDesk\AtlasConnect.exe"
        "%ProgramFiles(x86)%\RustDesk\rustdesk.exe"
    ) do if exist "%%P" set "APP=%%P"
)
if not defined APP (
    echo [X] Could not locate Atlas Connect after install.
    pause
    exit /b 1
)
echo [OK] Using: %APP%

REM ---- Point it at the Atlas relay ----
echo [*] Applying Atlas relay config...
"%APP%" --config "host=%HOST%,key=%KEY%,relay=%RELAY%,"

REM ---- Make sure the service runs (unattended access) ----
sc query RustDesk >nul 2>&1 && (
    echo [*] Starting RustDesk service...
    sc start RustDesk >nul 2>&1
    net start RustDesk >nul 2>&1
)

REM ---- Show the config file so you can confirm the relay is set ----
if exist "%AppData%\RustDesk\config\RustDesk2.toml" (
    echo.
    echo ------------------------------------------------------------
    echo   Relay config written to:
    echo   %AppData%\RustDesk\config\RustDesk2.toml
    echo ------------------------------------------------------------
    type "%AppData%\RustDesk\config\RustDesk2.toml"
)

REM ---- Launch the client so it registers with the relay ----
echo [*] Launching Atlas Connect...
start "" "%APP%"

echo.
echo ============================================================
echo   DONE. Your Atlas Connect ID is shown in the app window.
echo   From anywhere: web.rustdesk.com  -or-  the AtlasConnect APK
echo   ID server : %HOST%
echo   Key       : %KEY%
echo ============================================================
pause