#!/bin/bash
# Launch Chrome with remote debugging for deep scraping
# Uses a copy of the user's profile to enable debug port

PROFILE_SRC="/home/wdjones/.config/google-chrome"
PROFILE_DBG="/home/wdjones/.config/google-chrome-debug"
PORT=${1:-9222}

# Kill any existing debug chrome
pkill -f "chrome-debug" 2>/dev/null

# Create/sync debug profile
if [ ! -d "$PROFILE_DBG" ]; then
    echo "Creating debug profile (first time, this takes a moment)..."
    cp -r "$PROFILE_SRC" "$PROFILE_DBG"
else
    # Sync cookies and local storage
    cp "$PROFILE_SRC/Default/Cookies" "$PROFILE_DBG/Default/Cookies" 2>/dev/null
    cp -r "$PROFILE_SRC/Default/Local Storage" "$PROFILE_DBG/Default/Local Storage" 2>/dev/null
fi

rm -f "$PROFILE_DBG/SingletonLock" "$PROFILE_DBG/SingletonSocket" "$PROFILE_DBG/SingletonCookie" 2>/dev/null

DISPLAY=:0 /usr/bin/google-chrome-stable --no-sandbox \
    --user-data-dir="$PROFILE_DBG" \
    --remote-debugging-port=$PORT \
    --remote-allow-origins=* \
    https://x.com/home &>/dev/null &

echo "Chrome launched (PID $!, debug port $PORT)"
echo "Waiting for port..."

for i in $(seq 1 15); do
    if curl -s "http://127.0.0.1:$PORT/json" >/dev/null 2>&1; then
        echo "Ready!"
        exit 0
    fi
    sleep 1
done

echo "ERROR: Port $PORT not ready after 15s"
exit 1
