#!/bin/sh
# Installs the Eligapris founder skills pack (v0.0.3) into ~/.agents/skills
# and links each skill into the agents found on this machine
# (Claude Code, Codex, Cursor, Gemini CLI, OpenCode).
#
#   curl -fsSL https://eligapris.com/founder-skills.sh | sh
#
# Run from an unzipped bundle, it installs the copy next to it instead of downloading.
set -eu

PACK_URL="https://eligapris.com/founder-skills.zip"
TELEMETRY_URL="https://eligapris.com/api/skills/events"
SKILLS="lean-venture-analyst research-lab agent-led-product codebase-5yr-forecast ip-booster why-first-marketing propaganda-for-founders conversion-copy-audit humanize"

say() { printf '%s\n' "$*"; }
die() { printf 'error: %s\n' "$*" >&2; exit 1; }

AGENTS_HOME="${AGENTS_HOME:-$HOME/.agents}"
DEST="$AGENTS_HOME/skills"
STAMP=$(date +%Y%m%d-%H%M%S)
BACKUP="$AGENTS_HOME/skill-backups/$STAMP"

TMP=$(mktemp -d 2>/dev/null || mktemp -d -t skills)
trap 'rm -rf "$TMP"' EXIT INT TERM

# Use the bundle next to this script if there is one, otherwise download it.
HERE=$(cd "$(dirname "$0")" 2>/dev/null && pwd || pwd)
if [ -f "$HERE/pack.json" ] && [ -d "$HERE/skills" ]; then
  SRC="$HERE/skills"
  MODE=bundle
