#!/bin/bash
# Very long monitor - check every 60s for up to 30 minutes
echo "Starting long monitor at $(date +%H:%M:%S)"
for i in $(seq 1 30); do
    echo "=== CHECK $i ($(date +%H:%M:%S)) ==="
    tail -2 /home/fabian/comfyui8.log
    
    # Check for output image
    if ls /home/fabian/ComfyUI/output/bc250_test* 2>/dev/null; then
        echo "*** IMAGE SAVED! ***"
        ls -la /home/fabian/ComfyUI/output/
        exit 0
    fi
    
    # Check any other output files
    outcount=$(find /home/fabian/ComfyUI/output/ -type f -not -name '_*' 2>/dev/null | wc -l)
    if [ "$outcount" -gt "0" ]; then
        echo "*** FOUND OUTPUT FILES ($outcount) ***"
        ls -la /home/fabian/ComfyUI/output/
        exit 0
    fi
    
    # Check process alive
    if ! pgrep -f "python.*main.py" > /dev/null; then
        echo "PROCESS DIED!"
        tail -30 /home/fabian/comfyui8.log
        exit 1
    fi
    
    # Show CPU usage
    pid=$(pgrep -f "python.*main.py" | head -1)
    ps -p $pid -o pcpu,pmem,rss --no-headers 2>/dev/null
    
    sleep 60
done
echo "=== TIMEOUT 30min ==="
