@echo off
:: ============================================================
:: RustDesk Silent Installer - Brainy-People.GmbH
:: Laedt automatisch die NEUESTE Version von GitHub herunter.
:: Doppelklick genuegt - alles wird automatisch eingerichtet.
:: ============================================================
title Brainy-People.GmbH - RustDesk Setup

:: Admin-Rechte anfordern
net session >nul 2>&1
if %errorlevel% neq 0 (
    echo Fordere Administratorrechte an...
    powershell -Command "Start-Process '%~f0' -Verb RunAs"
    exit /b
)

:: --- Konfiguration ---
set "RUSTDESK_SERVER=rustdesk.brainy-people.gmbh"
set "RUSTDESK_KEY=FpTbzLX2y9EaQt9C6caRk87giw44lkRZQrlWnrSJvZM="
set "INSTALL_DIR=C:\Program Files\RustDesk"

echo.
echo ============================================================
echo.
echo        Brainy-People.GmbH - Fernwartung
echo.
echo ============================================================
echo.
echo [1/5] Neueste RustDesk-Version wird ermittelt...

:: Neueste Version ueber GitHub API abfragen
for /f "delims=" %%V in ('powershell -Command "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; try { $r = Invoke-RestMethod -Uri 'https://api.github.com/repos/rustdesk/rustdesk/releases/latest' -UseBasicParsing; $r.tag_name } catch { Write-Output 'FEHLER' }"') do set "RUSTDESK_VERSION=%%V"

if "%RUSTDESK_VERSION%"=="FEHLER" (
    echo.
    echo FEHLER: Konnte neueste Version nicht ermitteln.
    echo Bitte Internetverbindung pruefen.
    echo.
    pause
    exit /b 1
)

echo          Gefunden: Version %RUSTDESK_VERSION%

set "MSI_URL=https://github.com/rustdesk/rustdesk/releases/download/%RUSTDESK_VERSION%/rustdesk-%RUSTDESK_VERSION%-x86_64.msi"
set "MSI_FILE=%TEMP%\rustdesk-%RUSTDESK_VERSION%.msi"

echo [2/5] RustDesk %RUSTDESK_VERSION% wird heruntergeladen...

powershell -Command "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; (New-Object Net.WebClient).DownloadFile('%MSI_URL%', '%MSI_FILE%')"

if not exist "%MSI_FILE%" (
    echo.
    echo FEHLER: Download fehlgeschlagen. Bitte Internetverbindung pruefen.
    echo.
    pause
    exit /b 1
)

echo [3/5] RustDesk wird installiert (bitte warten)...

msiexec /i "%MSI_FILE%" /qn INSTALLFOLDER="%INSTALL_DIR%" CREATEDESKTOPSHORTCUTS="Y" CREATESTARTMENUSHORTCUTS="Y" /l*v "%TEMP%\rustdesk_install.log"

if %errorlevel% neq 0 (
    echo.
    echo FEHLER: Installation fehlgeschlagen. Fehlercode: %errorlevel%
    echo Logdatei: %TEMP%\rustdesk_install.log
    echo.
    pause
    exit /b 1
)

:: Warten bis RustDesk bereit ist
echo [4/5] Warte auf RustDesk-Dienst...
timeout /t 5 /nobreak >nul

:: Config-Verzeichnis sicherstellen
if not exist "%APPDATA%\RustDesk\config" (
    mkdir "%APPDATA%\RustDesk\config"
)

echo [5/5] Server-Konfiguration wird eingerichtet...

:: RustDesk2.toml mit Server-Konfiguration schreiben
(
echo rendezvous_server = 'rs-ny.rustdesk.com'
echo nat_type = 1
echo serial = 0
echo.
echo [options]
echo custom-rendezvous-server = '%RUSTDESK_SERVER%'
echo relay-server = '%RUSTDESK_SERVER%'
echo api-server = ''
echo key = '%RUSTDESK_KEY%'
) > "%APPDATA%\RustDesk\config\RustDesk2.toml"

:: Config auch fuer den Service-Kontext schreiben (SYSTEM-Benutzer)
if not exist "C:\Windows\ServiceProfiles\LocalService\AppData\Roaming\RustDesk\config" (
    mkdir "C:\Windows\ServiceProfiles\LocalService\AppData\Roaming\RustDesk\config"
)
copy /Y "%APPDATA%\RustDesk\config\RustDesk2.toml" "C:\Windows\ServiceProfiles\LocalService\AppData\Roaming\RustDesk\config\RustDesk2.toml" >nul 2>&1

:: RustDesk-Dienst neu starten damit Config uebernommen wird
net stop RustDesk >nul 2>&1
timeout /t 2 /nobreak >nul
net start RustDesk >nul 2>&1

:: Aufraeumen
del "%MSI_FILE%" >nul 2>&1

echo.
echo ============================================================
echo.
echo   Brainy-People.GmbH - Installation abgeschlossen!
echo.
echo   Die Fernwartungssoftware (Version %RUSTDESK_VERSION%)
echo   wurde erfolgreich eingerichtet.
echo   Sie koennen dieses Fenster jetzt schliessen.
echo.
echo ============================================================
echo.
pause
