#!/usr/bin/env bash
# Lope installer — registers skills and commands in each AI CLI host's
# native command/skill directory using the format that host expects.
#
# Usage: ./install [--host claude|codex|gemini|opencode|cursor|vibe|qwen|pi|all]
#                  [--with-headroom]
#
# The optional Headroom MCP integration is OFF by default. Its Docker-native
# fallback fetches and executes a remote install script; lope never runs that
# automatically — enable Headroom with --with-headroom (or LOPE_INSTALL_HEADROOM=1)
# and the remote-script step still asks for consent.
#
# Compatible with bash 3.2+ (stock macOS).

set -euo pipefail

LOPE_DIR="$(cd "$(dirname "$0")" && pwd)"
ENGINE_ROOT="${LOPE_ENGINE_ROOT:-$LOPE_DIR}"
PYTHON_BIN="${LOPE_PYTHON:-python3}"
SKILLS_DIR="$LOPE_DIR/skills"
COMMANDS_DIR="$LOPE_DIR/commands"
LOPE_INSTALL_BACKUP_DIR="${LOPE_INSTALL_BACKUP_DIR:-${XDG_STATE_HOME:-$HOME/.local/state}/lope/install-backups/$(date -u +%Y%m%dT%H%M%SZ)-$$}"
export LOPE_INSTALL_BACKUP_DIR
# Headroom is opt-in: its Docker fallback executes a remote install script, so
# it must never run on the default path. Enable with --with-headroom / =1.
INSTALL_HEADROOM="${LOPE_INSTALL_HEADROOM:-0}"
HEADROOM_AGENT_FILTER="${LOPE_HEADROOM_AGENT:-}"
HEADROOM_PROXY_URL_VALUE="${HEADROOM_PROXY_URL:-http://127.0.0.1:8787}"
HEADROOM_DOCKER_INSTALL_URL_VALUE="${HEADROOM_DOCKER_INSTALL_URL:-https://raw.githubusercontent.com/chopratejas/headroom/main/scripts/install.sh}"
HEADROOM_DOCKER_NATIVE=0

# Parse args (POSIX-compatible, no bash 4+ features)
HOST="all"
SKIP_NEXT=0
ARGS=("$@")
for i in "${!ARGS[@]}"; do
  if [ "$SKIP_NEXT" = "1" ]; then SKIP_NEXT=0; continue; fi
  arg="${ARGS[$i]}"
  case "$arg" in
    --host=*)
      HOST="${arg#--host=}"
      ;;
    --host)
      next_i=$((i + 1))
      if [ "$next_i" -lt "${#ARGS[@]}" ]; then
        HOST="${ARGS[$next_i]}"
        SKIP_NEXT=1
      fi
      ;;
    --skip-headroom|--no-headroom)
      INSTALL_HEADROOM="0"
      ;;
    --with-headroom|--headroom)
      INSTALL_HEADROOM="1"
      ;;
    --headroom-agent=*)
      HEADROOM_AGENT_FILTER="${arg#--headroom-agent=}"
      ;;
    --headroom-agent)
      next_i=$((i + 1))
      if [ "$next_i" -lt "${#ARGS[@]}" ]; then
        HEADROOM_AGENT_FILTER="${ARGS[$next_i]}"
        SKIP_NEXT=1
      fi
      ;;
    claude|codex|gemini|opencode|cursor|vibe|qwen|pi|all)
      HOST="$arg"
      ;;
  esac
done

# Preserve existing host content before replacement; never discard custom skills.
replace_target() {
  local target="$1"
  if [ -e "$target" ] || [ -L "$target" ]; then
    local relative="${target#"$HOME"/}"
    local backup="$LOPE_INSTALL_BACKUP_DIR/$relative"
    if [ "$relative" = "$target" ] || [ -e "$backup" ] || [ -L "$backup" ]; then
      echo "ERROR: cannot safely back up $target" >&2
      return 1
    fi
    mkdir -p "$(dirname "$backup")"
    mv "$target" "$backup"
    echo "  preserved: $backup"
  fi
}

