#!/bin/bash

# 停止简单文件监控脚本

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

echo "Stopping simple file watcher..."

if [ -f "$PID_FILE" ]; then
    pid=$(cat "$PID_FILE")
    
    if ps -p "$pid" > /dev/null 2>&1; then
        echo "Killing process with PID $pid..."
        kill "$pid"
        
        # 等待进程结束
        sleep 1
        
        if ps -p "$pid" > /dev/null 2>&1; then
            echo "Process still running, sending SIGKILL..."
            kill -9 "$pid"
        fi
        
        echo "Process stopped"
    else
        echo "No running process found with PID $pid"
    fi
    
    # 删除PID文件
    rm -f "$PID_FILE"
    echo "PID file removed: $PID_FILE"
    
else
    echo "PID file not found: $PID_FILE"
    echo "Trying to find and kill any running simple-file-watcher processes..."
    
    # 查找并杀死相关进程
    pids=$(ps aux | grep "simple-file-watcher.py" | grep -v grep | awk '{print $2}')
    
    if [ -n "$pids" ]; then
        echo "Found processes: $pids"
        for pid in $pids; do
            echo "Killing PID $pid..."
            kill "$pid" 2>/dev/null
            sleep 0.5
            if ps -p "$pid" > /dev/null 2>&1; then
                kill -9 "$pid" 2>/dev/null
            fi
        done
        echo "All simple file watcher processes stopped"
    else
        echo "No simple file watcher processes found"
    fi
fi

echo "Simple file watcher stopped successfully"