@echo off
chcp 65001 >nul
set NAME=长安逸动刷入工具
set SRC=app-install.py
title %NAME% - Build

echo ============================================================
echo   %NAME% - Cython Build
echo ============================================================
echo.

where python >nul 2>&1
if errorlevel 1 (
    echo [ERROR] Python not found
    pause
    exit /b
)
for /f "delims=" %%i in ('where python') do set PY=%%i
echo Python: %PY%

echo [1/6] Installing deps...
%PY% -m pip install pyinstaller cython pyzipper -q
if errorlevel 1 (
    %PY% -m pip install pyinstaller cython pyzipper -q -i https://pypi.tuna.tsinghua.edu.cn/simple
)

echo [2/6] Clean...
if exist "dist_cy" rmdir /s /q dist_cy 2>nul
if exist "build" rmdir /s /q build 2>nul
if exist "dist" rmdir /s /q dist 2>nul

echo [3/6] Cython compile...
mkdir dist_cy 2>nul
copy %SRC% dist_cy\_core.py >nul

%PY% -c "open('dist_cy/setup_cython.py','w').write('from setuptools import setup\nfrom Cython.Build import cythonize\nsetup(ext_modules=cythonize(\"_core.py\", compiler_directives={\"language_level\":\"3\"}))\n')"

cd dist_cy
%PY% setup_cython.py build_ext --inplace
if errorlevel 1 (
    cd ..
    echo [WARN] Cython failed, fallback
    goto :NORMAL
)

for %%f in (_core*.pyd) do set PYD=%%f
if "%PYD%"=="" (
    cd ..
    echo [WARN] No pyd, fallback
    goto :NORMAL
)
echo PYD: %PYD%
copy "%PYD%" _core.pyd >nul

%PY% -c "open('launcher.py','w').write('# -*- coding: utf-8 -*-\nfrom _core import main\nmain()\n')"

echo [4/6] Copy resources...
copy ..\adb.exe . >nul
copy ..\AdbWinApi.dll . >nul
copy ..\AdbWinUsbApi.dll . >nul
copy ..\7za.exe . >nul
if exist "..\app.ico" copy "..\app.ico" . >nul

echo [5/6] PyInstaller...
%PY% -m PyInstaller --onefile --windowed --name="%NAME%" --icon=app.ico --add-data "adb.exe;." --add-data "AdbWinApi.dll;." --add-data "AdbWinUsbApi.dll;." --add-data "7za.exe;." --add-binary "_core.pyd;." --hidden-import=queue --hidden-import=threading --hidden-import=tkinter --hidden-import=tkinter.simpledialog --hidden-import=zipfile --hidden-import=json --hidden-import=urllib --hidden-import=urllib.parse --collect-all tkinter --uac-admin launcher.py
if errorlevel 1 (
    cd ..
    echo [ERROR] PyInstaller failed
    pause
    exit /b
)

echo [6/6] Cleanup...
del /q _core.py _core.c _core.pyd %PYD% launcher.py setup_cython.py 2>nul
rmdir /s /q build 2>nul
cd ..
goto :DONE

:NORMAL
cd /d "%~dp0"
echo [INFO] Normal PyInstaller...
%PY% -m PyInstaller --onefile --windowed --name="%NAME%" --icon=app.ico --add-data "adb.exe;." --add-data "AdbWinApi.dll;." --add-data "AdbWinUsbApi.dll;." --add-data "7za.exe;." --hidden-import=queue --hidden-import=threading --hidden-import=tkinter --hidden-import=tkinter.simpledialog --hidden-import=zipfile --hidden-import=json --hidden-import=urllib --hidden-import=urllib.parse --collect-all tkinter --uac-admin %SRC%

:DONE
echo.
echo Done.
if exist "dist_cy\dist\%NAME%.exe" (
    echo Output: dist_cy\dist\%NAME%.exe
) else if exist "dist\%NAME%.exe" (
    echo Output: dist\%NAME%.exe
) else (
    echo Check dist folder
)
pause