# ─── Per-host install functions ────────────────────────────────

# Claude Code: skills/ dir, one subdir per skill containing SKILL.md
install_claude() {
  local dir="$HOME/.claude/skills"
  mkdir -p "$dir"
  for skill in "$SKILLS_DIR"/lope "$SKILLS_DIR"/lope-*/ "$SKILLS_DIR"/using-lope; do
    skill="${skill%/}"
    [ -d "$skill" ] || continue
    local target="$dir/$(basename "$skill")"
    replace_target "$target"
    ln -sf "$(cd "$skill" && pwd)" "$target"
    echo "  claude:   $target -> $skill"
  done
}

# Codex: skills/ dir for skill content.
# Codex 0.130 resolves symlinked external skill dirs under a plugin namespace
# (e.g. /lope:lope-negotiate), so copy Lope skills into ~/.codex/skills
# to restore the plain /lope-negotiate-style skill names.
install_codex() {
  local dir="$HOME/.codex/skills"
  mkdir -p "$dir"
  for skill in "$SKILLS_DIR"/lope "$SKILLS_DIR"/lope-*/ "$SKILLS_DIR"/using-lope; do
    skill="${skill%/}"
    [ -d "$skill" ] || continue
    local target="$dir/$(basename "$skill")"
    replace_target "$target"
    mkdir -p "$target"
    cp -R "$skill"/. "$target"/
    echo "  codex:    $target <= $skill"
  done
}

# Gemini CLI: commands/<namespace>/<command>.toml format.
# We link every commands/lope/*.toml file under ~/.gemini/commands/lope/
# so the user gets /lope:<verb> namespaced slash commands.
install_gemini() {
  local dir="$HOME/.gemini/commands/lope"
  mkdir -p "$dir"
  for toml in "$COMMANDS_DIR"/lope/*.toml; do
    [ -f "$toml" ] || continue
    local target="$dir/$(basename "$toml")"
    replace_target "$target"
    ln -sf "$(cd "$(dirname "$toml")" && pwd)/$(basename "$toml")" "$target"
    echo "  gemini:   $target -> $toml"
  done
}

# OpenCode: ~/.config/opencode/commands/ (PLURAL — singular "command" is wrong)
# with flat markdown files that MUST have YAML frontmatter including `agent:`.
# We ship pre-authored wrapper files at commands/opencode/*.md that have the
# right frontmatter shape and delegate to `lope <mode>` via bash in their body.
install_opencode() {
  local dir="$HOME/.config/opencode/commands"
  mkdir -p "$dir"
  # Clean up v0.3.1 mistake (singular "command" dir) if it exists
  local stale="$HOME/.config/opencode/command"
  if [ -d "$stale" ]; then
    for f in "$stale"/lope*.md "$stale"/using-lope.md; do
      if [ -e "$f" ] || [ -L "$f" ]; then replace_target "$f"; fi
    done
  fi
  for md in "$COMMANDS_DIR"/opencode/*.md; do
    [ -f "$md" ] || continue
    local target="$dir/$(basename "$md")"
    replace_target "$target"
    ln -sf "$(cd "$(dirname "$md")" && pwd)/$(basename "$md")" "$target"
    echo "  opencode: $target -> $md"
  done
}

# Cursor: ~/.cursor/agents/ with flat markdown files.
install_cursor() {
  local dir="$HOME/.cursor/agents"
  mkdir -p "$dir"
  for skill in "$SKILLS_DIR"/lope "$SKILLS_DIR"/lope-*/ "$SKILLS_DIR"/using-lope; do
    skill="${skill%/}"
    [ -d "$skill" ] || continue
    local name="$(basename "$skill")"
    local src="$skill/SKILL.md"
    local target="$dir/$name.md"
    [ -f "$src" ] || continue
    replace_target "$target"
    ln -sf "$src" "$target"
    echo "  cursor:   $target -> $src"
  done
}

