#!/bin/bash

# Meilisearch 启动脚本
# 此脚本提供多种方式启动 Meilisearch 服务

echo "==================================="
echo "Meilisearch 启动脚本"
echo "==================================="
echo ""

# 检查 Docker 是否可用
if command -v docker &> /dev/null && docker info &> /dev/null; then
    echo "✓ 检测到 Docker 已安装并运行"
    echo ""
    echo "使用 Docker Compose 启动 Meilisearch..."
    docker compose up -d meilisearch
    
    if [ $? -eq 0 ]; then
        echo ""
        echo "✓ Meilisearch 服务已启动！"
        echo ""
        echo "访问地址: http://localhost:7700"
        echo "主密钥: your-master-key-change-this-in-production"
        echo ""
        echo "查看日志: docker compose logs -f meilisearch"
        echo "停止服务: docker compose down"
    else
        echo ""
        echo "✗ 启动失败，请检查 Docker 配置"
        exit 1
    fi
elif command -v meilisearch &> /dev/null; then
    echo "✓ 检测到本地安装的 Meilisearch"
    echo ""
    echo "启动 Meilisearch 服务..."
    echo ""
    echo "请在新终端窗口运行以下命令："
    echo "meilisearch --master-key=\"your-master-key-change-this-in-production\""
    echo ""
    echo "或者在后台运行："
    echo "nohup meilisearch --master-key=\"your-master-key-change-this-in-production\" > storage/logs/meilisearch.log 2>&1 &"
else
    echo "✗ 未检测到 Docker 或 Meilisearch"
    echo ""
    echo "请选择以下安装方式之一："
    echo ""
    echo "1. 使用 Docker（推荐）："
    echo "   - 安装 Docker Desktop: https://www.docker.com/products/docker-desktop"
    echo "   - 启动 Docker Desktop"
    echo "   - 重新运行此脚本"
    echo ""
    echo "2. 使用 Homebrew（macOS）："
    echo "   brew install meilisearch"
    echo ""
    echo "3. 手动下载："
    echo "   curl -L https://install.meilisearch.com | sh"
    echo ""
    echo "详细说明请查看: docs/MEILISEARCH_SETUP.md"
    exit 1
fi
