#!/bin/bash

# 检查简单文件监控脚本状态

PROJECT_DIR="/Users/chufeng/.openclaw/sanguo_projects/sanguo_quant_live"
PID_FILE="$PROJECT_DIR/simple-watcher.pid"
LOG_FILE="$PROJECT_DIR/simple-watcher.log"

echo "=== Simple File Watcher Status ==="
echo "Project Directory: $PROJECT_DIR"
echo ""

# 检查PID文件
if [ -f "$PID_FILE" ]; then
    pid=$(cat "$PID_FILE")
    echo "PID File: $PID_FILE"
    echo "Recorded PID: $pid"
    
    if ps -p "$pid" > /dev/null 2>&1; then
        echo "Status: ✅ RUNNING (PID: $pid)"
        
        # 获取进程信息
        echo ""
        echo "Process Info:"
        ps -p "$pid" -o pid,ppid,user,%cpu,%mem,etime,command
        
        # 检查打开的文件
        echo ""
        echo "Open Files (lsof):"
        lsof -p "$pid" 2>/dev/null | head -10
        
    else
        echo "Status: ❌ NOT RUNNING (stale PID)"
        echo "Note: PID file exists but process is not running"
    fi
else
    echo "Status: ❌ NOT RUNNING"
    echo "Reason: PID file not found"
fi

echo ""

# 检查日志文件
if [ -f "$LOG_FILE" ]; then
    log_size=$(stat -f%z "$LOG_FILE" 2>/dev/null || stat -c%s "$LOG_FILE" 2>/dev/null)
    echo "Log File: $LOG_FILE"
    echo "Log Size: $log_size bytes"
    
    echo ""
    echo "=== Last 10 Log Entries ==="
    tail -10 "$LOG_FILE" 2>/dev/null || echo "(log file empty or unreadable)"
else
    echo "Log File: Not found"
fi

echo ""

# 检查是否有其他监控进程
echo "=== Other Watcher Processes ==="
echo "Active simple-file-watcher.py processes:"
ps aux | grep "simple-file-watcher.py" | grep -v grep

echo ""

echo "=== Quick Commands ==="
echo "Start watcher:  ./start-simple-watcher.sh"
echo "Stop watcher:   ./stop-simple-watcher.sh"
echo "View logs:      tail -f $LOG_FILE"
echo ""

echo "=== Auto-sync Script ==="
SYNC_SCRIPT="$PROJECT_DIR/auto-sync.sh"
if [ -f "$SYNC_SCRIPT" ] && [ -x "$SYNC_SCRIPT" ]; then
    echo "✅ Sync script exists and is executable"
else
    echo "❌ Sync script missing or not executable"
fi