# Mistral Vibe: ~/.vibe/skills/ with standard SKILL.md directory format.
install_vibe() {
  local dir="$HOME/.vibe/skills"
  # Only install if vibe is actually on disk — never create ~/.vibe from scratch
  [ -d "$HOME/.vibe" ] || { echo "  vibe:     (skipped — ~/.vibe not present)"; return 0; }
  mkdir -p "$dir"
  for skill in "$SKILLS_DIR"/lope "$SKILLS_DIR"/lope-*/ "$SKILLS_DIR"/using-lope; do
    skill="${skill%/}"
    [ -d "$skill" ] || continue
    local target="$dir/$(basename "$skill")"
    replace_target "$target"
    ln -sf "$(cd "$skill" && pwd)" "$target"
    echo "  vibe:     $target -> $skill"
  done
}

# Qwen Code: ~/.qwen/skills/ with standard SKILL.md directory format.
install_qwen() {
  local dir="$HOME/.qwen/skills"
  [ -d "$HOME/.qwen" ] || { echo "  qwen:     (skipped — ~/.qwen not present)"; return 0; }
  mkdir -p "$dir"
  for skill in "$SKILLS_DIR"/lope "$SKILLS_DIR"/lope-*/ "$SKILLS_DIR"/using-lope; do
    skill="${skill%/}"
    [ -d "$skill" ] || continue
    local target="$dir/$(basename "$skill")"
    replace_target "$target"
    ln -sf "$(cd "$skill" && pwd)" "$target"
    echo "  qwen:     $target -> $skill"
  done
}

# pi: shares ~/.agents/skills/ with the broader @agents skill ecosystem
# (Codex, Qwen, and other multi-CLI consumers). Standard SKILL.md dir format.
install_pi() {
  local dir="$HOME/.agents/skills"
  [ -d "$HOME/.pi" ] || { echo "  pi:       (skipped — ~/.pi not present)"; return 0; }
  mkdir -p "$dir"
  for skill in "$SKILLS_DIR"/lope "$SKILLS_DIR"/lope-*/ "$SKILLS_DIR"/using-lope; do
    skill="${skill%/}"
    [ -d "$skill" ] || continue
    local target="$dir/$(basename "$skill")"
    replace_target "$target"
    ln -sf "$(cd "$skill" && pwd)" "$target"
    echo "  pi:       $target -> $skill"
  done
}

# Headroom: MCP compression layer for bulky tool/validator output.
# Best-effort: never fail the Lope install because Python/PyPI/agent config is unavailable.
find_headroom_python() {
  for candidate in python3.12 python3.11 python3.10 python3; do
    command -v "$candidate" >/dev/null 2>&1 || continue
    if "$candidate" - <<'PYCHECK' >/dev/null 2>&1
import sys
raise SystemExit(0 if sys.version_info >= (3, 10) else 1)
PYCHECK
    then
      command -v "$candidate"
      return 0
    fi
  done
  return 1
}

is_headroom_docker_native() {
  local hr
  hr="$(command -v headroom 2>/dev/null || true)"
  [ -n "${hr:-}" ] || return 1
  grep -q 'HEADROOM_IMAGE_DEFAULT' "$hr" 2>/dev/null
}

find_headroom_bash() {
  local candidate bashbin
  for candidate in "${BASH:-}" bash /usr/local/bin/bash /opt/homebrew/bin/bash; do
    [ -n "$candidate" ] || continue
    if [ -x "$candidate" ]; then
      bashbin="$candidate"
    else
      bashbin="$(command -v "$candidate" 2>/dev/null || true)"
    fi
    [ -n "${bashbin:-}" ] || continue
    if "$bashbin" -c '((BASH_VERSINFO[0] > 4 || (BASH_VERSINFO[0] == 4 && BASH_VERSINFO[1] >= 3)))' >/dev/null 2>&1; then
      printf '%s\n' "$bashbin"
      return 0
    fi
  done
  return 1
}

