#!/usr/bin/env bash
# Chromium for an agent's Karma runs: CHROME_BIN points here, so
# `ng test --watch=false --browsers=ChromeHeadless` (the develop skill's
# frontend unit-test step) works inside the agent sandbox.
#
# Two things the stock launcher cannot do on this box: there is no system Chrome
# (the only browser is the Playwright Chromium in ~/.cache/ms-playwright, which
# the agent Playwright MCP uses too), and Ubuntu 23.10+ refuses Chromium's own
# sandbox for an unprivileged user ("No usable sandbox!"), so it needs
# --no-sandbox, as the Playwright MCP entry has. The agent AppArmor profile is
# the sandbox here. The newest Chromium revision in the cache wins.
set -euo pipefail

cache="${PLAYWRIGHT_BROWSERS_PATH:-$HOME/.cache/ms-playwright}"
chrome="$(ls -d "$cache"/chromium-*/chrome-linux*/chrome 2>/dev/null | sort -V | tail -n 1 || true)"
if [ -z "$chrome" ]; then
  echo "chrome-nosandbox: no Playwright Chromium under $cache (install it: npx playwright install chromium)" >&2
  exit 127
fi
exec "$chrome" --no-sandbox "$@"
