#!/usr/bin/env bash
# Dedicated "automation" Chrome for the hypr-cua CDP channel.
#
# A separate, persistent Chrome profile with the DevTools port open on 9222.
# hypr-cua attaches to it while it runs, so logins you make here persist. When it
# isn't running, hypr-cua starts its own private headless Chrome instead. Your
# main Chrome stays uninstrumented either way.
#
# SECURITY: the debug port is loopback-only, but it is a control channel into
# THIS profile: any local process that reaches it can drive it. Keep only what
# you're comfortable automating logged in here.
#
# Usage:
#   bin/hypr-cua-chrome        # launch it, log in, leave it open
#   then in Claude:  cdp_targets   (reports "browser": "automation")
# Override the profile dir or port with HYPR_CUA_CDP_USER_DATA_DIR / HYPR_CUA_CDP_PORT.
set -euo pipefail

PROFILE="${HYPR_CUA_CDP_USER_DATA_DIR:-$HOME/.local/share/hypr-cua-chrome}"
PORT="${HYPR_CUA_CDP_PORT:-9222}"

CHROME=""
for bin in google-chrome-stable google-chrome chromium chromium-browser chrome; do
  if command -v "$bin" >/dev/null 2>&1; then CHROME="$bin"; break; fi
done
: "${CHROME:?no chrome/chromium binary found on PATH}"

mkdir -p "$PROFILE"
exec "$CHROME" \
  --user-data-dir="$PROFILE" \
  --remote-debugging-port="$PORT" \
  --remote-allow-origins="http://127.0.0.1:${PORT}" \
  --no-first-run --no-default-browser-check \
  "$@"