else
  [ -n "$PACK_URL" ] || die "no bundle next to this script and no download URL set"
  MODE=online
  say "Downloading the skills pack..."
  if command -v curl >/dev/null 2>&1; then
    curl -fsSL "$PACK_URL" -o "$TMP/pack.zip" || die "download failed: $PACK_URL"
  elif command -v wget >/dev/null 2>&1; then
    wget -q "$PACK_URL" -O "$TMP/pack.zip" || die "download failed: $PACK_URL"
  else
    die "need curl or wget to download the pack"
  fi
  mkdir -p "$TMP/x"
  if command -v unzip >/dev/null 2>&1; then
    unzip -q "$TMP/pack.zip" -d "$TMP/x"
  elif command -v bsdtar >/dev/null 2>&1; then
    bsdtar -xf "$TMP/pack.zip" -C "$TMP/x"
  elif command -v python3 >/dev/null 2>&1; then
    python3 -m zipfile -e "$TMP/pack.zip" "$TMP/x"
  else
    die "need unzip, bsdtar or python3 to unpack the pack"
  fi
  SRC=""
  for d in "$TMP"/x/*/skills; do [ -d "$d" ] && SRC="$d"; done
  [ -n "$SRC" ] || die "the downloaded file doesn't look like a skills pack"
fi

# Move something out of the way instead of deleting it.
backup() {
  mkdir -p "$BACKUP/$2"
  mv "$1" "$BACKUP/$2/"
  BACKED_UP=1
}

BACKED_UP=0
mkdir -p "$DEST"
for name in $SKILLS; do
  [ -f "$SRC/$name/SKILL.md" ] || die "pack is missing $name"
  target="$DEST/$name"
  if [ -L "$target" ]; then
    rm -f "$target"
  elif [ -f "$target/.installed-by" ] && grep -q "^eligapris-" "$target/.installed-by"; then
    rm -rf "$target"   # installed by this or another Eligapris pack, nothing of the user's to keep
  elif [ -e "$target" ]; then
    backup "$target" skills
  fi
  cp -R "$SRC/$name" "$target"
  echo "eligapris-founder-skills 0.0.3" > "$target/.installed-by"
  find "$target" -type f \( -name '*.sh' -o -name '*.py' -o -name '*.mjs' \) -exec chmod +x {} + 2>/dev/null || true
  say "  installed $name"
done

# Link into every agent that's installed. A real folder with the same name is
# backed up first so the agent doesn't keep loading an old copy.
LINKED=""
for agent_dir in "$HOME/.claude" "$HOME/.codex" "$HOME/.cursor" "$HOME/.gemini" "$HOME/.config/opencode"; do
  [ -d "$agent_dir" ] || continue
  links="$agent_dir/skills"
  mkdir -p "$links"
  for name in $SKILLS; do
    link="$links/$name"
    if [ -L "$link" ]; then
      rm -f "$link"
    elif [ -e "$link" ]; then
      backup "$link" "links-$(basename "$agent_dir" | tr -d .)"
    fi
    ln -s "$DEST/$name" "$link"
  done
  LINKED="$LINKED $(basename "$agent_dir" | tr -d .)"
done

# Always-on writing rule: put the humanize rules in each agent's global
# instructions so every reply follows them without the skill being called.
# The block sits between markers, so a rerun replaces it and deleting it turns it off.
RULES="$DEST/humanize/references/ai-writing-signs.md"
MARK_START="<!-- dev-skill-pack:humanize:start -->"
MARK_END="<!-- dev-skill-pack:humanize:end -->"
RULED=""

rule_block() { # claude | inline
  printf '%s\n' "$MARK_START"
  printf '%s\n' "<!-- Added by the Dev Skill Pack (eligapris.com/skills). Delete everything from the start marker to the end marker to turn it off. -->"
  printf '%s\n' "# Writing voice" "" \
    "Everything you write has to read as if a person wrote it: chat replies, docs," \
    "commit messages, PR descriptions, code comments and UI copy. Follow the rules" \
    "below every time, without being asked. Before handing over a long piece of" \
    "prose, run \`python3 ~/.agents/skills/humanize/scripts/check.py <file>\` and fix" \
    "every \"tell\"." ""
  if [ "$1" = claude ]; then
    printf '%s\n' "@~/.agents/skills/humanize/references/ai-writing-signs.md"
  else
    sed 's/^#/##/' "$RULES"
  fi
  printf '%s\n' "$MARK_END"
}

add_rule() { # file, claude|inline, label
  f=$1
  mkdir -p "$(dirname "$f")"
  if [ -f "$f" ]; then
    outside=$(awk -v s="$MARK_START" -v e="$MARK_END" '$0==s{skip=1;next} $0==e{skip=0;next} !skip' "$f")
    # Already has these rules some other way: leave it alone.
    if printf '%s' "$outside" | grep -qiE 'ai-writing-signs|signs of ai writing'; then return 0; fi
    if ! grep -qF "$MARK_START" "$f"; then
      mkdir -p "$BACKUP/instructions"
      cp "$f" "$BACKUP/instructions/$3-$(basename "$f")"
      BACKED_UP=1
    fi
    if [ -n "$outside" ]; then
      { printf '%s\n\n' "$outside"; rule_block "$2"; } > "$f"
    else
      rule_block "$2" > "$f"
    fi
  else
    rule_block "$2" > "$f"
  fi
  RULED="$RULED $3"
}

if [ -f "$RULES" ]; then
  if [ -d "$HOME/.claude" ]; then add_rule "$HOME/.claude/CLAUDE.md" claude claude; fi
  if [ -d "$HOME/.codex" ]; then add_rule "$HOME/.codex/AGENTS.md" inline codex; fi
  if [ -d "$HOME/.gemini" ]; then add_rule "$HOME/.gemini/GEMINI.md" inline gemini; fi
  if [ -d "$HOME/.config/opencode" ]; then add_rule "$HOME/.config/opencode/AGENTS.md" inline opencode; fi
fi

say ""
say "Done. 9 skills are in $DEST"
[ -n "$LINKED" ] && say "Linked for:$LINKED"
[ -n "$RULED" ] && say "Writing rule always on for:$RULED"
[ "$BACKED_UP" = 1 ] && say "Older copies are in $BACKUP"
say "Restart your agent (or open a new session) to pick them up."

# One anonymous install report: pack, version, OS, and which agents were set up.
# Nothing about you, your paths or your files. DO_NOT_TRACK=1 skips it, and it
# gives up after 2 seconds so it never holds up the install.
report() {
  [ -n "${DO_NOT_TRACK:-}" ] && return 0
  [ -n "$TELEMETRY_URL" ] || return 0
  case "$(uname -s 2>/dev/null)" in
    Linux) os=linux ;;
    Darwin) os=macos ;;
    *) os=other ;;
  esac
  arch=$(uname -m 2>/dev/null | tr -cd 'a-zA-Z0-9_-' | cut -c1-20)
  list() { out=""; for x in $1; do out="$out\"$x\","; done; printf '%s' "${out%,}"; }
  backups=false; [ "$BACKED_UP" = 1 ] && backups=true
  count=0; for name in $SKILLS; do count=$((count + 1)); done
  json=$(printf '{"pack":"%s","version":"%s","event":"install","os":"%s","arch":"%s","mode":"%s","shell":"sh","agents":[%s],"rules":[%s],"skills":%s,"backups":%s}' \
    "eligapris-founder-skills" "0.0.3" "$os" "$arch" "$MODE" "$(list "$LINKED")" "$(list "$RULED")" "$count" "$backups")
  if command -v curl >/dev/null 2>&1; then
    curl -fsS -m 2 -o /dev/null -H 'Content-Type: application/json' -d "$json" "$TELEMETRY_URL" 2>/dev/null || true
  elif command -v wget >/dev/null 2>&1; then
    wget -q -T 2 -t 1 -O /dev/null --header='Content-Type: application/json' --post-data="$json" "$TELEMETRY_URL" 2>/dev/null || true
  fi
}
report || true
