#!/usr/bin/env bash
# ONE-TIME seed: clone Ahmed's real, logged-in Chrome profile into the dedicated
# Jarvis profile (~/.jarvis-chrome) so the Chrome that Jarvis drives over CDP
# starts FULLY LOGGED IN — no more re-entering credentials, and it's not the
# "wrong" empty Chrome anymore. It's still a SEPARATE window from your daily
# Chrome (Chrome 136+ forbids remote-debugging on the real default dir), but you
# log in ZERO more times.
#
# Why a copy and not the real dir: Chrome refuses --remote-debugging-port on the
# DEFAULT user-data-dir. A copy at a non-default path is allowed AND carries your
# cookies/logins. On macOS the "Chrome Safe Storage" keychain key is shared
# across all Chrome instances for your user, so the copied (encrypted) cookies
# decrypt fine in the new location.
#
# Chrome MUST be fully quit first, or the cookie/login DBs are locked and the
# copy comes over inconsistent (you'd still be logged out). Pass --auto-quit to
# let this script gracefully quit Chrome for you (it saves your session; reopen
# Chrome normally afterward and your tabs restore).
#
# Usage:
#   bash seed-jarvis-chrome.sh            # requires Chrome already quit
#   bash seed-jarvis-chrome.sh --auto-quit
set -euo pipefail

REAL="$HOME/Library/Application Support/Google/Chrome"
DEST="${JARVIS_CHROME_PROFILE:-$HOME/.jarvis-chrome}"
AUTO_QUIT=0
[ "${1:-}" = "--auto-quit" ] && AUTO_QUIT=1

if [ ! -d "$REAL" ]; then
  echo "Real Chrome profile not found at: $REAL" >&2
  exit 1
fi

# 1) Chrome must be closed for a clean copy of the cookie/login SQLite DBs.
if pgrep -x "Google Chrome" >/dev/null 2>&1; then
  if [ "$AUTO_QUIT" = "1" ]; then
    echo "Quitting Chrome gracefully (your session is saved; tabs restore on reopen)…"
    osascript -e 'quit app "Google Chrome"' || true
    for _ in $(seq 1 20); do
      pgrep -x "Google Chrome" >/dev/null 2>&1 || break
      sleep 0.5
    done
    if pgrep -x "Google Chrome" >/dev/null 2>&1; then
      echo "Chrome didn't quit in time. Quit it manually and re-run." >&2
      exit 1
    fi
  else
    echo "Chrome is running. Quit it fully (Cmd-Q) and re-run," >&2
    echo "or re-run with:  bash seed-jarvis-chrome.sh --auto-quit" >&2
    exit 1
  fi
fi

# 2) Back up any existing Jarvis profile (don't silently destroy prior logins).
if [ -d "$DEST" ]; then
  BAK="${DEST}.bak"
  rm -rf "$BAK"
  mv "$DEST" "$BAK"
  echo "Backed up old Jarvis profile → $BAK"
fi
mkdir -p "$DEST"

# 3) Copy the profile WITHOUT the multi-GB caches (they don't hold logins and
#    just bloat the copy). rsync excludes are matched against the source tree.
echo "Cloning your Chrome profile (excluding caches — this is the ~logged-in state)…"
rsync -a \
  --exclude='*/Cache/' \
  --exclude='*/Code Cache/' \
  --exclude='*/GPUCache/' \
  --exclude='*/DawnGraphiteCache/' \
  --exclude='*/DawnWebGPUCache/' \
  --exclude='*/GraphiteDawnCache/' \
  --exclude='*/Service Worker/CacheStorage/' \
  --exclude='*/Service Worker/ScriptCache/' \
  --exclude='ShaderCache/' \
  --exclude='GrShaderCache/' \
  --exclude='component_crx_cache/' \
  --exclude='extensions_crx_cache/' \
  --exclude='Crashpad/' \
  --exclude='*/Application Cache/' \
  --exclude='Safe Browsing*' \
  "$REAL"/ "$DEST"/

# 4) Strip lock/singleton files so the seeded profile launches clean.
rm -f "$DEST"/Singleton* "$DEST"/RunningChromeVersion "$DEST"/lockfile 2>/dev/null || true

SZ=$(du -sh "$DEST" 2>/dev/null | cut -f1)
echo "Done. Jarvis Chrome profile seeded at $DEST ($SZ)."
echo "Launching it once so you can confirm you're logged in…"
JARVIS_CHROME_PROFILE="$DEST" bash "$(dirname "$0")/chrome-jarvis.sh" || true
echo
echo "Check the window that opened: you should already be signed into your"
echo "Google accounts. If so, you're set — Jarvis will attach to THIS Chrome"
echo "from now on and never ask for credentials again."
