#!/bin/bash
# Windows 交叉编译脚本
# 使用 x86_64-w64-mingw32 工具链

set -e

echo "🚀 ReadFlow Windows 交叉编译脚本"
echo "=================================="

# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'

log_info() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

log_warn() {
    echo -e "${YELLOW}[WARN]${NC} $1"
}

log_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# 检查是否安装了 mingw 工具链
check_mingw() {
    if command -v x86_64-w64-mingw32-gcc &> /dev/null; then
        log_info "MinGW 工具链已安装"
        return 0
    else
        log_error "MinGW 工具链未安装"
        log_info "请运行以下命令安装:"
        echo "  brew install mingw-w64  # macOS"
        echo "  sudo apt-get install mingw-w64  # Linux"
        return 1
    fi
}

# 添加 Windows 目标
add_target() {
    log_info "添加 Windows 目标..."
    rustup target add x86_64-pc-windows-gnu
}

# 编译
build() {
    log_info "开始编译 Windows 版本..."
    
    # 设置交叉编译器
    export CARGO_TARGET_X86_64_PC_WINDOWS_GNU_LINKER=x86_64-w64-mingw32-gcc
    
    # 编译
    cargo build --release --target x86_64-pc-windows-gnu
    
    log_info "编译完成！"
}

# 打包
package() {
    log_info "打包 Windows 版本..."
    
    local version="0.2.0"
    local build_dir="target/x86_64-pc-windows-gnu/release"
    local package_dir="dist/readflow-${version}-windows-x86_64"
    
    mkdir -p "${package_dir}"
    
    # 复制可执行文件
    cp "${build_dir}/readflow.exe" "${package_dir}/"
    
    # 创建 README
    cat > "${package_dir}/README.txt" << EOF
ReadFlow v${version} for Windows

快速开始:
1. 解压到任意目录
2. 双击运行 readflow.exe

功能:
- EPUB/MOBI/Markdown/PDF 阅读
- 20+ 编程语言语法高亮
- 双语翻译对照
- 笔记与书签系统
- 插件系统
- 主题商店

技术支持：damai@foshanhuiya.com
Gitea: http://192.168.120.110:4000/damai/readflow
EOF
    
    # 创建 ZIP
    cd dist
    zip -r "readflow-${version}-windows-x86_64.zip" "readflow-${version}-windows-x86_64/"
    cd ..
    
    log_info "打包完成：dist/readflow-${version}-windows-x86_64.zip"
}

# 上传到 Release
upload_release() {
    log_info "上传到 Gitea Release..."
    
    local version="0.2.0"
    local token="884162096b7f331c6fb236217d00a9452e34d4aa"
    local file="dist/readflow-${version}-windows-x86_64.zip"
    
    # 获取 Release ID
    local release_id=$(curl -s "http://192.168.120.110:4000/api/v1/repos/damai/readflow/releases/tags/v${version}" \
        -H "Authorization: token ${token}" | jq -r '.id')
    
    if [ "$release_id" == "null" ] || [ -z "$release_id" ]; then
        log_error "Release v${version} 不存在"
        return 1
    fi
    
    log_info "Release ID: ${release_id}"
    
    # 上传文件
    curl -L -X POST "http://192.168.120.110:4000/api/v1/repos/damai/readflow/releases/${release_id}/assets?name=readflow-${version}-windows-x86_64.zip" \
        -H "Authorization: token ${token}" \
        -H "Content-Type: application/zip" \
        --data-binary @"${file}"
    
    log_info "上传完成！"
}

# 主函数
main() {
    log_info "开始 Windows 交叉编译..."
    
    # 检查工具链
    if ! check_mingw; then
        log_warn "跳过编译，请安装 MinGW 后重试"
        exit 1
    fi
    
    # 添加目标
    add_target
    
    # 编译
    build
    
    # 打包
    package
    
    # 上传 (可选)
    if [ "$1" == "--upload" ]; then
        upload_release
    fi
    
    log_info "🎉 Windows 打包完成！"
    log_info "文件位置：dist/readflow-0.2.0-windows-x86_64.zip"
}

# 运行
main "$@"
