#!/usr/bin/env bash
# Install a parallel `omp-v2` CLI that runs THIS checkout, without replacing global `omp`.
# Use when another agent/tool depends on the published `omp` binary.
#
# Usage (from a built oh-my-copilot checkout):
#   npm run build
#   bash scripts/install-omp-v2.sh
#   omp-v2 version
#   omp-v2 handoff create --json --objective "demo" --next "go"
set -euo pipefail

ROOT="$(cd "$(dirname "$0")/.." && pwd)"
CLI="$ROOT/dist/src/cli.js"
BIN_DIR="${OMP_V2_BIN_DIR:-$HOME/.local/bin}"
TARGET="$BIN_DIR/omp-v2"

if [[ ! -f "$CLI" ]]; then
  echo "✗ missing $CLI — run: npm run build" >&2
  exit 1
fi

mkdir -p "$BIN_DIR"
cat > "$TARGET" <<EOF
#!/usr/bin/env bash
# Auto-generated by scripts/install-omp-v2.sh — points at a specific checkout.
# Global \`omp\` is left alone. Rebuild checkout after code changes.
exec node "$CLI" "\$@"
EOF
chmod +x "$TARGET"

echo "✓ installed $TARGET"
echo "  → $CLI"
if ! command -v omp-v2 >/dev/null 2>&1; then
  echo "⚠ $BIN_DIR is not on PATH. Add to your shell rc:"
  echo "  export PATH=\"$BIN_DIR:\$PATH\""
else
  echo "  omp-v2 → $(command -v omp-v2)"
  omp-v2 version 2>/dev/null | head -3 || true
fi
echo
echo "Leave global omp untouched for other agents. Example:"
echo "  omp-v2 handoff create --json --objective \"test\" --next \"verify\""
