#!/bin/bash
# scripts/ontoref — thin wrapper for projects consuming the ontoref protocol
# Set ONTOREF_ROOT to the ontoref checkout, then delegate to its entry point.
#
# Usage: ./scripts/ontoref <command> [args...]
# Alias:  Add `alias ontoref="./scripts/ontoref"` to your shell profile.
#
# Required env vars (exported here):
#   ONTOREF_ROOT          — absolute path to ontoref checkout
#   ONTOREF_PROJECT_ROOT  — absolute path to THIS project root

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
readonly SCRIPT_DIR
PROJECT_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
readonly PROJECT_ROOT

# ── Ontoref root ──────────────────────────────────────────────────────────────
# Point to your ontoref checkout. Can be overridden via env var.

ONTOREF_ROOT="${ONTOREF_ROOT:-{{ ontoref_dir }}}"
readonly ONTOREF_ROOT

if [[ ! -f "${ONTOREF_ROOT}/ontoref" ]]; then
  echo "ontoref: cannot find ontoref entry point at ${ONTOREF_ROOT}/ontoref"
  echo "  Set ONTOREF_ROOT to the correct path or update this script."
  exit 1
fi

# ── Export project context ────────────────────────────────────────────────────

export ONTOREF_ROOT
export ONTOREF_PROJECT_ROOT="${PROJECT_ROOT}"

# Prepend project-specific paths so they take priority over any inherited
# NICKEL_IMPORT_PATH. Existing value is preserved as a fallback at the end.
_project_paths="${PROJECT_ROOT}:${PROJECT_ROOT}/.ontology:${PROJECT_ROOT}/adrs:${ONTOREF_ROOT}/adrs:${ONTOREF_ROOT}/ontology/schemas:${ONTOREF_ROOT}"
export NICKEL_IMPORT_PATH="${_project_paths}${NICKEL_IMPORT_PATH:+:${NICKEL_IMPORT_PATH}}"
unset _project_paths

# Preserve caller name for dispatcher help output
export ONTOREF_CALLER="${ONTOREF_CALLER:-./scripts/ontoref}"

exec "${ONTOREF_ROOT}/ontoref" "$@"
