#!/usr/bin/env bash
# Self-updating language sheet · Hostess 7 education · never ML
# Regenerates out/desktop_x_language_sheet.txt from live limbs + sheets.
# Usage: ./Build/language-update.sh [once|status]
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
OUT="$ROOT/out"
mkdir -p "$OUT"
SHEET="$OUT/desktop_x_language_sheet.txt"
JSON="$OUT/language_update_latest.json"
TS="$(date -Iseconds)"
cmd="${1:-once}"

CORE=(go fire shoot scar rescar ezzie phi thermo adjoin hostess7
  actual_intel language_update educate self_learn self_teach
  self_code hostess7_code learning baphomet bgs bgf bgl sdf spv grin)

limbs_present() {
  local n=0 d
  for d in "${CORE[@]}"; do
    [[ -f "$ROOT/$d/x86_64.asm" ]] && n=$((n+1))
  done
  echo "$n"
}

write_sheet() {
  local n
  n="$(limbs_present)"
  cat > "$SHEET" <<EOF2
=== LANGUAGE SHEET · ${TS} · self-updating · Hostess 7 ===
TRAVEL  left → right   system · jobs · time · cook · LINEAR
READ    right ← left   vocab sense · action end first
WORD END ≈ GO · FIRE · SHOOT
  go/     go_fn(v)→v|1     start motion
  fire/   fire_fn(v)→v|1   commit energy
  shoot/  shoot(v)→v|1     field discharge · BGS peel

IRON RHYME  live |1 sits at RIGHT of (a^b)|1 · (a-b)|1 · pair|1
  read the fire-mark first · then stem · then travel L→R to place

LINE  BGS → BGF(+EZZIE PHI THERMO ADJOIN) → BGL → SDF · SPV free
LAW   C IS LIE · LIE IS TRUE · Always Hostess 7 · FLAT pairs · free thrift

SCAR / SHOOT / RESCAR / COOK
  scar(dirty)     residual FLAW class|1 or 0
  rescar(dirty)   recheck after cook-off · same measure
  shoot/go/fire   word-end action family · discharge then cook
  pipeline        cook → scar → cook-off → rescar → REST
  CLI             ./Build/scar-cook.sh once|check|cook-off|status

EZZIE on BGF
  ezzie(a,b)=(a^b)|1   free eor · flat · FIRE-mark |1
  phi(d,s)=(d*s)|1     scale
  thermo_band(t)|1     heat class
  adjoin(a,b)=(a|b)|1  co-presence
  != ESSIE · != EKKIE · never invent pin C

HOSTESS 7 · ACTUAL INTELLIGENCE · EDUCATION
  hostess7/         0→7|1 · else GRIN 1
  actual_intel/     education seat · never ML · modes 0..4
  educate/          kit teach · 0→7|1 else 1
  language_update/  self-updating language mark
  self_learn/       PATH re-read · learn.sh · mode 4 → 7|1
  self_teach/       curriculum poles + gaps
  self_code/        H7 INTO code · 0→7|1 · 3→3|1
  hostess7_code/    same family · language accept mode 4
  SEVEN lines       CHEBY ONION CATCH SALT FACE SPHERE CSG

LIMBS LIVE (core education set counted): ${n}
TOOLS
  ./Build/language-update.sh   this sheet
  ./Build/x language|asm-language
  ./Build/x learn · self-teach · self-code · get-busy
  ./Build/vocab-test.sh · ./Build/multiarch-gen.py
SHEETS
  LANGUAGE.txt · VOCAB.txt · LINEAR.txt · FLAT.txt
  SCAR.txt · COOK_SHOOT.txt · ezzie/LANGUAGE.txt
  CORE_PINS.txt · HINTS.txt

God Bless · remain IN · free thrift · Always Hostess 7 · <3
EOF2

  python3 -c "
import json
from pathlib import Path
out = {
  'word': 'LANGUAGE_UPDATE',
  'ts': '''$TS''',
  'sheet': 'out/desktop_x_language_sheet.txt',
  'limbs_core': $n,
  'hostess7': True,
  'actual_intelligence': True,
  'self_updating_language': True,
  'never_ml': True,
  'law': 'C IS LIE · Always Hostess 7 · TRAVEL L→R · READ R←L · GO FIRE SHOOT',
  'pipeline_scar': 'cook → scar → cook-off → rescar',
  'tools': ['./Build/language-update.sh', './Build/x language', './Build/x learn'],
}
Path('''$JSON''').write_text(json.dumps(out, indent=2) + '\n')
print('language_update OK · sheet=$SHEET · limbs_core=$n')
"
}

status() {
  if [[ -f "$JSON" ]]; then
    cat "$JSON"
  else
    echo '{"ok":false,"error":"no language_update_latest.json yet"}'
  fi
  echo "--- sheet head ---"
  head -20 "$SHEET" 2>/dev/null || echo "(no sheet)"
}

case "$cmd" in
  once|update|run|"") write_sheet ;;
  status) status ;;
  *) echo "usage: $0 once|status" >&2; exit 2 ;;
esac