install_headroom_docker_native() {
  command -v docker >/dev/null 2>&1 || { echo "  headroom: Docker fallback unavailable (docker not found)"; return 1; }
  docker version >/dev/null 2>&1 || { echo "  headroom: Docker fallback unavailable (docker not available)"; return 1; }
  command -v curl >/dev/null 2>&1 || { echo "  headroom: Docker fallback unavailable (curl not found)"; return 1; }

  local bashbin tmp
  bashbin="$(find_headroom_bash || true)"
  [ -n "${bashbin:-}" ] || { echo "  headroom: Docker fallback unavailable (bash >=4.3 not found)"; return 1; }

  # Trust boundary: this fallback fetches and executes a remote install script
  # (unpinned, main-tracking). Never run it automatically — require interactive
  # consent, or an explicit env opt-in for non-interactive/automated installs.
  if [ "${LOPE_HEADROOM_ALLOW_REMOTE_EXEC:-0}" != "1" ]; then
    if [ -t 0 ] && [ -t 1 ]; then
      echo "  headroom: the Docker-native install fetches and runs a remote script from:"
      echo "      $HEADROOM_DOCKER_INSTALL_URL_VALUE"
      printf "  headroom: download and execute it? [y/N] "
      read -r _hr_ans
      case "$_hr_ans" in
        y|Y|yes|YES) ;;
        *) echo "  headroom: skipped Docker-native fallback (declined)"; return 1 ;;
      esac
    else
      echo "  headroom: skipping Docker-native fallback — it would execute an unpinned remote script."
      echo "            Set LOPE_HEADROOM_ALLOW_REMOTE_EXEC=1 to allow it non-interactively."
      return 1
    fi
  fi

  tmp="${TMPDIR:-/tmp}/headroom-install.$$"
  rm -f "$tmp"
  echo "  headroom: installing Docker-native wrapper via $HEADROOM_DOCKER_INSTALL_URL_VALUE"
  if ! curl -fsSL "$HEADROOM_DOCKER_INSTALL_URL_VALUE" -o "$tmp"; then
    rm -f "$tmp"
    return 1
  fi
  if ! "$bashbin" "$tmp"; then
    rm -f "$tmp"
    return 1
  fi
  rm -f "$tmp"
  export PATH="$HOME/.local/bin:$HOME/bin:$PATH"
  if command -v headroom >/dev/null 2>&1; then
    HEADROOM_DOCKER_NATIVE=1
    return 0
  fi
  return 1
}

write_headroom_docker_mcp_shim() {
  mkdir -p "$HOME/.local/bin"
  local shim="$HOME/.local/bin/headroom-mcp-stdio"
  cat >"$shim" <<'EOF'
#!/usr/bin/env bash
set -euo pipefail

HEADROOM_IMAGE="${HEADROOM_DOCKER_IMAGE:-ghcr.io/chopratejas/headroom:latest}"
HEADROOM_CONTAINER_HOME="${HEADROOM_CONTAINER_HOME:-/tmp/headroom-home}"
HEADROOM_HOST_HOME="${HOME:?}"
HEADROOM_PROXY_URL_VALUE="${HEADROOM_PROXY_URL:-http://127.0.0.1:8787}"

mkdir -p \
  "${HEADROOM_HOST_HOME}/.headroom" \
  "${HEADROOM_HOST_HOME}/.claude" \
  "${HEADROOM_HOST_HOME}/.codex" \
  "${HEADROOM_HOST_HOME}/.gemini"

user_args=()
if command -v id >/dev/null 2>&1; then
  user_args=(--user "$(id -u):$(id -g)")
fi

docker run --rm -i \
  "${user_args[@]}" \
  -w /workspace \
  --env "HOME=${HEADROOM_CONTAINER_HOME}" \
  --env "PYTHONUNBUFFERED=1" \
  --env "HEADROOM_WORKSPACE_DIR=${HEADROOM_CONTAINER_HOME}/.headroom" \
  --env "HEADROOM_CONFIG_DIR=${HEADROOM_CONTAINER_HOME}/.headroom/config" \
  --env "HEADROOM_PROXY_URL=${HEADROOM_PROXY_URL_VALUE}" \
  -v "${PWD}:/workspace" \
  -v "${HEADROOM_HOST_HOME}/.headroom:${HEADROOM_CONTAINER_HOME}/.headroom" \
  -v "${HEADROOM_HOST_HOME}/.claude:${HEADROOM_CONTAINER_HOME}/.claude" \
  -v "${HEADROOM_HOST_HOME}/.codex:${HEADROOM_CONTAINER_HOME}/.codex" \
  -v "${HEADROOM_HOST_HOME}/.gemini:${HEADROOM_CONTAINER_HOME}/.gemini" \
  --entrypoint headroom \
  "${HEADROOM_IMAGE}" \
  mcp serve "$@"
EOF
  chmod +x "$shim"
  printf '%s\n' "$shim"
}

