#!/usr/bin/env bash
# orch — top-level subcommand dispatcher.
#
# Issue #189 friction points 1 + 3: this is a vendor-first shim around the
# Go binary at cmd/orch/. Built-in Go subcommands (tell / ask / peek /
# spy / spawn / migrate-aliases) are handled in-process; unknown
# subcommands are forwarded to a sibling `orch-<sub>` binary on PATH
# (preserves the legacy dispatch model for orch up / orch down / etc.).
#
# Resolution order:
#   1. vendor/orch (shipped by `npm install -g @agent-ops/orch` via
#      scripts/postinstall.js — fetched from the matching GitHub Release).
#   2. .bin/orch (lazy-built from cmd/orch on a dev checkout where go.mod
#      is in this repo tree).
#
# Same vendor-first pattern as bin/orch-subtree / bin/orch-workflow.
set -euo pipefail

# Resolve symlinks (npm installs bin entries as symlinks under <prefix>/bin/).
# Without this, vendor/ lookups resolve relative to the symlink's directory
# instead of the package's actual installed-location under lib/node_modules.
script_path="${BASH_SOURCE[0]}"
while [ -L "$script_path" ]; do
    link=$(readlink "$script_path")
    if [[ "$link" = /* ]]; then
        script_path="$link"
    else
        script_path="$(dirname "$script_path")/$link"
    fi
done
script_dir=$(cd "$(dirname "$script_path")" && pwd)
pkg_root=$(cd "$script_dir/.." && pwd)

# 1. Prefer the vendored binary from the GH Release.
vendored="$pkg_root/vendor/orch"
if [ -x "$vendored" ]; then
    exec "$vendored" "$@"
fi

# 2. Fallback: dev-checkout lazy-build. Walk up from this script looking for
#    go.mod; if we find it (and `go` is on PATH), build into .bin and exec.
build_root="$script_dir"
while [ ! -f "$build_root/go.mod" ]; do
    parent=$(dirname "$build_root")
    if [ "$parent" = "$build_root" ]; then
        echo "orch: no vendored binary at $vendored and no go.mod above $script_dir" >&2
        echo "      re-run \`npm install -g @agent-ops/orch\` (or unset ORCH_SKIP_DOWNLOAD if you set it)" >&2
        exit 1
    fi
    build_root="$parent"
done

src_dir="$build_root/cmd/orch"
build_dir="$build_root/.bin"
built="$build_dir/orch"

needs_build=0
if [ ! -x "$built" ]; then
    needs_build=1
else
    # Rebuild if any .go file under cmd/orch, internal/registry, or
    # internal/synadia is newer than the cached binary.
    if find "$src_dir" "$build_root/internal/registry" "$build_root/internal/synadia" \
        -name '*.go' -newer "$built" -print -quit 2>/dev/null | grep -q .; then
        needs_build=1
    fi
fi

if [ "$needs_build" -eq 1 ]; then
    if ! command -v go >/dev/null 2>&1; then
        echo "orch: no vendored binary and 'go' not on PATH; cannot build $src_dir" >&2
        exit 1
    fi
    mkdir -p "$build_dir"
    (cd "$build_root" && go build -o "$built" ./cmd/orch) \
        || { echo "orch: build failed" >&2; exit 1; }
fi

exec "$built" "$@"
