@echo off
chcp 65001 >nul
title Server Manager - 打包便携版
for %%I in ("%~dp0..\..") do set "ROOT=%%~fI"
set "SRC=%ROOT%\src"
set "APP_NAME=ServerManager"
pushd "%SRC%" || (
    echo [错误] 未找到 src 目录: %SRC%
    exit /b 1
)

echo ============================================================
echo          Server Manager - 打包便携版 EXE
echo ============================================================
echo.

:: 检查 Python 是否安装
python --version >nul 2>&1
if errorlevel 1 (
    echo [错误] 未找到 Python，请先安装 Python 3.8+
    pause
    exit /b 1
)

echo [1/5] 检查并安装依赖...
pip install PyQt6 pyinstaller Pillow -q

if errorlevel 1 (
    echo [错误] 安装依赖失败
    pause
    exit /b 1
)

echo [2/5] 生成图标文件...
python "%ROOT%\resources\icon\convert_icon.py"
if not exist "%ROOT%\resources\icon\icon.ico" (
    echo [警告] 无法生成 ICO 图标，将使用默认图标
    set "ICON_PARAM="
) else (
    set "ICON_PARAM=--icon %ROOT%\resources\icon\icon.ico"
)

echo [3/5] 清理旧的构建文件...
if exist "dist" rd /s /q dist
if exist "build" rd /s /q build
if exist "%APP_NAME%.spec" del /q "%APP_NAME%.spec"

echo [4/5] 开始打包（单文件版本）...
pyinstaller --noconfirm --onefile --windowed ^
    --name "%APP_NAME%" ^
    %ICON_PARAM% ^
    --add-data "%ROOT%\resources\icon;icon" ^
    --hidden-import "PyQt6.QtWidgets" ^
    --hidden-import "PyQt6.QtCore" ^
    --hidden-import "PyQt6.QtGui" ^
    --hidden-import "entrypoints.cli.main" ^
    --hidden-import "entrypoints.cli.extended" ^
    --hidden-import "entrypoints.gui.bootstrap" ^
    --hidden-import "mcp.server" ^
    --hidden-import "mcp.tools" ^
    --hidden-import "models.app_config" ^
    --hidden-import "services.commands" ^
    --hidden-import "services.server_creator" ^
    --hidden-import "services.hot_update" ^
    --hidden-import "services.rg_search" ^
    --hidden-import "requests" ^
    --hidden-import "bsdiff4" ^
    --collect-all "PyQt6" ^
    --collect-all "bsdiff4" ^
    entrypoints\main.py

if errorlevel 1 (
    echo [错误] 打包失败，尝试使用目录模式...
    
    pyinstaller --noconfirm --onedir --windowed ^
        --name "%APP_NAME%" ^
        %ICON_PARAM% ^
        --add-data "%ROOT%\resources\icon;icon" ^
        --hidden-import "PyQt6.QtWidgets" ^
        --hidden-import "PyQt6.QtCore" ^
        --hidden-import "PyQt6.QtGui" ^
        --hidden-import "entrypoints.cli.main" ^
        --hidden-import "entrypoints.cli.extended" ^
        --hidden-import "entrypoints.gui.bootstrap" ^
        --hidden-import "mcp.server" ^
        --hidden-import "mcp.tools" ^
        --hidden-import "models.app_config" ^
        --hidden-import "services.commands" ^
        --hidden-import "services.server_creator" ^
        --hidden-import "services.hot_update" ^
        --hidden-import "services.rg_search" ^
        --hidden-import "requests" ^
        --hidden-import "bsdiff4" ^
        --collect-all "bsdiff4" ^
        entrypoints\main.py
    
    if errorlevel 1 (
        echo [错误] 打包失败
        pause
        exit /b 1
    )
)

echo [5/5] 打包完成！

echo.
echo ============================================================
if exist "dist\%APP_NAME%.exe" (
    echo 打包成功！单文件版本: dist\%APP_NAME%.exe
) else if exist "dist\%APP_NAME%\%APP_NAME%.exe" (
    echo 打包成功！目录版本: dist\%APP_NAME%\%APP_NAME%.exe
)
echo ============================================================
echo.

pause
popd
