@echo off
title MC AutoClicker - Build
echo.
echo === MC AutoClicker - Build EXE ===
echo.

python --version >nul 2>nul
if errorlevel 1 goto NO_PYTHON
echo [OK] Python found.
goto INSTALL

:NO_PYTHON
echo [->] Python not found. Installing via winget...
winget install -e --id Python.Python.3.12 --silent --accept-package-agreements --accept-source-agreements
python --version >nul 2>nul
if errorlevel 1 goto PYTHON_FAIL
echo [OK] Python installed.

:INSTALL
echo.
echo [->] Installing packages...
python -m pip install pywin32 keyboard pyinstaller --timeout 120 -q
if errorlevel 1 goto TRY_MIRROR1
echo [OK] Packages installed.
goto BUILD

:TRY_MIRROR1
echo [!] Timeout. Trying mirror 1...
python -m pip install pywin32 keyboard pyinstaller -i https://pypi.tuna.tsinghua.edu.cn/simple --timeout 120 -q
if errorlevel 1 goto TRY_MIRROR2
echo [OK] Packages installed via mirror 1.
goto BUILD

:TRY_MIRROR2
echo [!] Trying mirror 2...
python -m pip install pywin32 keyboard pyinstaller -i https://mirrors.ustc.edu.cn/pypi/web/simple --timeout 120 -q
if errorlevel 1 goto DEPS_FAIL
echo [OK] Packages installed via mirror 2.

:BUILD
echo.
echo [->] Building MC_AutoClicker.exe...
echo.
if exist "dist\MC_AutoClicker.exe" del /f "dist\MC_AutoClicker.exe" >nul
if exist "build" rmdir /s /q "build" >nul
if exist "MC_AutoClicker.spec" del /f "MC_AutoClicker.spec" >nul
python -m PyInstaller --onefile --noconsole --name "MC_AutoClicker" --clean mc_autoclicker.py
if errorlevel 1 goto BUILD_FAIL
if not exist "dist\MC_AutoClicker.exe" goto BUILD_FAIL
copy /y "dist\MC_AutoClicker.exe" "MC_AutoClicker.exe" >nul
echo.
echo ==========================================
echo  DONE! MC_AutoClicker.exe is ready.
echo  Location: %CD%\MC_AutoClicker.exe
echo ==========================================
echo.
explorer /select,"%CD%\MC_AutoClicker.exe"
pause
exit /b 0

:PYTHON_FAIL
echo [FAIL] Cannot install Python automatically.
echo        Go to: https://www.python.org/downloads/
echo        Check "Add Python to PATH" during install!
pause
exit /b 1

:DEPS_FAIL
echo [FAIL] All mirrors failed. Check internet connection.
pause
exit /b 1

:BUILD_FAIL
echo [FAIL] Build failed. See errors above.
pause
exit /b 1
