#!/bin/bash

# Cleanup script to kill zombie Chromium processes
# This should be run periodically or when the application shuts down

echo "🧹 Cleaning up zombie Chromium processes..."

# Kill all defunct chromium processes
pkill -f "chromium.*defunct" 2>/dev/null

# Kill any remaining chromium processes in the container
if [ -n "$DOCKER_ENV" ]; then
    echo "Running in Docker environment, cleaning up container processes..."
    # In Docker, we need to be more careful about process cleanup
    ps aux | grep '[c]hromium' | grep -v grep | awk '{print $2}' | xargs -r kill -TERM 2>/dev/null
    sleep 2
    ps aux | grep '[c]hromium' | grep -v grep | awk '{print $2}' | xargs -r kill -KILL 2>/dev/null
else
    echo "Running in host environment, cleaning up host processes..."
    pkill -f "chromium" 2>/dev/null
fi

# Clean up any temporary puppeteer profiles
rm -rf /tmp/puppeteer_dev_chrome_profile-* 2>/dev/null

echo "✅ Cleanup completed"
