#!/usr/bin/env bash
set -euo pipefail

repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"

usage() {
  cat <<'USAGE'
ufoo

Monorepo layout:
  ~/.ufoo/                   (clone of ufoo)
    modules/ai-context/              (protocol module)
    modules/ai-resources/            (optional resources)

Usage:
  ufoo doctor
  ufoo status
  ufoo daemon --start|--stop|--status
  ufoo chat
  ufoo init [--modules context[,resources]] [--project <dir>]
  ufoo bus <...>            # JS bus implementation (project-local data)
  ufoo ctx <...>            # JS context commands (doctor|lint|decisions)
  ufoo skills list
  ufoo skills install <name|all> [--codex|--agents|--target <dir>]

Notes:
  - `init` creates/updates <project>/.ai-context and injects CLAUDE.md/AGENTS.md blocks.
  - For Codex notifications, prefer `ufoo bus alert` / `ufoo bus listen` (no IME issues).
USAGE
}

cmd="${1:-}"
shift || true

case "$cmd" in
  ""|--help|-h)
    exec node "$repo_root/bin/ufoo.js" chat
    ;;
  doctor)
    exec node "$repo_root/bin/ufoo.js" doctor "$@"
    ;;
  status)
    exec node "$repo_root/bin/ufoo.js" status "$@"
    ;;
  daemon)
    exec node "$repo_root/bin/ufoo.js" daemon "$@"
    ;;
  chat)
    exec node "$repo_root/bin/ufoo.js" chat
    ;;
  init)
    exec node "$repo_root/bin/ufoo.js" init "$@"
    ;;
  bus)
    # Project-local bus commands. Must be run from within the target project.
    exec node "$repo_root/bin/ufoo.js" bus "$@"
    ;;
  ctx)
    exec node "$repo_root/bin/ufoo.js" ctx "$@"
    ;;
  skills)
    sub="${1:-}"
    shift || true
    exec node "$repo_root/bin/ufoo.js" skills "$sub" "$@"
    ;;
  *)
    echo "Unknown command: $cmd" >&2
    usage >&2
    exit 1
    ;;
esac
