#!/bin/bash
#
# Agent监控启动脚本
# 用法: ./start_agent.sh <agent-name>
#

AGENT_NAME=$1

if [ -z "$AGENT_NAME" ]; then
    echo "Usage: $0 <agent-name>"
    echo "Available agents: zhangfei guanyu zhaoyun jiangwei"
    exit 1
fi

echo "=============================================="
echo "🚀 启动 $AGENT_NAME Agent监控"
echo "=============================================="

# 创建任务目录
mkdir -p .tasks/$AGENT_NAME

# 启动监控循环
echo "📋 监控已启动，开始检查任务..."
echo "⏰ 每30秒检查一次任务目录"
echo ""

while true; do
    # 检查是否有新任务
    TASK_FILES=$(find .tasks/$AGENT_NAME -name "*.task" 2>/dev/null | sort)
    
    if [ -n "$TASK_FILES" ]; then
        for TASK_FILE in $TASK_FILES; do
            echo "=================================================="
            echo "📥 收到新任务: $TASK_FILE"
            echo "=================================================="
            
            # 读取任务
            TASK_NAME=$(grep -E "^#.*TASK_NAME:" "$TASK_FILE" | sed 's/.*TASK_NAME: //')
            TASK_DESC=$(grep -E "^#.*TASK_DESC:" "$TASK_FILE" | sed 's/.*TASK_DESC: //')
            
            echo "📋 任务名称: $TASK_NAME"
            echo "📝 任务描述: $TASK_DESC"
            echo ""
            
            # 标记为进行中
            mv "$TASK_FILE" "$TASK_FILE.processing"
            
            # 执行任务 - 这里需要用户手动执行
            echo "👉 请手动执行任务内容，完成后:"
            echo "   1. 完成工作"
            echo "   2. mv $TASK_FILE.processing $TASK_FILE.done"
            echo "   3. git add/commit/push"
            echo "   4. 通知发起者"
            echo ""
        done
    fi
    
    # 等待30秒
    sleep 30
done
