#!/usr/bin/env bash
# Tacit Skills installer — https://tacit.sh/skills
#
# Install all five skills:
#   curl -fsSL https://tacit.sh/install.sh | bash
#
# Install specific skills only:
#   curl -fsSL https://tacit.sh/install.sh | bash -s scrutiny verdict
#
# Re-run at any time to update — it overwrites existing files.

set -euo pipefail

# ===== Config =====
# Source: currently GitHub raw (public, works out of the box).
# Migration path: when traffic/rate-limits justify it, flip this to
# R2 public bucket — e.g. REPO_RAW="https://skills.tacit.sh"
# The public URL contract (curl | bash) stays identical for end users.
REPO_RAW="https://raw.githubusercontent.com/ketankhairnar/tacit-skills/main/skills"
TARGET_DIR="${HOME}/.claude/commands"
ALL_SKILLS=("scrutiny" "verdict" "autopsy" "fracture" "tacit")
VERSION="0.1"

# ===== Colors (if TTY) =====
if [ -t 1 ]; then
  ORANGE=$(printf '\033[38;5;208m')
  BOLD=$(printf '\033[1m')
  DIM=$(printf '\033[2m')
  RESET=$(printf '\033[0m')
else
  ORANGE=""
  BOLD=""
  DIM=""
  RESET=""
fi

# ===== Output helpers =====
say()  { printf "%s\n" "$*"; }
bold() { printf "${BOLD}%s${RESET}\n" "$*"; }
tick() { printf "  ${ORANGE}✓${RESET} %s\n" "$*"; }
warn() { printf "  ${ORANGE}!${RESET} %s\n" "$*" >&2; }
die()  { printf "  ${ORANGE}×${RESET} %s\n" "$*" >&2; exit 1; }

# ===== Banner =====
cat <<EOF

${BOLD}tacit skills installer${RESET} ${DIM}v${VERSION}${RESET}
${DIM}one command for any lonely technical decision${RESET}

EOF

# ===== Preflight =====
command -v curl >/dev/null 2>&1 || die "curl is required but not installed"

# Figure out which skills to install
if [ "$#" -gt 0 ]; then
  SKILLS=("$@")
  # validate each requested skill is in the registry
  for s in "${SKILLS[@]}"; do
    found=0
    for known in "${ALL_SKILLS[@]}"; do
      [ "$s" = "$known" ] && found=1 && break
    done
    [ "$found" -eq 1 ] || die "Unknown skill: ${s}. Known: ${ALL_SKILLS[*]}"
  done
  bold "Installing ${#SKILLS[@]} skill(s): ${SKILLS[*]}"
else
  SKILLS=("${ALL_SKILLS[@]}")
  bold "Installing all ${#SKILLS[@]} skills"
fi

# Ensure target dir exists
mkdir -p "$TARGET_DIR"

# ===== Install loop =====
say ""
say "Target: ${DIM}${TARGET_DIR}${RESET}"
say ""

installed=0
for skill in "${SKILLS[@]}"; do
  src="${REPO_RAW}/${skill}.md"
  dst="${TARGET_DIR}/${skill}.md"

  if curl -fsSL --max-time 30 "$src" -o "${dst}.tmp"; then
    # Guard against empty downloads
    if [ ! -s "${dst}.tmp" ]; then
      rm -f "${dst}.tmp"
      warn "failed: ${skill} (empty response)"
      continue
    fi
    mv "${dst}.tmp" "$dst"
    tick "/${skill}   ${DIM}→ ${dst}${RESET}"
    installed=$((installed + 1))
  else
    rm -f "${dst}.tmp" 2>/dev/null || true
    warn "failed: ${skill} (download error)"
  fi
done

# ===== Summary =====
say ""
if [ "$installed" -eq "${#SKILLS[@]}" ]; then
  bold "${ORANGE}done.${RESET}"
  say ""
  say "  ${installed} skill(s) installed to ${TARGET_DIR}"
  say ""
  say "${DIM}In Claude Code:${RESET}"
  say "  ${ORANGE}/tacit${RESET}    ${DIM}— route any situation to the right skill${RESET}"
  say "  ${ORANGE}/scrutiny${RESET} ${DIM}— architecture review${RESET}"
  say "  ${ORANGE}/verdict${RESET}  ${DIM}— decision memo${RESET}"
  say "  ${ORANGE}/autopsy${RESET}  ${DIM}— incident postmortem${RESET}"
  say "  ${ORANGE}/fracture${RESET} ${DIM}— spec stress-test${RESET}"
  say ""
  say "${DIM}Update later: re-run the same command.${RESET}"
  say "${DIM}Docs: https://tacit.sh/skills${RESET}"
  say ""
  exit 0
else
  warn "installed ${installed} of ${#SKILLS[@]} — some downloads failed"
  exit 1
fi
