@echo off
chcp 65001 >nul
title RustDesk o'rnatish — Filial-2 | PC-1 (Kassa)

echo ════════════════════════════════════════════════
echo  RustDesk o'rnatish
echo  PC: Filial-2 | PC-1 (Kassa)
echo ════════════════════════════════════════════════
echo.

REM Admin huquqi tekshiruvi
net session >nul 2>&1
if %errorLevel% neq 0 (
    echo XATO: Admin huquqi kerak!
    echo Sichqoncha o'ng tugmasi -> "Run as administrator"
    pause
    exit /b 1
)

REM Eski RustDesk to'xtatish (agar bor bo'lsa)
echo [1/5] Eski o'rnatishni tozalash...
taskkill /F /IM rustdesk.exe >nul 2>&1
sc stop rustdesk >nul 2>&1
sc delete rustdesk >nul 2>&1
timeout /t 2 /nobreak >nul

REM RustDesk yuklab olish
echo [2/5] RustDesk v1.4.6 yuklash...
powershell -Command "Invoke-WebRequest -Uri 'https://rustdesk.tomoshow.uz/rustdesk-stock.exe' -OutFile '%TEMP%\rustdesk-installer.exe' -UseBasicParsing"
if not exist "%TEMP%\rustdesk-installer.exe" (
    echo XATO: yuklab bo'lmadi
    pause & exit /b 1
)

REM Silent install
echo [3/5] Windows xizmati sifatida o'rnatish...
"%TEMP%\rustdesk-installer.exe" --silent-install
timeout /t 8 /nobreak >nul

REM Config yozish — server va kalit
echo [4/5] Server sozlamalari...
set CFG_USER=%APPDATA%\RustDesk\config
set CFG_SYS=C:\Windows\ServiceProfiles\LocalService\AppData\Roaming\RustDesk\config
if not exist "%CFG_USER%" mkdir "%CFG_USER%"
if not exist "%CFG_SYS%" mkdir "%CFG_SYS%"

(
echo rendezvous_server = ''
echo nat_type = 1
echo serial = 3
echo.
echo [options]
echo custom-rendezvous-server = 'rustdesk.tomoshow.uz'
echo key = 'BEpFtnftZV1N5AxJDR6aG3nxPWCgcTga+aEaEKtutW0='
echo relay-server = 'rustdesk.tomoshow.uz'
echo api-server = ''
echo codec-preference = 'h264'
echo enable-direct-ip-access = 'Y'
echo allow-auto-record-incoming = 'N'
echo verification-method = 'use-permanent-password'
echo approve-mode = 'password'
) > "%CFG_USER%\RustDesk2.toml"
copy /Y "%CFG_USER%\RustDesk2.toml" "%CFG_SYS%\RustDesk2.toml" >nul

REM Permanent password o'rnatish
echo [5/5] Permanent password o'rnatish...
"C:\Program Files\RustDesk\rustdesk.exe" --password "N28TAHHl2ad9"
timeout /t 2 /nobreak >nul

REM Service restart
sc stop rustdesk >nul 2>&1
timeout /t 2 /nobreak >nul
sc start rustdesk >nul 2>&1
timeout /t 3 /nobreak >nul

REM ID olish
for /f "tokens=*" %%i in ('"C:\Program Files\RustDesk\rustdesk.exe" --get-id 2^>nul') do set MYID=%%i

echo.
echo ════════════════════════════════════════════════
echo  TAYYOR!
echo ════════════════════════════════════════════════
echo  PC nomi:  Filial-2 | PC-1 (Kassa)
echo  RustDesk ID: %MYID%
echo  Parol:    N28TAHHl2ad9
echo.
echo  Bu ma'lumotlarni admin'ga yuboring.
echo  RustDesk endi Windows xizmati sifatida ishlaydi.
echo  Kompyuter o'chsa ham, qaytadan yoqilganda ishlaveradi.
echo ════════════════════════════════════════════════
pause