register_headroom_claude_docker_mcp() {
  local shim config
  shim="$(write_headroom_docker_mcp_shim)"
  if command -v claude >/dev/null 2>&1; then
    claude mcp remove headroom -s user >/dev/null 2>&1 || true
    claude mcp add headroom -s user -e "HEADROOM_PROXY_URL=$HEADROOM_PROXY_URL_VALUE" -- "$shim" >/dev/null || return 1
    echo "  headroom: registered Docker-native MCP for Claude via $shim"
    return 0
  fi

  mkdir -p "$HOME/.claude"
  config="$HOME/.claude/mcp.json"
  if command -v python3 >/dev/null 2>&1; then
    HEADROOM_SHIM="$shim" HEADROOM_PROXY_URL_VALUE="$HEADROOM_PROXY_URL_VALUE" HEADROOM_CONFIG="$config" python3 - <<'PY' || return 1
import json, os
from pathlib import Path

path = Path(os.environ["HEADROOM_CONFIG"])
try:
    data = json.loads(path.read_text()) if path.exists() else {}
except Exception:
    data = {}
servers = data.setdefault("mcpServers", {})
servers["headroom"] = {
    "command": os.environ["HEADROOM_SHIM"],
    "args": [],
    "env": {"HEADROOM_PROXY_URL": os.environ["HEADROOM_PROXY_URL_VALUE"]},
}
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(json.dumps(data, indent=2) + "\n")
PY
    echo "  headroom: registered Docker-native MCP for Claude in $config"
    return 0
  fi

  echo "  headroom: WARNING Claude CLI not found and python3 unavailable; cannot write Claude MCP config"
  return 1
}

ensure_headroom_cli() {
  if command -v headroom >/dev/null 2>&1; then
    echo "  headroom: $(command -v headroom)"
    if is_headroom_docker_native; then
      HEADROOM_DOCKER_NATIVE=1
      echo "  headroom: detected Docker-native wrapper"
    fi
    headroom --version 2>/dev/null || true
    return 0
  fi

  local pybin
  pybin="$(find_headroom_python || true)"
  if [ -z "${pybin:-}" ]; then
    echo "  headroom: Python 3.10+ unavailable, trying Docker-native fallback"
    install_headroom_docker_native
    return $?
  fi

  if command -v pipx >/dev/null 2>&1; then
    echo "  headroom: installing headroom-ai[mcp] via pipx ($pybin)"
    if pipx install --python "$pybin" 'headroom-ai[mcp]>=0.25.0'; then
      export PATH="$HOME/.local/bin:$PATH"
      return 0
    fi
    echo "  headroom: pipx failed, trying $pybin -m pip --user"
  fi

  if "$pybin" -m pip install --user 'headroom-ai[mcp]>=0.25.0'; then
    local user_base
    user_base="$($pybin - <<'PYBASE' 2>/dev/null || true
import site
print(site.USER_BASE)
PYBASE
)"
    if [ -n "${user_base:-}" ]; then
      export PATH="$user_base/bin:$HOME/.local/bin:$PATH"
    else
      export PATH="$HOME/.local/bin:$PATH"
    fi
    return 0
  fi
  echo "  headroom: Python install failed, trying Docker-native fallback"
  install_headroom_docker_native
}

