#!/usr/bin/env bash
# H·AI·K·U MCP server entry.
#
# Production (marketplace install under ~/.claude/plugins/marketplaces/...):
#   exec the bundled, self-contained `bin/haiku.mjs` under plain `node`.
#   The bundle has every runtime dep (including @sentry/node) tree-rolled
#   in via esbuild — it does NOT depend on the consumer's node_modules.
#   Built by CI, committed to git, shipped to users.
#
# Dev (everywhere else):
#   exec source `packages/haiku/src/main.ts` via `bun` (preferred) or
#   `npx tsx`, so edits hot-reload on the next MCP restart without
#   rebuilding. Devs never run the bundle locally.
#
# The marketplace path is the discriminator because marketplace installs
# are full git clones — `packages/haiku/src/main.ts` exists there too —
# so `[ -f source ]` alone misroutes production into the dev branch and
# crashes on missing project-local @sentry/node when `npx tsx` resolves
# against the consumer's node_modules.
#
# HAIKU_PROD=1 forces bundle mode anywhere (CI smoke tests, debugging
# bundle behavior in a dev checkout). HAIKU_DEV=1 forces source mode
# (custom plugin install paths that should still hot-reload).
set -euo pipefail
HERE="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
BUNDLE="$HERE/haiku.mjs"
SOURCE_ENTRY="$HERE/../../packages/haiku/src/main.ts"

# Detect production install location. `*/.claude/plugins/*` covers the
# marketplaces dir too — `*` in bash `case` patterns matches `/`.
IS_PROD=""
case "$HERE" in
  */.claude/plugins/*|*/.claude/mcpb/*)
    IS_PROD=1
    ;;
esac
[ "${HAIKU_PROD:-}" = "1" ] && IS_PROD=1
[ "${HAIKU_DEV:-}" = "1" ] && IS_PROD=""

# Dev path: source via bun/tsx.
if [ -z "$IS_PROD" ] && [ -f "$SOURCE_ENTRY" ]; then
  if command -v bun >/dev/null 2>&1; then
    exec bun "$SOURCE_ENTRY" "$@"
  fi
  if command -v npx >/dev/null 2>&1; then
    exec npx tsx "$SOURCE_ENTRY" "$@"
  fi
  echo "haiku: dev checkout at $SOURCE_ENTRY but neither 'bun' nor 'npx' is on PATH." >&2
  echo "Install bun (https://bun.sh) or node, or set HAIKU_PROD=1 to use the bundle." >&2
  exit 1
fi

# Production path: bundle under node.
if [ -f "$BUNDLE" ]; then
  exec node "$BUNDLE" "$@"
fi

# Last-resort fallback for an unbuilt dev checkout that ended up flagged
# as production (e.g. someone symlinked it into ~/.claude/plugins/).
if [ -f "$SOURCE_ENTRY" ]; then
  echo "haiku: bundle missing at $BUNDLE — falling back to source. Run 'npm run build -w @haiku/haiku' or unset HAIKU_PROD." >&2
  if command -v bun >/dev/null 2>&1; then
    exec bun "$SOURCE_ENTRY" "$@"
  fi
  if command -v npx >/dev/null 2>&1; then
    exec npx tsx "$SOURCE_ENTRY" "$@"
  fi
  echo "haiku: bundle missing and neither 'bun' nor 'npx' is available for source fallback." >&2
  exit 1
fi

echo "haiku: cannot locate bundle ($BUNDLE) or source ($SOURCE_ENTRY)." >&2
exit 1
