#!/usr/bin/env bash
# DCS install (POSIX): copy the DCS package from this repo (canonical) into ~/.claude.
# Idempotent; overwrites installed files with repo versions. Run after every committed change.
#
# --target=<claude|dsh|pi|codex|all|auto> (default claude -- unchanged historical behavior):
#   claude  the Claude home: payload and skills copied unchanged, charters'
#           frontmatter model: mapped by dcs/tools/claude_transform.py (shipped
#           map < <claude home>/dcs-seats.json); the claude leg REQUIRES python
#           (DCS_PYTHON overrides `python`); DCS_CLAUDE_DIR overrides the home;
#           a bad map fails loudly with nothing written; no marker is written
#   dsh     transformed payload into the DSH home (~/.dsh)
#   pi      transformed payload and bridge into the Pi agent home
#   codex   validated package, generated agents/skills and local marketplace;
#           hook declarations still require current-definition /hooks trust
#   all     Claude, DSH, Pi, then Codex
#   auto    only targets whose home directory exists
# --no-plugin skips the Claude seat-picker plugin leg (env DCS_NO_PLUGIN=1 does the
# same, persistently). The leg itself is `node bin/dcs.js plugin-leg`, so the
# `claude plugin ...` logic has ONE implementation; with no node it prints one
# `DCS plugin:` warning and the install still succeeds.
# dsh/pi/codex/all/auto delegate to the SAME installer logic (node bin/dcs.js install
# --target ...) so the transform and the marked appends have ONE implementation;
# this script stays a thin front door for those targets. The claude body below
# is kept native so a machine without node can still refresh the Claude tree.
set -euo pipefail
repo="$(cd "$(dirname "$0")" && pwd)"

target="claude"
no_plugin=0
while [ $# -gt 0 ]; do
  case "$1" in
    --target=*) target="${1#--target=}"; shift ;;
    --target)
      [ $# -ge 2 ] || { echo "error: --target requires a value" >&2; exit 1; }
      target="$2"; shift 2 ;;
    --no-plugin) no_plugin=1; shift ;;
    *) shift ;;
  esac
done
case "$target" in
  claude|dsh|pi|codex|all|auto) ;;
  *) echo "error: unknown --target '$target' (valid: claude|dsh|pi|codex|all|auto)" >&2; exit 1 ;;
esac

if [ "$target" != "claude" ]; then
  if [ "$no_plugin" = 1 ]; then
    node "$repo/bin/dcs.js" install --target "$target" --no-plugin
  else
    node "$repo/bin/dcs.js" install --target "$target"
  fi
  exit $?
fi

claude="${DCS_CLAUDE_DIR:-$HOME/.claude}"
py="${DCS_PYTHON:-python}"

# Map the charters FIRST, into a fresh temp dir: a bad seat map must leave the
# Claude home untouched.
tmp="$(mktemp -d)"
trap 'rm -rf "$tmp"' EXIT
if ! "$py" "$repo/dcs/tools/claude_transform.py" --agents-src "$repo/agents" --dst "$tmp/agents" \
    --shipped "$repo/dcs/claude/seat-models.json" --user "$claude/dcs-seats.json"; then
  echo "claude target FAILED LOUDLY -- NOTHING was written to $claude (is python installed? set DCS_PYTHON)" >&2
  exit 1
fi

mkdir -p "$claude/dcs" "$claude/agents" "$claude/skills"

# Purge only the plugin subtree so files dropped from the plugin never survive.
rm -rf "$claude/dcs/claude/plugin"

rsync -a --exclude '__pycache__' "$repo/dcs/" "$claude/dcs/" 2>/dev/null \
  || cp -R "$repo/dcs/." "$claude/dcs/"

cp "$tmp"/agents/dcs-*.md "$claude/agents/"

for d in "$repo"/skills/dcs-*/; do
  name="$(basename "$d")"
  mkdir -p "$claude/skills/$name"
  cp -R "$d." "$claude/skills/$name/"
done

version="$(tr -d '[:space:]' < "$repo/dcs/VERSION")"
echo "DCS $version installed to $claude"
echo "NOTE: projects hold their own copy of dcs_gate.py in <project>/.claude/hooks/ -- re-run /dcs-init (or copy dcs/hooks/dcs_gate.py manually) in each onboarded project after a hook change."

# Plugin leg: delegated to node, never fatal (exit code and files as without it).
case "${DCS_NO_PLUGIN:-}" in
  ""|0|false|FALSE|False|no|NO|No|off|OFF|Off) env_no_plugin=0 ;;
  *) env_no_plugin=1 ;;
esac
if [ "$no_plugin" != 1 ] && [ "$env_no_plugin" != 1 ]; then
  if command -v node >/dev/null 2>&1; then
    DCS_CLAUDE_DIR="$claude" node "$repo/bin/dcs.js" plugin-leg || true
  else
    echo "DCS plugin: node was not found, so the seat picker plugin was not set up (install node, or pass --no-plugin). DCS itself installed fine." >&2
  fi
fi
