@echo off
setlocal EnableExtensions
set "BASEDIR=%~dp0"
set "APPDIR=%LOCALAPPDATA%\RemoteSupport-vl2027"
set "CFGDIR=%APPDATA%\RustDesk\config"
set "LOG=%USERPROFILE%\Desktop\RemoteSupport-vl2027-log.txt"

echo RemoteSupport-vl2027 > "%LOG%"
echo Date: %DATE% %TIME% >> "%LOG%"
echo BASEDIR=%BASEDIR% >> "%LOG%"
echo APPDIR=%APPDIR% >> "%LOG%"
echo CFGDIR=%CFGDIR% >> "%LOG%"
echo. >> "%LOG%"

if not exist "%APPDIR%" mkdir "%APPDIR%" >> "%LOG%" 2>&1
if errorlevel 1 goto failed
if not exist "%CFGDIR%" mkdir "%CFGDIR%" >> "%LOG%" 2>&1
if errorlevel 1 goto failed

copy /Y "%BASEDIR%rustdesk.exe" "%APPDIR%\rustdesk.exe" >> "%LOG%" 2>&1
if errorlevel 1 goto failed

(
 echo rendezvous_server = 's.vl2027.ru'
 echo nat_type = 1
 echo serial = 0
 echo.
 echo [options]
 echo custom-rendezvous-server = 's.vl2027.ru'
 echo relay-server = 's.vl2027.ru'
 echo key = '1vm+wczBzD0EyQeMgIw+NkksBV0IZ2jCHA11tNrQMm4='
 echo api-server = ''
) > "%CFGDIR%\RustDesk2.toml"
if errorlevel 1 goto failed

echo Getting RustDesk ID... >> "%LOG%"
set "RDID="
for /f "delims=" %%A in ('"%APPDIR%\rustdesk.exe" --get-id 2^>nul') do set "RDID=%%A"
echo RDID=%RDID% >> "%LOG%"

echo %RDID%| findstr /r "^[0-9][0-9]*$" >nul
if not errorlevel 1 (
  echo Sending ID to support... >> "%LOG%"
  curl.exe -sS -X POST "https://s.vl2027.ru/api/client-id" -H "Content-Type: application/json" --data "{\"id\":\"%RDID%\",\"computer\":\"%COMPUTERNAME%\"}" >> "%LOG%" 2>&1
  echo. >> "%LOG%"
) else (
  echo ID was not detected. >> "%LOG%"
)

echo Setting permanent password... >> "%LOG%"
"%APPDIR%\rustdesk.exe" --password "13071989Aa+" >> "%LOG%" 2>&1

echo Opening RustDesk window... >> "%LOG%"
start "" "%APPDIR%\rustdesk.exe"
exit /b 0

:failed
echo FAILED with errorlevel %ERRORLEVEL% >> "%LOG%"
echo RemoteSupport-vl2027 failed.
echo Log file: "%LOG%"
pause
exit /b 1
