@echo off
rem ============================================================
rem  PixelPup Remote Support - one-click installer
rem  Downloads the client, installs it as a service, points it at
rem  pixelpup.net (server + key + API), writes config where the
rem  LocalSystem service reads it, and registers. One admin prompt.
rem ============================================================
Title PixelPup Remote Support Installer

rem ---- Self-elevate (install + service config need admin) ----
net session >nul 2>&1
if %errorlevel% NEQ 0 (
    echo Requesting administrator privileges...
    powershell -NoProfile -Command "Start-Process -FilePath '%~f0' -Verb RunAs"
    exit /b
)

rem ============================================================
rem  SETTINGS
rem ============================================================
set "IDSRV=pixelpup.net"
set "APISRV=https://console.pixelpup.net"
set "KEY=VYJnhkZ4XuVVCOLt6L1DR4Q5ySF6y97TBScSBcIDIc0="
set "EXEURL=https://pixelpup.net/rustdesk-host=pixelpup.net,key=VYJnhkZ4XuVVCOLt6L1DR4Q5ySF6y97TBScSBcIDIc0=.exe"

rem ---- Shared permanent password ------------------------------
rem  THIS FILE IS PUBLISHED ON pixelpup.net. Leave PERMPW as CHANGE_ME
rem  so no password is ever shipped on the public page (the server key
rem  is already public, so a published password would be the only lock
rem  on every machine). To deploy machines with a permanent password
rem  for no-prompt console "Link" shares, use the hand-out copy in
rem  D:\RustDesk_Server_Deployment\Technician\ and send that file
rem  directly to the customer - do not link it publicly.
set "PERMPW=CHANGE_ME"

set "TMPEXE=%TEMP%\PixelPup_Installer.exe"

color 0B
cls
echo.
echo    ==========================================================
echo.
echo        __      _
echo     o'')}____//     P I X E L P U P
echo      `_/      )     REMOTE SUPPORT
echo      (_(_/-(_/      installer
echo.
echo    ==========================================================
echo      Fast, secure remote help  -  powered by pixelpup.net
echo    ----------------------------------------------------------
echo.

rem ---- 1. Download the pre-configured client ----
echo [1/6] Downloading client...
powershell -NoProfile -Command "try { Invoke-WebRequest -Uri '%EXEURL%' -OutFile '%TMPEXE%' -UseBasicParsing } catch { exit 1 }"
if not exist "%TMPEXE%" (
    echo [X] Download failed. Check the internet connection and try again.
    pause
    exit /b 1
)

rem ---- 2. Silent install (creates the Windows service) ----
echo [2/6] Installing background service...
"%TMPEXE%" --silent-install

rem Wait for the service to appear (up to ~30s)
set /a tries=0
:waitsvc
sc query RustDesk >nul 2>&1
if %errorlevel%==0 goto svcok
set /a tries+=1
if %tries% GEQ 15 (
    echo [X] The service did not install.
    pause
    exit /b 1
)
timeout /t 2 >nul
goto waitsvc
:svcok

rem ---- 3. Firewall rules (idempotent - replaces any previous PixelPup rules).
rem  The program-scoped inbound rule is the important one: incoming sessions
rem  arrive on ephemeral ports as well as 21118, so a port-only rule misses them.
echo [3/6] Adding firewall rules...
set "RDEXE=C:\Program Files\RustDesk\RustDesk.exe"
netsh advfirewall firewall delete rule name="PixelPup RustDesk - Client (Inbound)" >nul 2>&1
netsh advfirewall firewall add rule name="PixelPup RustDesk - Client (Inbound)" dir=in action=allow program="%RDEXE%" enable=yes profile=any >nul
netsh advfirewall firewall delete rule name="PixelPup RustDesk - Server & API (Outbound TCP)" >nul 2>&1
netsh advfirewall firewall add rule name="PixelPup RustDesk - Server & API (Outbound TCP)" dir=out action=allow program="%RDEXE%" protocol=TCP remoteport=443,21114-21119 profile=any >nul
netsh advfirewall firewall delete rule name="PixelPup RustDesk - Registration (Outbound UDP)" >nul 2>&1
netsh advfirewall firewall add rule name="PixelPup RustDesk - Registration (Outbound UDP)" dir=out action=allow program="%RDEXE%" protocol=UDP remoteport=21116 profile=any >nul

rem ---- 4. Write server config where the LocalSystem service reads it ----
echo [4/6] Configuring connection to pixelpup.net...
net stop RustDesk >nul 2>&1

rem The service reads its config from one of two profiles depending on install
rem history: systemprofile (fresh 1.4.x installs) or ServiceProfiles\LocalService
rem (older/upgraded installs). Write both so either way the service finds it.
for %%D in ("C:\Windows\System32\config\systemprofile\AppData\Roaming\RustDesk\config" "C:\Windows\ServiceProfiles\LocalService\AppData\Roaming\RustDesk\config") do (
    if not exist "%%~D" mkdir "%%~D"
    > "%%~D\RustDesk2.toml" echo [options]
    >> "%%~D\RustDesk2.toml" echo custom-rendezvous-server = '%IDSRV%'
    >> "%%~D\RustDesk2.toml" echo relay-server = '%IDSRV%'
    >> "%%~D\RustDesk2.toml" echo api-server = '%APISRV%'
    >> "%%~D\RustDesk2.toml" echo key = '%KEY%'
)

rem Also each user profile that already has a RustDesk folder (UI display)
for /d %%U in (C:\Users\*) do (
    if exist "%%U\AppData\Roaming\RustDesk" (
        if not exist "%%U\AppData\Roaming\RustDesk\config" mkdir "%%U\AppData\Roaming\RustDesk\config"
        > "%%U\AppData\Roaming\RustDesk\config\RustDesk2.toml" echo [options]
        >> "%%U\AppData\Roaming\RustDesk\config\RustDesk2.toml" echo custom-rendezvous-server = '%IDSRV%'
        >> "%%U\AppData\Roaming\RustDesk\config\RustDesk2.toml" echo relay-server = '%IDSRV%'
        >> "%%U\AppData\Roaming\RustDesk\config\RustDesk2.toml" echo api-server = '%APISRV%'
        >> "%%U\AppData\Roaming\RustDesk\config\RustDesk2.toml" echo key = '%KEY%'
    )
)

rem ---- 5. Start the service and wait for Running ----
echo [5/6] Starting and registering...
net start RustDesk >nul 2>&1
set /a tries=0
:waitrun
sc query RustDesk | find "RUNNING" >nul
if %errorlevel%==0 goto runok
set /a tries+=1
if %tries% GEQ 15 goto runok
timeout /t 2 >nul
goto waitrun
:runok

rem ---- 6. Set the permanent password (only if configured) ----
if /I "%PERMPW%"=="CHANGE_ME" (
    echo [6/6] No permanent password set ^(temporary password only^).
) else (
    echo [6/6] Setting permanent password...
    "C:\Program Files\RustDesk\RustDesk.exe" --password "%PERMPW%"
)

echo.
echo    ==========================================================
echo      ALL SET - this computer is now linked to PixelPup.
echo      It will show ONLINE in our console within ~30 seconds.
echo.
echo      WANT YOUR OWN LOGIN?
echo      Visit https://console.pixelpup.net and click Register.
echo      Fill in your details - our team unlocks new accounts
echo      and assigns your machines, usually the same day. Then
echo      sign in any time at:
echo.
echo          https://console.pixelpup.net/webclient/
echo.
echo      to see and connect to your computers from any browser.
echo    ==========================================================
echo.
pause
