#!/usr/bin/env bash
#
# _smoke-root.sh  -  resolve the pipeline tree for a smoke that must run in BOTH
# the repo checkout and an end-user install.
#
# Why this exists: the repo and an install have different shapes.
#
#   repo checkout          install (~/.claude or ~/.copilot)
#   ------------------     --------------------------------
#   pipeline/scripts/      scripts/
#   pipeline/schemas/      schemas/
#   pipeline/multi-agent-refs/  multi-agent-refs/
#   pipeline/commands/     commands/
#   package.json           (absent)
#
# A smoke that hardcodes `REPO_ROOT="$(dirname "$0")/../.."` and then reads
# `$REPO_ROOT/pipeline/...` resolves to `$HOME/pipeline/...` once installed, so
# every path check fails. Whoever ran it sees a red report and concludes the
# install is broken. Source this file instead and read the MA_* variables.
#
# Usage (from any script in pipeline/scripts/):
#   . "$(dirname "${BASH_SOURCE[0]}")/_smoke-root.sh"
#   grep -q needle "$MA_REFS/phases/phase-4-review.md"
#
# Exports:
#   MA_LAYOUT     "repo" | "install"
#   MA_ROOT       repo root, or the install root (~/.claude)
#   MA_SCRIPTS    directory holding this script
#   MA_SCHEMAS    JSON schemas
#   MA_REFS       multi-agent-refs
#   MA_COMMANDS   slash-command tree
#   MA_SKILLS     skills tree
#
# Every exported directory is verified to exist; a missing one is left empty so
# the caller can skip that check rather than report a false failure.

MA_SCRIPTS="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# The repo is the only layout with pipeline/ beside package.json. Checking both
# rules out an install that happens to sit next to an unrelated pipeline/ dir.
_ma_candidate="$(cd "$MA_SCRIPTS/../.." && pwd)"
if [ -f "$_ma_candidate/package.json" ] && [ -d "$_ma_candidate/pipeline" ]; then
  MA_LAYOUT="repo"
  MA_ROOT="$_ma_candidate"
  _ma_base="$_ma_candidate/pipeline"
else
  MA_LAYOUT="install"
  MA_ROOT="$(cd "$MA_SCRIPTS/.." && pwd)"
  _ma_base="$MA_ROOT"
fi
unset _ma_candidate

_ma_dir() { [ -d "$1" ] && echo "$1" || echo ""; }

MA_SCHEMAS="$(_ma_dir "$_ma_base/schemas")"
MA_REFS="$(_ma_dir "$_ma_base/multi-agent-refs")"
MA_COMMANDS="$(_ma_dir "$_ma_base/commands")"
MA_SKILLS="$(_ma_dir "$_ma_base/skills")"
unset _ma_base

export MA_LAYOUT MA_ROOT MA_SCRIPTS MA_SCHEMAS MA_REFS MA_COMMANDS MA_SKILLS
