#!/bin/bash

# 启动简单文件监控脚本

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

echo "Starting simple file watcher daemon..."

# 检查是否已经运行
if [ -f "$PID_FILE" ]; then
    pid=$(cat "$PID_FILE")
    if ps -p "$pid" > /dev/null 2>&1; then
        echo "Simple file watcher is already running with PID $pid"
        echo "To stop it, run: kill $pid && rm -f $PID_FILE"
        exit 0
    else
        echo "Stale PID file found, removing..."
        rm -f "$PID_FILE"
    fi
fi

# 确保Python脚本可执行
chmod +x "$WATCHER_SCRIPT"

# 运行监控脚本（后台运行）
echo "Starting watcher process..."
nohup python3 "$WATCHER_SCRIPT" > /dev/null 2>&1 &
watcher_pid=$!

# 保存PID
echo $watcher_pid > "$PID_FILE"

echo "Simple file watcher started with PID $watcher_pid"
echo "PID saved to: $PID_FILE"
echo "Log file: $LOG_FILE"
echo ""
echo "To stop the watcher, run:"
echo "  kill $(cat $PID_FILE) && rm -f $PID_FILE"
echo "or use: stop-simple-watcher.sh"
echo ""
echo "To view logs:"
echo "  tail -f $LOG_FILE"
echo ""
echo "Watcher is now monitoring: $PROJECT_DIR"
echo "Files changed will trigger: $PROJECT_DIR/auto-sync.sh"