@echo off
rem ============================================================
rem  PixelPup Remote Support - Home Computer Setup
rem
rem  Run this on your HOME or PERSONAL computer (NOT your work
rem  computer). Installs PixelPup so you can reach your work
rem  machines from anywhere.
rem
rem  Differences from the work installer:
rem    - Does NOT run as a background service (Manual start only)
rem    - Does NOT register this machine as a managed device
rem    - Does NOT set a permanent password (no incoming access)
rem    - Opens PixelPup so you can sign in and connect right away
rem
rem  Before running this you need:
rem    - A PixelPup account (register at console.pixelpup.net)
rem    - Your work computer set up with a personal password
rem      (run PixelPup_SetMyPassword.bat on the work machine first)
rem ============================================================
Title PixelPup Remote Support - Home Computer Setup
setlocal

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

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"
set "TMPEXE=%TEMP%\PixelPup_Installer.exe"
set "RDEXE=C:\Program Files\RustDesk\RustDesk.exe"

color 0B
cls
echo.
echo    ==========================================================
echo.
echo        __      _
echo     o'')}____//     P I X E L P U P
echo      `_/      )     HOME COMPUTER SETUP
echo      (_(_/-(_/
echo.
echo    ==========================================================
echo      Sets up PixelPup on this home or personal computer.
echo      This machine will NOT be added to your admin console.
echo      You are connecting OUT to your work machines only.
echo    ----------------------------------------------------------
echo.

rem ---- 1. Download ----
echo [1/5] 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 ----
echo [2/5] Installing...
"%TMPEXE%" --silent-install

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] Service did not install.
    pause
    exit /b 1
)
timeout /t 2 >nul
goto waitsvc
:svcok

rem ---- 3. Firewall rules ----
echo [3/5] Adding firewall rules...
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 to user profiles only ----
rem  NOT written to the system/LocalSystem profile so the background
rem  service does not auto-register this machine as a managed device.
echo [4/5] Configuring PixelPup network...
net stop RustDesk >nul 2>&1
for /d %%U in (C:\Users\*) do (
    if not exist "%%U\AppData\Roaming\RustDesk\config" mkdir "%%U\AppData\Roaming\RustDesk\config" >nul 2>&1
    > "%%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. Set service to Manual (not autostart) ----
rem  The service will not run in the background or register this
rem  machine as an online peer unless you explicitly start it.
echo [5/5] Setting to manual start (not a background service)...
sc config RustDesk start= demand >nul 2>&1
net stop RustDesk >nul 2>&1

echo.
echo    ==========================================================
echo      HOME COMPUTER IS SET UP.
echo.
echo      Open PixelPup from the Start menu to connect.
echo.
echo        1. Open PixelPup from your Start menu.
echo        2. Click the person icon in the top-right corner.
echo        3. Sign in with your PixelPup account email + password.
echo        4. Your work computers appear in your address book.
echo        5. Click a computer and enter the password you set
echo           when you ran PixelPup_SetMyPassword on it.
echo.
echo      PixelPup only runs while you have it open.
echo      This computer is NOT visible or accessible to admins.
echo.
echo      Don't have a PixelPup account?
echo      Register at https://console.pixelpup.net
echo    ==========================================================
echo.
pause