headroom_agent_for_host() {
  if [ -n "$HEADROOM_AGENT_FILTER" ]; then
    printf '%s\n' "$HEADROOM_AGENT_FILTER"
    return 0
  fi
  case "$HOST" in
    claude|codex|cursor) printf '%s\n' "$HOST" ;;
    all) printf '\n' ;;
    *) printf '__skip__\n' ;;
  esac
}

install_headroom() {
  [ "$INSTALL_HEADROOM" = "0" ] && { echo "  headroom: skipped (--skip-headroom/LOPE_INSTALL_HEADROOM=0)"; return 0; }

  if ! ensure_headroom_cli; then
    echo "  headroom: WARNING install failed; run: pipx install --python python3.11 'headroom-ai[mcp]>=0.25.0' || curl -fsSL '$HEADROOM_DOCKER_INSTALL_URL_VALUE' | bash"
    return 0
  fi

  command -v headroom >/dev/null 2>&1 || { echo "  headroom: WARNING executable not on PATH after install"; return 0; }

  local agent
  agent="$(headroom_agent_for_host)"
  if [ "$agent" = "__skip__" ]; then
    echo "  headroom: CLI installed; no MCP registrar for host '$HOST'"
    return 0
  fi

  if [ "${HEADROOM_DOCKER_NATIVE:-0}" = "1" ]; then
    if [ -z "$agent" ] || [ "$agent" = "claude" ]; then
      register_headroom_claude_docker_mcp || echo "  headroom: WARNING Docker-native Claude MCP registration failed"
      [ "$agent" = "claude" ] && return 0
      echo "  headroom: Docker-native MCP fallback currently auto-registers Claude only"
      return 0
    fi
    echo "  headroom: WARNING Docker-native MCP fallback cannot auto-register agent '$agent'"
    return 0
  fi

  if [ -n "$agent" ]; then
    echo "  headroom: registering MCP for $agent at $HEADROOM_PROXY_URL_VALUE"
    headroom mcp install --agent "$agent" --proxy-url "$HEADROOM_PROXY_URL_VALUE" || echo "  headroom: WARNING MCP registration failed"
  else
    echo "  headroom: registering MCP for every detected agent at $HEADROOM_PROXY_URL_VALUE"
    headroom mcp install --proxy-url "$HEADROOM_PROXY_URL_VALUE" || echo "  headroom: WARNING MCP registration failed"
  fi
}

install_host() {
  case "$1" in
    claude)   install_claude ;;
    codex)    install_codex ;;
    gemini)   install_gemini ;;
    opencode) install_opencode ;;
    cursor)   install_cursor ;;
    vibe)     install_vibe ;;
    qwen)     install_qwen ;;
    pi)       install_pi ;;
    *) echo "Unknown host: $1"; return 1 ;;
  esac
}

if [ "${LOPE_INSTALL_ONE_HOST:-0}" = "1" ]; then
  install_host "$HOST"
  exit $?
fi

echo ""
echo "Lope v0.17.2 — Installing skills and commands"
echo "─────────────────────────────────────────────"
echo ""

INSTALL_FAILED=0
if [ "$HOST" = "all" ]; then
  for h in claude codex gemini opencode cursor vibe qwen pi; do
    if ! LOPE_INSTALL_ONE_HOST=1 "${BASH:-bash}" "$LOPE_DIR/install" --host "$h"; then
      echo "  $h:      (failed — see error above)"
      INSTALL_FAILED=1
    fi
  done
else
  LOPE_INSTALL_ONE_HOST=1 "${BASH:-bash}" "$LOPE_DIR/install" --host "$HOST" || exit $?
fi

if [ "$INSTALL_FAILED" = "1" ]; then
  echo ""
  echo "ERROR: one or more host skill installs failed."
  exit 1
fi

echo ""
if [ "$INSTALL_HEADROOM" = "1" ]; then
  echo "Installing Headroom MCP compression (best-effort)..."
else
  echo "Headroom MCP compression is optional; skipping by default (enable with --with-headroom)."
