@echo off
title Installazione Assistenza Remota - Informatica Gelormini
color 0A
echo ============================================================
echo.
echo      AG - ASSISTENZA GELORMINI
echo      Installazione Assistenza Remota
echo.
echo ============================================================
echo.
echo Scaricamento in corso...
echo.

set TMPDIR=%TEMP%\rustdesk_install
mkdir "%TMPDIR%" 2>nul

powershell -Command "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $ProgressPreference = 'SilentlyContinue'; Invoke-WebRequest -Uri 'https://github.com/rustdesk/rustdesk/releases/download/1.4.6/rustdesk-1.4.6-x86_64.exe' -OutFile '%TMPDIR%\rustdesk.exe'"

if not exist "%TMPDIR%\rustdesk.exe" (
    echo.
    echo ERRORE: Download fallito. Controlla la connessione internet.
    echo.
    pause
    exit /b 1
)

echo Download completato.
echo.
echo Installazione in corso...
"%TMPDIR%\rustdesk.exe" --silent-install

echo Attendo installazione...
timeout /t 20 /nobreak >nul

echo.
echo Configurazione server assistenza...

:: === FIX: Scrivi config in TUTTE le posizioni possibili ===

:: 1. Config utente corrente
set RUSTDESK_CFG_USER=%AppData%\RustDesk\config\RustDesk2.toml
mkdir "%AppData%\RustDesk\config" 2>nul

(
echo rendezvous_server = 'informaticagelormini.ddns.net'
echo nat_type = 1
echo serial = 0
echo.
echo [options]
echo custom-rendezvous-server = 'informaticagelormini.ddns.net'
echo relay-server = 'informaticagelormini.ddns.net'
echo key = 'PEQMCbxAg6WVxPDEd7YN6emBoeekWLtBi7kAeKhLopU='
echo enable-audio = 'Y'
) > "%RUSTDESK_CFG_USER%"

:: 2. Config servizio Windows (LocalService) - CRITICO per persistenza dopo riavvio
set SVC_CFG=C:\Windows\ServiceProfiles\LocalService\AppData\Roaming\RustDesk\config
mkdir "%SVC_CFG%" 2>nul

(
echo rendezvous_server = 'informaticagelormini.ddns.net'
echo nat_type = 1
echo serial = 0
echo.
echo [options]
echo custom-rendezvous-server = 'informaticagelormini.ddns.net'
echo relay-server = 'informaticagelormini.ddns.net'
echo key = 'PEQMCbxAg6WVxPDEd7YN6emBoeekWLtBi7kAeKhLopU='
echo enable-audio = 'Y'
) > "%SVC_CFG%\RustDesk2.toml"

:: 3. Config nella cartella di installazione (fallback)
set INSTALL_CFG=%ProgramFiles%\RustDesk
if exist "%INSTALL_CFG%\rustdesk.exe" (
    (
    echo rendezvous_server = 'informaticagelormini.ddns.net'
    echo nat_type = 1
    echo serial = 0
    echo.
    echo [options]
    echo custom-rendezvous-server = 'informaticagelormini.ddns.net'
    echo relay-server = 'informaticagelormini.ddns.net'
    echo key = 'PEQMCbxAg6WVxPDEd7YN6emBoeekWLtBi7kAeKhLopU='
    echo enable-audio = 'Y'
    ) > "%INSTALL_CFG%\RustDesk2.toml"
)

echo Configurazione scritta in tutte le posizioni.

:: Riavvia RustDesk per applicare la configurazione
net stop RustDesk 2>nul
taskkill /f /im rustdesk.exe 2>nul
timeout /t 3 /nobreak >nul
net start RustDesk 2>nul
timeout /t 3 /nobreak >nul
start "" "%ProgramFiles%\RustDesk\rustdesk.exe"


:: === Crea collegamento desktop "Assistenza Gelormini" ===
powershell -Command "$ws = New-Object -ComObject WScript.Shell; $s = $ws.CreateShortcut([Environment]::GetFolderPath('Desktop') + '\Assistenza Gelormini.lnk'); $s.TargetPath = '%ProgramFiles%\RustDesk\rustdesk.exe'; $s.Description = 'Assistenza Remota - Informatica Gelormini'; $s.WorkingDirectory = '%ProgramFiles%\RustDesk'; $s.Save()"

:: Cleanup
rd /s /q "%TMPDIR%" 2>nul

echo.
echo ============================================================
echo.
echo   INSTALLAZIONE COMPLETATA!
echo.
echo   Sul desktop trovi "Assistenza Gelormini"
echo   Comunica il tuo ID al tecnico per ricevere assistenza.
echo.
echo   Informatica Gelormini - Tel: 339 288 5800
echo.
echo ============================================================
echo.
pause
