@echo off
setlocal

cd /d "%~dp0"
set "MGMT_URL=https://vpn.rtmedical.com.br"
set "CONFIG_DIR=%ProgramData%\RTMedicalVPNWin7"
set "CONFIG_FILE=%CONFIG_DIR%\config.json"
set "LOG_FILE=%CONFIG_DIR%\client.log"

if "%~1"=="" goto usage
set "SETUP_KEY=%~1"

net session >nul 2>&1
if errorlevel 1 (
  echo ERRO: abra o cmd.exe como Administrador.
  exit /b 1
)

if not exist "%CONFIG_DIR%" mkdir "%CONFIG_DIR%"

echo Instalando servico Netbird legado...
rtmedical-vpn-win7.exe --config "%CONFIG_FILE%" --log-file "%LOG_FILE%" --management-url "%MGMT_URL%" service install
if errorlevel 1 (
  echo Aviso: service install retornou erro. Tentando continuar com servico existente.
)

echo Iniciando servico...
rtmedical-vpn-win7.exe service start
if errorlevel 1 goto failed

echo Conectando com setup key...
rtmedical-vpn-win7.exe --management-url "%MGMT_URL%" --setup-key "%SETUP_KEY%" up
if errorlevel 1 goto failed

echo.
rtmedical-vpn-win7.exe status
exit /b %ERRORLEVEL%

:usage
echo Uso:
echo   install-win7.bat "SETUP-KEY"
exit /b 2

:failed
echo.
echo Falhou. Verifique se o Windows e 7 SP1 x64 e se o cmd.exe esta como Administrador.
exit /b 1

