#!/usr/bin/env bash
# Routes in-box URL opens to `agentbox-ctl open`, which opens the link in the
# box's own Chromium (agent-browser, visible via `agentbox screen`) and asks
# the host user — in the footer/dashboard — whether to also open it on the
# host. This script is installed at /usr/local/bin (earlier in PATH than
# xdg-utils' /usr/bin/xdg-open, which it is also symlinked over) and is the
# box's $BROWSER, so `xdg-open`, Claude Code's OAuth flow, `gh`,
# `git web--browse`, python's webbrowser, etc. all land here.
#
# Only http(s) URLs are routed. Anything else (a file path, another scheme)
# falls through to the real xdg-open, which resolves it locally in the box.

set -uo pipefail

target="${1:-}"

case "$target" in
  http://* | https://*)
    exec agentbox-ctl open "$target"
    ;;
  *)
    if [[ -x /usr/bin/xdg-open ]]; then
      exec /usr/bin/xdg-open "$@"
    fi
    echo "agentbox-open: not an http(s) URL: $target" >&2
    exit 1
    ;;
esac
