name: 'Cleanup Processes (Linux)'
description: 'Kill all lemonade, Python, and llama-server processes on Linux'
runs:
  using: "composite"
  steps:
    - name: Kill zombie processes
      shell: bash
      run: |
        echo "========================================"
        echo "Cleaning up processes on Linux..."
        echo "========================================"

        # Kill processes aggressively by pattern
        kill_by_pattern() {
            local pattern=$1
            local description=$2

            echo ""
            echo "Killing $description processes..."

            # Find and kill in one shot with SIGKILL
            pkill -9 -f "$pattern" 2>/dev/null && echo "  Killed processes matching: $pattern" || echo "  No $description processes found"
        }

        # Kill by exact name (even more aggressive)
        kill_by_exact_name() {
            local name=$1

            if pgrep -x "$name" > /dev/null 2>&1; then
                echo "Killing $name (exact match)..."
                pkill -9 -x "$name"
                sleep 0.5
                # Verify it's dead
                if pgrep -x "$name" > /dev/null 2>&1; then
                    echo "  WARNING: $name still running after kill!"
                else
                    echo "  Successfully killed $name"
                fi
            fi
        }

        # Kill llama processes
        kill_by_pattern "llama-server" "llama-server"
        kill_by_pattern "llama-cli" "llama-cli"
        kill_by_pattern "llama-bench" "llama-bench"

        # Kill everything lemonade-related
        kill_by_pattern "lemonade" "lemonade"

        # Kill Python processes (this is aggressive - be careful!)
        kill_by_pattern "python" "Python"

        # Additional pass: kill by exact process names
        echo ""
        echo "Second pass: killing by exact name..."
        for proc_name in lemond lemonade-tray llama-server llama-cli llama-bench; do
            kill_by_exact_name "$proc_name"
        done

        # Wait for processes to die
        sleep 2

        # FINAL VERIFICATION - show what's still alive
        echo ""
        echo "=== Verification: checking for remaining processes ==="
        if pgrep -f "lemonade|llama" > /dev/null 2>&1; then
            echo "WARNING: Some processes still alive:"
            ps aux | grep -E "lemonade|llama" | grep -v grep | grep -v "cleanup-processes" || true

            # NUCLEAR OPTION: Kill them with extreme prejudice
            echo "Using nuclear option..."
            pkill -9 -f "llama" 2>/dev/null || true
            pkill -9 -f "lemonade" 2>/dev/null || true
            sleep 1

            # Check again
            if pgrep -f "lemonade|llama" > /dev/null 2>&1; then
                echo "ERROR: Processes STILL alive after nuclear option!"
                ps aux | grep -E "lemonade|llama" | grep -v grep || true
            else
                echo "Nuclear option successful"
            fi
        else
            echo "All target processes terminated successfully"
        fi

        echo ""
        echo "Cleaning up lock and PID files..."
        TMP_ROOT="${RUNNER_TEMP:-$PWD}"
        rm -f "$TMP_ROOT"/lemonade*.lock 2>/dev/null || true
        rm -f "$TMP_ROOT"/lemonade*.pid 2>/dev/null || true
        rm -f "$TMP_ROOT"/lemond.pid 2>/dev/null || true
        if [ -n "${XDG_RUNTIME_DIR:-}" ]; then
          rm -f "${XDG_RUNTIME_DIR}/lemonade/lemonade"*.lock 2>/dev/null || true
          rm -f "${XDG_RUNTIME_DIR}/lemonade/lemonade"*.pid 2>/dev/null || true
        fi
        echo "Lock and PID files cleaned."

        # Clean up log files
        echo ""
        echo "Cleaning up log files..."
        rm -f "$TMP_ROOT"/lemonade*.log 2>/dev/null || true
        rm -f "$TMP_ROOT"/lemonade-server*.log 2>/dev/null || true
        rm -f "$TMP_ROOT"/lemond*.log 2>/dev/null || true
        if [ -n "${XDG_RUNTIME_DIR:-}" ]; then
          rm -f "${XDG_RUNTIME_DIR}/lemonade/lemonade"*.log 2>/dev/null || true
        fi
        echo "Log files cleaned."

        # Clean up installation directories
        echo ""
        echo "Cleaning up lemonade installation directories..."
        if [ -d "$HOME/.local/share/lemonade-server" ]; then
            rm -rf "$HOME/.local/share/lemonade-server" 2>/dev/null || true
            echo "Removed: $HOME/.local/share/lemonade-server"
        else
            echo "No lemonade-server directory found in ~/.local/share/"
        fi

        echo ""
        echo "========================================"
        echo "Process cleanup complete!"
        echo "========================================"
