@echo off
setlocal
cd /d "%~dp0"

set "CFGDIR=%APPDATA%\RustDesk\config"
set "SRC=%~dp0RustDesk2.toml"
set "EXE=%~dp0RustDesk-GrantTechSupport.exe"

if not exist "%EXE%" (
  echo.
  echo Grant-Tech Support could not start because the RustDesk application file is missing.
  echo.
  echo Most common cause:
  echo You opened the ZIP and double-clicked this file without extracting it first.
  echo.
  echo Please do this:
  echo 1. Close this window.
  echo 2. Right-click the downloaded grant-tech-support ZIP file.
  echo 3. Choose "Extract All..."
  echo 4. Open the extracted folder.
  echo 5. Double-click Start-Grant-Tech-Support.cmd again.
  echo.
  echo If you already extracted the ZIP, re-download it from Grant-Tech.
  echo.
  pause
  exit /b 1
)

if not exist "%SRC%" (
  echo.
  echo Grant-Tech Support could not start because RustDesk2.toml is missing.
  echo.
  echo Please extract the entire ZIP folder before running this file.
  echo.
  pause
  exit /b 1
)

if not exist "%CFGDIR%" mkdir "%CFGDIR%" >nul 2>nul

if exist "%CFGDIR%\RustDesk2.toml" (
  copy /Y "%CFGDIR%\RustDesk2.toml" "%CFGDIR%\RustDesk2.toml.backup-grant-tech" >nul 2>nul
)

copy /Y "%SRC%" "%CFGDIR%\RustDesk2.toml" >nul
if errorlevel 1 (
  echo.
  echo ERROR: Could not write RustDesk config.
  echo Try right-clicking this file and choosing Run as administrator.
  echo.
  pause
  exit /b 1
)

echo Grant-Tech Support is starting...
echo.
echo When RustDesk opens, give Grant-Tech your ID and one-time password.
echo Do not close this window until RustDesk opens.
start "Grant-Tech Support" "%EXE%"
timeout /t 3 >nul
endlocal
