#!/bin/bash
# Mac Mini Docker应用发现脚本

set -e

echo "🔍 开始扫描Mac Mini上的Docker应用..."
echo "======================================"

# 1. 查找所有docker-compose文件
echo "📁 查找docker-compose文件..."
COMPOSE_FILES=$(find ~ -name "docker-compose.yml" -o -name "docker-compose.yaml" 2>/dev/null | grep -v "node_modules" | grep -v ".git" | head -20)

if [ -z "$COMPOSE_FILES" ]; then
    echo "❌ 未找到docker-compose文件"
    exit 1
fi

echo "✅ 找到 $(echo "$COMPOSE_FILES" | wc -l) 个docker-compose文件"

# 2. 分析每个docker-compose文件
echo ""
echo "📊 分析Docker Compose项目..."
echo "--------------------------------------"

for file in $COMPOSE_FILES; do
    dir=$(dirname "$file")
    project_name=$(basename "$dir")
    
    echo "项目: $project_name"
    echo "路径: $dir"
    
    # 检查是否有正在运行的服务
    cd "$dir" 2>/dev/null && docker compose ps --services 2>/dev/null | while read service; do
        status=$(cd "$dir" && docker compose ps $service 2>/dev/null | grep $service)
        if echo "$status" | grep -q "Up"; then
            echo "  ✅ $service: 运行中"
        else
            echo "  ⏸️  $service: 未运行"
        fi
    done
    
    echo "--------------------------------------"
done

# 3. 检查正在运行的独立容器
echo ""
echo "🐳 检查独立Docker容器..."
echo "--------------------------------------"
docker ps --format "table {{.Names}}\t{{.Image}}\t{{.Status}}" | head -10

# 4. 建议的目录结构
echo ""
echo "🏗️ 建议的Docker项目目录结构:"
echo "--------------------------------------"
echo "建议创建: ~/docker-projects/"
echo "├── openclaw/"
echo "│   └── docker-compose.yml"
echo "├── postgres/"
echo "│   └── docker-compose.yml"
echo "└── redis/"
echo "    └── docker-compose.yml"
echo ""
echo "这样便于统一管理Mac Mini上的Docker应用"

# 5. 更新建议
echo ""
echo "🎯 更新docker-apps.yaml的建议:"
echo "--------------------------------------"
echo "在apps部分添加:"
echo ""
echo "  openclaw:"
echo "    path: ~/docker-projects/openclaw"
echo "    server: macmini"
echo "    description: OpenClaw网关服务"
echo "    discovered: $(date +%Y-%m-%d)"
echo ""
echo "然后运行配置编辑工具测试连接"

echo ""
echo "✅ 扫描完成!"