#!/bin/sh
# OrderBee skill installer — cross-agent.
# Installs the OrderBee skill (SKILL.md + helper + API reference) into any
# SKILL.md-compatible agent: Claude Code, Codex, Copilot CLI, Gemini CLI,
# OpenClaw, Hermes. Other SKILL.md agents (Pi, Antigravity, Claude apps) load
# skills their own way — install with --dir into their skills directory.
#
# Usage:
#   curl -fsSL https://orderbee.app/install.sh | sh
#   curl -fsSL https://orderbee.app/install.sh | sh -s -- --agent codex
#   curl -fsSL https://orderbee.app/install.sh | sh -s -- --dir ~/somewhere/skills
#
# With no flags it installs into every supported agent whose config dir exists.
set -eu

NAME="orderbee"
# Override for testing against a local/staging backend: ORDERBEE_INSTALL_BASE=http://localhost:8788
BASE_URL_DEFAULT="${ORDERBEE_INSTALL_BASE:-https://orderbee.app}"

AGENT=""
DIR=""
while [ $# -gt 0 ]; do
  case "$1" in
    --agent) AGENT="${2:-}"; shift 2 ;;
    --dir)   DIR="${2:-}"; shift 2 ;;
    -h|--help)
      echo "Usage: install.sh [--agent claude|codex|copilot|gemini|openclaw|hermes] [--dir PATH]"
      exit 0 ;;
    *) echo "Unknown argument: $1" >&2; exit 1 ;;
  esac
done

agent_skills_dir() {
  case "$1" in
    claude)   echo "$HOME/.claude/skills" ;;
    codex)    echo "$HOME/.codex/skills" ;;
    copilot)  echo "$HOME/.copilot/skills" ;;
    gemini)   echo "$HOME/.gemini/skills" ;;
    openclaw) echo "$HOME/.openclaw/skills" ;;
    hermes)   echo "$HOME/.hermes/skills" ;;
    *)        echo "" ;;
  esac
}

# Resolve install targets.
TARGETS=""
if [ -n "$DIR" ]; then
  TARGETS="$DIR/$NAME"
elif [ -n "$AGENT" ]; then
  base="$(agent_skills_dir "$AGENT")"
  [ -n "$base" ] || { echo "Unknown agent: $AGENT (use claude|codex|copilot|gemini|openclaw|hermes, or --dir)" >&2; exit 1; }
  TARGETS="$base/$NAME"
else
  for a in claude codex copilot gemini openclaw hermes; do
    [ -d "$HOME/.$a" ] && TARGETS="$TARGETS $(agent_skills_dir "$a")/$NAME"
  done
  if [ -z "$TARGETS" ]; then
    echo "No supported agent config dir found (~/.claude, ~/.codex, ~/.copilot, ~/.gemini, ~/.openclaw, ~/.hermes)." >&2
    echo "Re-run with --agent <name> or --dir <path>." >&2
    exit 1
  fi
fi

# Download the skill payload from the OrderBee backend (no GitHub / repo access needed).
TMP="$(mktemp -d)"
trap 'rm -rf "$TMP"' EXIT
echo "Downloading OrderBee skill..."
if ! curl -fsSL "$BASE_URL_DEFAULT/orderbee-skill.tar.gz" -o "$TMP/skill.tgz"; then
  echo "Download failed. Could not reach $BASE_URL_DEFAULT." >&2
  exit 1
fi
tar -xzf "$TMP/skill.tgz" -C "$TMP"
SRC="$TMP/skill"
[ -d "$SRC" ] || { echo "Skill payload malformed (no skill/ directory)." >&2; exit 1; }

# Install into each target.
for t in $TARGETS; do
  mkdir -p "$(dirname "$t")"
  rm -rf "$t"
  cp -R "$SRC" "$t"
  echo "Installed -> $t"
done

cat <<EOF

Done. Set your environment (or let the skill self-signup on first run):
  export ORDERBEE_BASE_URL=$BASE_URL_DEFAULT
  export ORDERBEE_API_KEY=...   # get a key at $BASE_URL_DEFAULT

Then ask your agent to order from a local business.
EOF
