@echo off
title Instalador de Suporte Remoto - Extreme STI
net session >nul 2>&1
if %errorlevel% neq 0 (
  echo Solicitando permissao de administrador...
  powershell -NoProfile -ExecutionPolicy Bypass -Command "Start-Process -FilePath '%~f0' -Verb RunAs"
  exit /b
)
cls
echo ==================================================
echo   Suporte Remoto - Extreme STI
echo ==================================================
echo.
set "TMPD=%TEMP%\suporte-remoto"
if not exist "%TMPD%" mkdir "%TMPD%"
powershell -NoProfile -ExecutionPolicy Bypass -Command "$n = Read-Host 'Digite seu nome ou o nome da sua empresa'; Set-Content -Path '%TMPD%\cliente.txt' -Value $n -Encoding UTF8"
echo.
echo [1/5] Baixando o RustDesk...
powershell -NoProfile -ExecutionPolicy Bypass -Command "[Net.ServicePointManager]::SecurityProtocol='Tls12'; Invoke-WebRequest -UseBasicParsing -Uri 'https://suporte.extremesti.com/download/rustdesk.exe' -OutFile '%TMPD%\rustdesk.exe'"
if not exist "%TMPD%\rustdesk.exe" (
  echo ERRO: nao foi possivel baixar. Verifique a internet e tente de novo.
  pause & exit /b 1
)
echo [2/5] Instalando...
"%TMPD%\rustdesk.exe" --silent-install
set "RD=%ProgramFiles%\RustDesk\rustdesk.exe"
set /a T=0
:espera
if exist "%RD%" goto instalado
timeout /t 2 /nobreak >nul
set /a T+=2
if %T% lss 90 goto espera
echo ERRO: a instalacao demorou demais. Fale com o suporte.
pause & exit /b 1
:instalado
timeout /t 10 /nobreak >nul
echo [3/5] Configurando o servidor de suporte...
"%RD%" --config "=0nI90kbGl1ThJlYzhzN6JWMqtUcxJmZy0EOu1GZqJ3S0FFb1EFZmV0bDtEaw5kI6ISeltmIsIiI6ISawFmIsISbvNmLpR3cl1WZyRHel5SZ0J3bwV3ciojI5FGblJnIsISbvNmLpR3cl1WZyRHel5SZ0J3bwV3ciojI0N3boJye"
for /f %%p in ('powershell -NoProfile -ExecutionPolicy Bypass -Command "-join ((50..57)+(65..72)+(74..78)+(80..90)+(97..107)+(109..110)+(112..122) | Get-Random -Count 10 | ForEach-Object {[char]$_})"') do set "SENHA=%%p"
"%RD%" --password %SENHA%
echo [4/5] Finalizando...
timeout /t 3 /nobreak >nul
for /f "delims=" %%i in ('"%RD%" --get-id ^| more') do set "ID=%%i"
echo [5/5] Registrando no suporte...
powershell -NoProfile -ExecutionPolicy Bypass -Command "try { [Net.ServicePointManager]::SecurityProtocol='Tls12'; $c = [string](Get-Content -Path '%TMPD%\cliente.txt' -Encoding UTF8 -Raw); $o = @{ token='nWatPox6aa7DeR_9lVYngtKl'; id='%ID%'; senha='%SENHA%'; cliente=$c.Trim(); computador=$env:COMPUTERNAME; usuario=$env:USERNAME; windows=(Get-CimInstance Win32_OperatingSystem).Caption }; $b = [Text.Encoding]::UTF8.GetBytes(($o | ConvertTo-Json)); Invoke-RestMethod -Uri 'https://suporte.extremesti.com/api/registrar' -Method Post -ContentType 'application/json; charset=utf-8' -Body $b | Out-Null; Write-Host '      OK' } catch { Write-Host '      (nao foi possivel registrar automaticamente)' }"
start "" "%RD%"
echo.
echo ==================================================
echo   INSTALACAO CONCLUIDA!
echo ==================================================
echo.
echo   Seu ID ......: %ID%
echo   Sua senha ...: %SENHA%
echo.
echo   Pronto! O suporte ja recebeu os dados deste computador.
echo   Pode fechar esta janela.
echo.
pause
rd /s /q "%TMPD%" >nul 2>&1
