@echo off
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
set alfanum=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
set rustdesk_pw=
for /L %%b in (1, 1, 12) do (
    set /A rnd_num=!RANDOM! %% 62
    for %%c in (!rnd_num!) do (
        set rustdesk_pw=!rustdesk_pw!!alfanum:~%%c,1!
    )
)
set rustdesk_cfg=Qfi0TT1YGVJZXMhNWUFNUbudXN5h2bRdUdVdHVrZlcDFWMUNUNyoVa5FVNEREViojI5V2aiwiInJ3buMnbkt2Y1RmL0NXdylnbhlXbv8iOzBHd0hmI6ISawFmIsIiI6ISehxWZyJCLigTMuATNx4iNyIjLyMTMiojI0N3boJye
set installer_name=rustdesk-1.4.6-x86_64.exe
set download_url=https://github.com/rustdesk/rustdesk/releases/download/1.4.6/rustdesk-1.4.6-x86_64.exe

net session >nul 2>&1
if %errorLevel% neq 0 (
    powershell -Command "Start-Process '%~f0' -Verb RunAs"
    exit /b
)

cd /d "%~dp0"
if not exist "%installer_name%" (
    curl -L "%download_url%" -o "%installer_name%"
)

"%installer_name%" --silent-install
timeout /t 12 >nul

cd "C:\Program Files\RustDesk"
rustdesk.exe --install-service
timeout /t 8 >nul
rustdesk.exe --config %rustdesk_cfg%
rustdesk.exe --password %rustdesk_pw%

for /f "delims=" %%i in ('rustdesk.exe --get-id ^| more') do set rustdesk_id=%%i

echo ----------------------------------------
echo RustDesk instalado y configurado
echo ID: %rustdesk_id%
echo Password: %rustdesk_pw%
echo ----------------------------------------
pause
