#!/bin/sh

# Check if bun is available (including ~/.bun/bin which may not be in PATH yet)
BUN_CMD=""
if command -v bun >/dev/null 2>&1; then
  BUN_CMD="bun"
elif [ -x "$HOME/.bun/bin/bun" ]; then
  BUN_CMD="$HOME/.bun/bin/bun"
fi

if [ -z "$BUN_CMD" ]; then
  echo "niahere requires Bun runtime."
  printf "Install Bun now? (y/n) "
  read -r answer
  if [ "$answer" = "y" ] || [ "$answer" = "Y" ]; then
    curl -fsSL https://bun.sh/install | bash
    BUN_CMD="$HOME/.bun/bin/bun"
    if [ ! -x "$BUN_CMD" ]; then
      echo "Bun install failed. Install manually: curl -fsSL https://bun.sh/install | bash"
      exit 1
    fi
    echo "Bun installed. Continuing..."
    echo ""
  else
    echo ""
    echo "Install manually: curl -fsSL https://bun.sh/install | bash"
    echo "Then run: nia $*"
    exit 1
  fi
fi

# Resolve the real path of this script (follows symlinks)
REAL="$(realpath "$0" 2>/dev/null)" || REAL="$0"
PACKAGE_DIR="$(dirname "$REAL")/.."
ENTRY="$PACKAGE_DIR/src/cli/index.ts"

if [ ! -f "$ENTRY" ]; then
  ENTRY="$(npm root -g 2>/dev/null)/niahere/src/cli/index.ts"
fi

if [ ! -f "$ENTRY" ]; then
  echo "Error: could not find niahere. Try: npm i -g niahere"
  exit 1
fi

exec "$BUN_CMD" "$ENTRY" "$@"
