@echo off
setlocal

set "CONFIG_DIR=%APPDATA%\RustDesk\config"
set "APP_DIR=%LOCALAPPDATA%\GuildaDesk"
set "APP_EXE=%APP_DIR%\GuildaDesk.exe"
set "SERVER_IP=169.58.221.187"
set "SERVER_KEY=+8x2nSNQLiQ5GnaSq34ZvPWIgOrZeaTnIfD2LF4VwdM="

echo.
echo  === Instalando GuildaDesk ===
echo.

if not exist "%CONFIG_DIR%" mkdir "%CONFIG_DIR%"
if not exist "%APP_DIR%" mkdir "%APP_DIR%"

echo Registrando download...
powershell -NoProfile -Command "try { Invoke-WebRequest -Uri 'https://guildadesk.com/api/track/download' -Method POST -TimeoutSec 3 } catch {}"

echo Baixando cliente...
powershell -NoProfile -Command "Invoke-WebRequest -Uri 'https://guildadesk.com/downloads/GuildaDesk-Setup-1.4.18.exe' -OutFile '%APP_EXE%'"

if not exist "%APP_EXE%" (
  echo Falha ao baixar o cliente. Verifique sua conexao e tente novamente.
  pause
  exit /b 1
)

echo Configurando servidor...
> "%CONFIG_DIR%\RustDesk2.toml" (
  echo rendezvous_server = '%SERVER_IP%:21116'
  echo nat_type = 1
  echo serial = 0
  echo unlock_pin = ''
  echo trusted_devices = ''
  echo.
  echo [options]
  echo custom-rendezvous-server = '%SERVER_IP%'
  echo relay-server = '%SERVER_IP%:21117'
  echo key = '%SERVER_KEY%'
)

echo.
echo Instalando o GuildaDesk no sistema (o Windows vai pedir confirmacao - clique em "Sim")...
start "" /wait "%APP_EXE%" --silent-install

echo.
echo Pronto! Abrindo o GuildaDesk...
start "" "C:\Program Files\GuildaDesk\GuildaDesk.exe"

endlocal
