#!/bin/bash

echo "========================================"
echo "   MeshRay 一键启动脚本 (Linux/Mac)"
echo "========================================"
echo ""

# 检查后端是否已编译
if [ ! -f "meshray" ]; then
    echo "[1/3] 正在编译后端..."
    go build -o meshray
    if [ $? -ne 0 ]; then
        echo "❌ 后端编译失败！"
        exit 1
    fi
    echo "✅ 后端编译完成"
else
    echo "[1/2] 后端已编译，跳过"
fi

# 检查前端是否已编译
if [ ! -d "web/dist" ]; then
    echo "[2/3] 正在编译前端..."
    cd web
    npm run build
    if [ $? -ne 0 ]; then
        echo "❌ 前端编译失败！"
        cd ..
        exit 1
    fi
    cd ..
    echo "✅ 前端编译完成"
else
    echo "[2/2] 前端已编译，跳过"
fi

echo ""
echo "[3/3] 正在启动 MeshRay 服务..."
echo ""
echo "========================================"
echo "🚀 MeshRay 启动成功！"
echo "========================================"
echo ""
echo "📱 访问地址：http://localhost:9531"
echo ""
echo "💡 提示:"
echo "  - 首次启动会生成管理员账户"
echo "  - 按 Ctrl+C 停止服务"
echo "  - 日志文件：data/logs/"
echo ""
echo "========================================"
echo ""

# 启动服务
./meshray
