@echo off
chcp 65001 >nul
cd /d "%~dp0"
set "NAME=Q05-Lidar_Installer"
set "SRC=Q05-Lidar_Installer.py"
set "APPDIR=%~dp0"
set "ROOT=%~dp0.."
set "TOOLS=%ROOT%\tools"
title %NAME% - Build

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

set "PY=C:\Users\31770\AppData\Local\Programs\Python\Python313\python.exe"
if not exist "%PY%" (
    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 cryptography -q
if errorlevel 1 (
    "%PY%" -m pip install pyinstaller cython pyzipper cryptography -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
if errorlevel 1 (
    echo [ERROR] Copy source failed. Cython build stopped.
    pause
    exit /b 1
)

"%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 [ERROR] Cython build failed. Normal PyInstaller fallback is disabled.
    pause
    exit /b 1
)

for %%f in (_core*.pyd) do set PYD=%%f
if "%PYD%"=="" (
    cd ..
    echo [ERROR] No Cython pyd generated. Normal PyInstaller fallback is disabled.
    pause
    exit /b 1
)
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...
if not exist "%TOOLS%\adb.exe" (
    echo [ERROR] tools\adb.exe not found
    cd ..
    pause
    exit /b 1
)
if not exist "%TOOLS%\AdbWinApi.dll" (
    echo [ERROR] tools\AdbWinApi.dll not found
    cd ..
    pause
    exit /b 1
)
if not exist "%TOOLS%\AdbWinUsbApi.dll" (
    echo [ERROR] tools\AdbWinUsbApi.dll not found
    cd ..
    pause
    exit /b 1
)
if not exist "%TOOLS%\fastboot.exe" (
    echo [ERROR] tools\fastboot.exe not found
    cd ..
    pause
    exit /b 1
)
if not exist "%TOOLS%\7za.exe" (
    echo [ERROR] tools\7za.exe not found at %TOOLS%\7za.exe
    cd ..
    pause
    exit /b 1
)
if not exist "%APPDIR%app.ico" (
    echo [ERROR] app.ico not found at %APPDIR%app.ico
    cd ..
    pause
    exit /b 1
)
copy "%TOOLS%\adb.exe" . >nul
copy "%TOOLS%\AdbWinApi.dll" . >nul
copy "%TOOLS%\AdbWinUsbApi.dll" . >nul
copy "%TOOLS%\7za.exe" . >nul
copy "%TOOLS%\fastboot.exe" . >nul
copy "%APPDIR%app.ico" . >nul
if exist "%APPDIR%resource.dat" copy "%APPDIR%resource.dat" . >nul

echo [5/6] PyInstaller...
set ADD_RESOURCE=
if exist "resource.dat" set ADD_RESOURCE=--add-data "resource.dat;."
"%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-data "fastboot.exe;." %ADD_RESOURCE% --add-binary "_core.pyd;." --hidden-import=queue --hidden-import=threading --hidden-import=tkinter --hidden-import=zipfile --hidden-import=json --hidden-import=urllib --hidden-import=cryptography --collect-all tkinter --collect-all cryptography --uac-admin launcher.py
if errorlevel 1 (
    cd ..
    echo [ERROR] PyInstaller failed
    pause
    exit /b
)

echo [6/7] Copy release data...
if not exist "dist\%NAME%.exe" (
    cd ..
    echo [ERROR] Build output exe not found
    pause
    exit /b 1
)
if not exist "%APPDIR%runtime.dat" (
    cd ..
    echo [ERROR] runtime.dat not found at %APPDIR%runtime.dat
    pause
    exit /b 1
)
if not exist "%APPDIR%Q05_Lidar-package.bin" (
    cd ..
    echo [ERROR] Q05_Lidar-package.bin not found at %APPDIR%Q05_Lidar-package.bin
    pause
    exit /b 1
)
copy "%APPDIR%runtime.dat" "dist\runtime.dat" >nul
if errorlevel 1 (
    cd ..
    echo [ERROR] Copy runtime.dat failed
    pause
    exit /b 1
)
copy "%APPDIR%Q05_Lidar-package.bin" "dist\Q05_Lidar-package.bin" >nul
if errorlevel 1 (
    cd ..
    echo [ERROR] Copy Q05_Lidar-package.bin failed
    pause
    exit /b 1
)

echo [7/7] Cleanup...
del /q _core.py _core.c _core.pyd %PYD% launcher.py setup_cython.py adb.exe AdbWinApi.dll AdbWinUsbApi.dll 7za.exe fastboot.exe resource.dat 2>nul
rmdir /s /q build 2>nul
cd ..
goto :DONE

: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
