@echo off
setlocal EnableExtensions EnableDelayedExpansion

title Richard IT - Remote Support Setup - FORCE CONFIG
color 0a

:: Run this batch file as administrator so the RustDesk output stays in this same terminal.
net session >nul 2>&1
if %errorlevel% neq 0 (
    echo.
    echo  ============================================================
    echo    Richard IT  -  Remote Support Setup
    echo  ============================================================
    echo.
    echo    Windows will ask for permission.
    echo    Please click YES to continue.
    echo.
    powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "Start-Process -FilePath '%~f0' -Verb RunAs"
    exit /b
)

echo.
echo  ============================================================
echo    Richard IT  -  Remote Support Setup  FORCE CONFIG MODE
echo  ============================================================
echo.
echo    This window will show each setup step for troubleshooting.
echo.
echo    When setup finishes, this window will show:
echo      - Code / RustDesk ID
echo      - Password
echo.
echo    Please give BOTH to Richard IT.
echo  ------------------------------------------------------------
echo.

set "LOCAL_PS1=%~dp0deploy-rustdesk.ps1"
set "TEMP_PS1=%TEMP%\deploy-rustdesk-richardit.ps1"
set "REMOTE_URL=https://www.richardit.com/deploy-rustdesk.ps1"
set "CACHEBUST=%RANDOM%%RANDOM%%RANDOM%"

echo [BAT] Running as administrator: YES
echo [BAT] Batch file path: %~f0
echo [BAT] Current folder: %~dp0
echo [BAT] Checking for local PowerShell script:
echo [BAT]   %LOCAL_PS1%
echo.

if exist "%LOCAL_PS1%" (
    echo [BAT] Found local deploy-rustdesk.ps1.
    echo [BAT] Running local script now...
    echo.
    powershell.exe -NoProfile -ExecutionPolicy Bypass -File "%LOCAL_PS1%"
) else (
    echo [BAT] Local deploy-rustdesk.ps1 not found.
    echo [BAT] Downloading fresh script from website...
    echo [BAT] URL: %REMOTE_URL%?cb=%CACHEBUST%
    echo [BAT] Destination: %TEMP_PS1%
    echo.

    powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "$ErrorActionPreference='Stop'; [Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12; $url='%REMOTE_URL%?cb=%CACHEBUST%'; $out='%TEMP_PS1%'; Write-Host ('[PS-DOWNLOAD] Downloading ' + $url); Invoke-WebRequest -UseBasicParsing -Uri $url -OutFile $out; Write-Host ('[PS-DOWNLOAD] Saved to ' + $out); Write-Host ('[PS-DOWNLOAD] Size: ' + (Get-Item $out).Length + ' bytes')"

    if errorlevel 1 (
        echo.
        echo  ERROR: Setup could not download deploy-rustdesk.ps1.
        echo  Check internet connection or verify the website file exists.
        echo.
        pause
        exit /b 1
    )

    echo.
    echo [BAT] Running downloaded script now...
    echo.
    powershell.exe -NoProfile -ExecutionPolicy Bypass -File "%TEMP_PS1%"
)

set "SCRIPT_EXIT=%ERRORLEVEL%"

echo.
echo  ------------------------------------------------------------
echo    PowerShell script exit code: %SCRIPT_EXIT%
echo  ------------------------------------------------------------

if not "%SCRIPT_EXIT%"=="0" (
    color 0c
    echo.
    echo    Setup reported an error. Take a screenshot of this window
    echo    and send Richard IT the log path shown above.
    echo.
) else (
    echo.
    echo    If setup completed successfully, give Richard IT the
    echo    Code / RustDesk ID and Password shown above.
    echo.
)

echo  ============================================================
echo.
pause
exit /b %SCRIPT_EXIT%