fi
install_headroom

echo ""
echo "Verifying lope engine..."
if "$PYTHON_BIN" -c "import sys; sys.path.insert(0, sys.argv[1]); import lope; print('  OK — lope v' + lope.__version__)" "$ENGINE_ROOT" 2>/dev/null; then
  :
else
  echo "  WARNING: python3 import check failed"
  echo "  Make sure Python 3.9+ is installed: python3 --version"
fi
echo ""
echo "Slash commands by host (honest state — not every host supports user slash commands):"
echo "  Claude Code  /lope  /using-lope  /lope-negotiate  /lope-execute  /lope-implement  /lope-audit  /lope-help  /lope-ask  /lope-review  /lope-vote  /lope-compare  /lope-pipe  /lope-team  /lope-flow  /lope-memory  /lope-deliberate  /lope-headroom"
echo "  Gemini CLI   /lope:negotiate  /lope:execute  /lope:implement  /lope:audit  /lope:help  /lope:ask  /lope:review  /lope:vote  /lope:compare  /lope:pipe  /lope:team  /lope:flow  /lope:memory  /lope:deliberate  /lope:headroom"
echo "  OpenCode     /lope  /using-lope  /lope-negotiate  /lope-execute  /lope-implement  /lope-audit  /lope-help  /lope-ask  /lope-review  /lope-vote  /lope-compare  /lope-pipe  /lope-team  /lope-flow  /lope-memory  /lope-deliberate  /lope-headroom"
echo "  pi           /lope  /using-lope  /lope-negotiate  /lope-execute  /lope-implement  /lope-audit  /lope-help  /lope-ask  /lope-review  /lope-vote  /lope-compare  /lope-pipe  /lope-team  /lope-flow  /lope-memory  /lope-deliberate  /lope-headroom"
echo "  Qwen Code    /lope  /using-lope  /lope-negotiate  /lope-execute  /lope-implement  /lope-audit  /lope-help  /lope-ask  /lope-review  /lope-vote  /lope-compare  /lope-pipe  /lope-team  /lope-flow  /lope-memory  /lope-deliberate  /lope-headroom"
echo "  Cursor       (unverified — skills written to ~/.cursor/agents/)"
echo ""
echo "Codex notes:"
echo "  Codex        Lope skills copied into ~/.codex/skills/ so plain skill names load"
echo "               (lope-negotiate, lope-execute, etc.). Codex UI may still treat"
echo "               custom slash commands as content-only; agent runs 'lope <mode>' via bash."
echo ""
echo "Content-only hosts (skills loaded as context, invoke lope via natural language):"
echo "  Vibe         skills at ~/.vibe/skills/  — agent runs 'lope <mode>' via bash"
echo ""
echo "For any host, 'lope docs' prints the complete reference — all flags, all domains,"
echo "troubleshooting, hard rules. Also available as /lope-help where slash commands work."
echo ""
echo "IMPORTANT: Restart your CLI to pick up the new slash commands —"
echo "every host caches its skill list at session start."
echo ""
echo "Next steps:"
if [ "${LOPE_PACKAGED_INSTALL:-0}" = "1" ]; then
  echo "  1. Configure validators:  lope configure"
  echo "  2. Check status:          lope status"
else
  echo "  1. Configure validators:  PYTHONPATH=$ENGINE_ROOT python3 -m lope configure"
  echo "  2. Check status:          PYTHONPATH=$ENGINE_ROOT python3 -m lope status"
fi
if [ "$INSTALL_HEADROOM" = "1" ]; then
  echo "  3. Verify Headroom:       headroom mcp status"
else
  echo "  3. Optional Headroom:     $LOPE_DIR/install --with-headroom"
fi
if [ "${LOPE_PACKAGED_INSTALL:-0}" != "1" ]; then
  echo "  4. Alias for convenience: alias lope='PYTHONPATH=$ENGINE_ROOT python3 -m lope'"
fi
echo "Existing host files, if any, were preserved under: $LOPE_INSTALL_BACKUP_DIR"
echo ""
