#!/bin/bash

# IPTV Mac 构建脚本

echo "🚀 开始构建 IPTV Mac 桌面应用..."

# 检查 Rust
if ! command -v cargo &> /dev/null; then
    echo "❌ Rust 未安装，请先安装:"
    echo "   curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh"
    exit 1
fi

echo "✓ Rust 已安装 ($(cargo --version))"

# 步骤1: 构建 ui
echo "📦 步骤 1/4: 构建 ui 应用..."
cd ui
npm install
npm run build
cp -r public/api dist-web/
cd ..

# 步骤2: 安装 Tauri 依赖
echo "📦 步骤 2/4: 安装 Tauri..."
cd desktop
npm install

# 步骤3: 构建 Mac 应用
echo "🔨 步骤 3/4: 构建 Mac 应用（这可能需要几分钟）..."
npx tauri build

# 步骤4: 检查输出
echo "✅ 步骤 4/4: 检查输出..."
APP_PATH="src-tauri/target/release/bundle/macos/iptv-desktop.app"
DMG_PATH="src-tauri/target/release/bundle/dmg/*.dmg"

if [ -d "$APP_PATH" ]; then
    echo ""
    echo "🎉 构建成功！"
    echo ""
    echo "📱 应用位置:"
    echo "   $APP_PATH"
    echo ""
    echo "📦 安装包位置:"
    ls -lh src-tauri/target/release/bundle/dmg/ 2>/dev/null || echo "   DMG 未生成"
    echo ""
    echo "🚀 运行方式:"
    echo "   1. 双击打开 $APP_PATH"
    echo "   2. 或拖拽到 Applications 文件夹"
else
    echo "❌ 构建失败，请检查错误信息"
    exit 1
fi
