#!/usr/bin/env bash
# npx @agfpd/iapeer bootstrap.
#
# Runs the iapeer foundation CLI from the package SOURCE via bun (the foundation is a
# bun project; there is no precompiled JS shipped). A BARE invocation (`npx
# @agfpd/iapeer`, no args) runs the `install` verb — compile the stable
# ~/.local/bin/iapeer binary + the global ~/.iapeer scaffold + the daemon plist —
# implementing the `npx @agfpd/iapeer` install contract. Any args pass straight through
# to the CLI (`npx @agfpd/iapeer onboard`, `… create <p>`, …).
set -euo pipefail

PKG_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
CLI="$PKG_ROOT/src/cli/index.ts"

if ! command -v bun >/dev/null 2>&1; then
  echo "iapeer: 'bun' is required on PATH to run the foundation CLI — install it from https://bun.sh" >&2
  exit 1
fi

if [ "$#" -eq 0 ]; then
  # Bare bootstrap → install the foundation (binary + scaffold + daemon plist).
  exec bun "$CLI" install
fi

exec bun "$CLI" "$@"
