#!/bin/sh
# Sema language skill installer.
#
# Installs the Sema language-support skill (SKILL.md) into your coding agent so an
# LLM that was never trained on Sema can still read and write it correctly.
#
# Usage:
#   curl -fsSL https://sema.49.12.246.95.sslip.io/skills/install.sh | sh
#   curl -fsSL https://sema.49.12.246.95.sslip.io/skills/install.sh | sh -s -- claude
#   ... | sh -s -- <auto|claude|global|cortex|codex>
set -eu

SITE="https://sema.49.12.246.95.sslip.io"
URL="$SITE/skills/sema-language/SKILL.md"
NAME="sema-language"
TARGET="${1:-auto}"

log() { printf '%s\n' "$*" >&2; }
fetch() {
  if command -v curl >/dev/null 2>&1; then curl -fsSL "$URL";
  elif command -v wget >/dev/null 2>&1; then wget -qO- "$URL";
  else log "error: need curl or wget"; exit 1; fi
}

put_skill() {  # $1 = skills directory
  dir="$1"
  mkdir -p "$dir/$NAME"
  fetch > "$dir/$NAME/SKILL.md"
  log "  installed: $dir/$NAME/SKILL.md"
}

put_codex() {
  mkdir -p .codex
  fetch > ".codex/$NAME.md"
  if [ ! -f AGENTS.md ] || ! grep -q "$NAME" AGENTS.md 2>/dev/null; then
    printf '\n## Sema language support\nWhen reading or writing Sema (`.sema`) files, follow `.codex/%s.md`.\nSema is a new AI-native language most models were not trained on.\n' "$NAME" >> AGENTS.md
    log "  updated: AGENTS.md (pointer added)"
  fi
  log "  installed: .codex/$NAME.md"
}

log "Sema language skill -> $TARGET"
case "$TARGET" in
  claude)                     put_skill ".claude/skills" ;;
  global|home)                put_skill "$HOME/.claude/skills" ;;
  cortex|openclaw|omp|agents) put_skill ".agents/skills" ;;
  codex)                      put_codex ;;
  auto|"")
    did=0
    if [ -d ".claude" ]; then put_skill ".claude/skills"; did=1; fi
    if [ -d ".agents" ]; then put_skill ".agents/skills"; did=1; fi
    if [ -f "AGENTS.md" ] || [ -d ".codex" ]; then put_codex; did=1; fi
    if [ "$did" -eq 0 ]; then
      log "  (no project agent dir found here; installing to your global Claude Code skills)"
      put_skill "$HOME/.claude/skills"
    fi
    ;;
  *) log "unknown target: $TARGET  (use: auto | claude | global | cortex | codex)"; exit 1 ;;
esac

log ""
log "Done. Start a new agent session so it loads the Sema skill."
