#!/bin/bash

# Quick Chromium cleanup script for live trading

echo "🧹 Cleaning up Chromium processes..."

# First pass - graceful termination
pkill -TERM -f 'chromium.*--remote-debugging-port' 2>/dev/null || true
pkill -TERM -f 'chromium.*--user-data-dir' 2>/dev/null || true  
pkill -TERM -f '/usr/lib/chromium/chromium' 2>/dev/null || true
pkill -TERM -f chrome 2>/dev/null || true

# Wait 2 seconds for graceful shutdown
sleep 2

# Second pass - force kill
pkill -9 -f 'chromium.*--remote-debugging-port' 2>/dev/null || true
pkill -9 -f 'chromium.*--user-data-dir' 2>/dev/null || true  
pkill -9 -f '/usr/lib/chromium/chromium' 2>/dev/null || true
pkill -9 -f chrome 2>/dev/null || true

# Clean up zombie processes
pkill -9 -f 'defunct' 2>/dev/null || true

# Kill specific process types
pkill -9 -f 'type=zygote' 2>/dev/null || true
pkill -9 -f 'type=gpu-process' 2>/dev/null || true
pkill -9 -f 'type=utility' 2>/dev/null || true

echo "📊 Remaining Chromium processes:"
ps aux | grep -i chromium | grep -v grep | grep -v gotenberg || echo "✅ No Chromium processes found (excluding gotenberg)"

echo "✅ Chromium cleanup completed"
