#!/usr/bin/env bash
# Wrapper for agent-coord. Runs TypeScript directly via Bun.
#
# The canonical event-stream stamps `source: "agent-coord"`; consumers
# (hooks, CLI spawners) treat the path `harnery/bin/agent-coord` as an
# opaque address. Sibling launcher is `harnery/bin/harn` for the main
# harn CLI.
SCRIPT_PATH="${BASH_SOURCE[0]:-$0}"
while [ -L "$SCRIPT_PATH" ]; do
    SCRIPT_DIR="$(cd "$(dirname "$SCRIPT_PATH")" && pwd)"
    LINK_TARGET="$(readlink "$SCRIPT_PATH")"
    case "$LINK_TARGET" in
        /*) SCRIPT_PATH="$LINK_TARGET" ;;
        *) SCRIPT_PATH="$SCRIPT_DIR/$LINK_TARGET" ;;
    esac
done
MODULE_DIR="$(cd "$(dirname "$SCRIPT_PATH")/.." && pwd)"

# Find bun in common install locations if not already on PATH. Hooks fire
# under bash subshells whose PATH may differ from the interactive shell.
if ! command -v bun >/dev/null 2>&1; then
    for candidate in \
        "$HOME/.bun/bin/bun" \
        "/usr/local/bin/bun" \
        "/opt/homebrew/bin/bun"; do
        if [ -x "$candidate" ]; then
            export PATH="$(dirname "$candidate"):$PATH"
            break
        fi
    done
fi

# Bun-first (zero-build TS); Node-with-dist/ fallback. The published package
# ships dist/, so this works on a Node-only host with no Bun installed.
if command -v bun >/dev/null 2>&1; then
    exec bun run "$MODULE_DIR/src/core/agents/cli.ts" "$@"
elif [ -f "$MODULE_DIR/dist/core/agents/cli.js" ]; then
    exec node "$MODULE_DIR/dist/core/agents/cli.js" "$@"
else
    echo "agent-coord: needs Bun or a built dist/. Install Bun (https://bun.sh), or build: npm install && npm run build." >&2
    exit 1
fi
