#!/bin/bash
# Cairnquire - Install Monitor
# Sets up the progress monitor to run in the background

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_DIR="/Users/tim/developer/cairnquire"
MONITOR_SCRIPT="$SCRIPT_DIR/monitor.sh"
PID_FILE="$PROJECT_DIR/.monitor.pid"
LOG_FILE="$PROJECT_DIR/monitor.log"

echo "Cairnquire - Monitor Installation"
echo "===================================="
echo ""

# Check if monitor is already running
if [[ -f "$PID_FILE" ]]; then
    OLD_PID=$(cat "$PID_FILE")
    if kill -0 "$OLD_PID" 2>/dev/null; then
        echo "Monitor is already running (PID: $OLD_PID)"
        echo ""
        echo "Options:"
        echo "  1) Stop existing monitor and start fresh"
        echo "  2) View current status"
        echo "  3) Exit"
        echo ""
        read -p "Choose option [1-3]: " choice
        
        case $choice in
            1)
                echo "Stopping existing monitor..."
                kill "$OLD_PID" 2>/dev/null || true
                rm -f "$PID_FILE"
                ;;
            2)
                echo ""
                cat "$PROJECT_DIR/docs/STATUS.md" 2>/dev/null || echo "No status file yet."
                exit 0
                ;;
            3)
                exit 0
                ;;
            *)
                echo "Invalid option"
                exit 1
                ;;
        esac
    else
        # Stale PID file
        rm -f "$PID_FILE"
    fi
fi

# Make sure monitor script exists
if [[ ! -f "$MONITOR_SCRIPT" ]]; then
    echo "ERROR: Monitor script not found at $MONITOR_SCRIPT"
    exit 1
fi

# Ensure it's executable
chmod +x "$MONITOR_SCRIPT"

# Create necessary directories
mkdir -p "$PROJECT_DIR/docs"
mkdir -p "$PROJECT_DIR/scripts"

echo "Starting monitor in background..."
echo "  Script: $MONITOR_SCRIPT"
echo "  Log:    $LOG_FILE"
echo "  PID:    $PID_FILE"
echo ""

# Start monitor in background
nohup "$MONITOR_SCRIPT" > "$LOG_FILE" 2>&1 &
MONITOR_PID=$!

# Save PID
echo $MONITOR_PID > "$PID_FILE"

sleep 2

# Check if it's actually running
if kill -0 "$MONITOR_PID" 2>/dev/null; then
    echo "Monitor started successfully!"
    echo "  PID: $MONITOR_PID"
    echo ""
    echo "Commands:"
    echo "  View status:    cat docs/STATUS.md"
    echo "  View log:       tail -f monitor.log"
    echo "  Stop monitor:   kill $(cat "$PID_FILE")"
    echo "  Restart:        ./scripts/install-monitor.sh"
    echo ""
    echo "The monitor will:"
    echo "  - Check for filesystem changes every 10 minutes"
    echo "  - Update docs/STATUS.md with current progress"
    echo "  - Auto-exit if no changes detected (Codex idle/finished)"
    echo "  - Provide code review notes for Codex"
    echo ""
    
    # Generate initial status
    echo "Generating initial status report..."
    "$MONITOR_SCRIPT" > /dev/null 2>&1 || true
    
    if [[ -f "$PROJECT_DIR/docs/STATUS.md" ]]; then
        echo ""
        echo "Current Status:"
        echo "---------------"
        head -30 "$PROJECT_DIR/docs/STATUS.md"
    fi
else
    echo "ERROR: Monitor failed to start. Check $LOG_FILE"
    rm -f "$PID_FILE"
    exit 1
fi
