#!/usr/bin/env bash
# PierrotKnowledge2 — this application's own command.
#
# Named after the application rather than `okf`, which is a short generic word
# likely to collide with another tool on the same PATH. Arguments pass straight
# through:
#
#     ./PierrotKnowledge2 Update          check for a new release
#     ./PierrotKnowledge2 Update --apply  install it
#     ./PierrotKnowledge2 ui              open the interface in a browser
#
# Unlike Windows, this shell is case-sensitive, so `Update` is folded to lower
# case before dispatch to keep both spellings working.

set -euo pipefail
ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

if [ $# -gt 0 ]; then
  first="$(printf '%s' "$1" | tr '[:upper:]' '[:lower:]')"
  shift
  set -- "$first" "$@"
fi

if [ -x "$ROOT/build/cli/okf" ]; then
  exec "$ROOT/build/cli/okf" "$@"
fi

if ! command -v bun >/dev/null 2>&1; then
  echo "Bun が見つかりません。https://bun.sh からインストールしてください。" >&2
  exit 1
fi

cd "$ROOT"
exec bun run src/bun/cli/main.ts "$@"
