#!/bin/sh
# Vellum CLI shim — finds the nearest project install and runs the review server.
# Installed to ~/.local/bin/vellum (and vellum-review) by install.sh.
set -e

cmd="$(basename "$0")"
case "$cmd" in
  vellum) script_name="vellum-server.mjs" ;;
  vellum-review) script_name="vellum-review.mjs" ;;
  *)
    printf 'vellum: unknown command: %s\n' "$cmd" >&2
    exit 1
    ;;
esac

dir="$PWD"
while [ "$dir" != "/" ]; do
  target="$dir/scripts/$script_name"
  if [ -f "$target" ]; then
    if [ -f "$dir/.vellum.env" ]; then
      # set -a so plain `VELLUM_DIR=...` assignments are EXPORTED into node's
      # environment (sourcing alone only sets shell vars the exec'd child can't see).
      set -a
      # shellcheck disable=SC1090
      . "$dir/.vellum.env"
      set +a
    fi
    cd "$dir"
    exec node "$target" "$@"
  fi
  dir="$(dirname "$dir")"
done

printf 'vellum: no installation found in this directory tree.\n' >&2
printf 'Run the installer from your HyperFrames project root:\n' >&2
printf '  curl -fsSL https://tryvellum.vercel.app/install | sh\n' >&2
exit 1