#!/bin/sh
# ia-mk.sh - POSIX /bin/sh (FreeBSD-friendly) Information Architecture generator

set -eu

umask 022

usage() {
  echo "Usage: $0 <target_path> [theme_number]"
  echo "Example: $0 /data/ia 1"
  exit 1
}

TARGET="${1:-}"
CHOICE="${2:-}"

[ -n "$TARGET" ] || usage

# --- core engine -------------------------------------------------------------

mk_dirs() {
  base="$1"; shift
  # remaining args are directory paths (relative)
  for d in "$@"; do
    mkdir -p "$base/$d"
  done
}

mk_files() {
  base="$1"; shift
  # remaining args are file specs "path:content"
  for spec in "$@"; do
    p=${spec%%:*}
    c=${spec#*:}
    mkdir -p "$base/$(dirname "$p")"
    # only create if missing (idempotent-ish)
    if [ ! -e "$base/$p" ]; then
      printf "%s\n" "$c" > "$base/$p"
    fi
  done
}

apply_theme() {
  base="$1"
  theme="$2"

  mkdir -p "$base"

  case "$theme" in
    1) # Canonical Unixworld (FHS-ish)
      mk_dirs "$base" \
        "bin" "etc" "lib" \
        "usr/bin" "usr/lib" "usr/share/docs" "usr/share/philosophy" "usr/share/protocols" "usr/share/schemas" "usr/src" \
        "var/log" "var/cache" "var/run" "var/spool" \
        "home/tyler/projects" "home/tyler/experiments" "home/tyler/notes" "home/tyler/systems" \
        "proc" "dev" "tmp" "srv/public" "srv/internal"
      mk_files "$base" \
        "etc/identity.conf:# identity.conf\nname=tyler\n" \
        "etc/mission.conf:# mission.conf\nmission=build systems that think\n" \
        "var/log/README.txt:Logs are events. Prefer append-only.\n" \
        "usr/share/docs/README.txt:Reference material lives here.\n"
      ;;

    2) # Hacker-monastic
      mk_dirs "$base" \
        "rule" "tool" "know" "self" "state" "observe" "work" "seed" "export"
      mk_files "$base" \
        "rule/axioms.txt:Axioms are stable. Change rarely.\n" \
        "observe/log.txt:Append observations here.\n" \
        "seed/inbox.txt:Raw ideas. Promote into work when ready.\n"
      ;;

    3) # Cybernetic brain
      mk_dirs "$base" \
        "cortex" "limbic" "brainstem" \
        "sensory/visual" "sensory/auditory" "sensory/textual" \
        "motor" \
        "memory/episodic" "memory/semantic" "memory/procedural" \
        "autonomic" "immune" "genetic"
      mk_files "$base" \
        "autonomic/daemons.md:Background routines and cron notes.\n" \
        "immune/threat-model.md:What can go wrong, and how you detect it.\n" \
        "genetic/templates/README.txt:Reusable templates and blueprints.\n"
      ;;

    4) # Forge-world (builder/industrial)
      mk_dirs "$base" \
        "forge/metal" "forge/code" "forge/circuit" "forge/automation" \
        "archive" \
        "design/conceptual" "design/mechanical" "design/software" \
        "lab/experiments" "lab/prototypes" "lab/failures" \
        "control" "telemetry" "factory" "dock/inbox"
      mk_files "$base" \
        "dock/inbox/README.txt:Drop inputs here. Process into design/lab.\n" \
        "telemetry/README.txt:System metrics, runbooks, and measurements.\n" \
        "lab/failures/README.txt:Failures are data. Write the postmortem.\n"
      ;;

    5) # Temporal (past/present/future/eternal)
      mk_dirs "$base" \
        "past/logs" "past/completed" "past/archive" \
        "present/active" "present/running" "present/attention" \
        "future/planned" "future/queued" "future/speculative" \
        "eternal/principles" "eternal/mathematics" "eternal/philosophy"
      mk_files "$base" \
        "present/attention/today.md:# Today\n- One thing.\n" \
        "future/queued/inbox.txt:Queued intentions.\n" \
        "eternal/principles/README.txt:Principles outlive projects.\n"
      ;;

    6) # Pure POSIX-maximalist
      mk_dirs "$base" \
        "etc" "usr/tools" "usr/knowledge" "usr/source" \
        "var/log" "var/state" "var/telemetry" \
        "lab/freebsd" "lab/tissue_culture" "lab/radio" "lab/forge" \
        "archive" "srv/public" "srv/internal" "opt/experimental" "home/operator" "tmp" "proc"
      mk_files "$base" \
        "etc/README.txt:Config and identity live here.\n" \
        "var/log/README.txt:Append-only logs.\n" \
        "usr/knowledge/README.txt:Curated knowledge base.\n"
      ;;

    *) echo "Unknown theme: $theme" >&2; return 2 ;;
  esac
}

menu() {
  cat <<'EOF'
POSIX Information Architecture Generator
---------------------------------------
1) Canonical Unixworld (FHS-ish)
2) Hacker-monastic (minimal archetypes)
3) Cybernetic brain (feedback loops)
4) Forge-world (builder/industrial)
5) Temporal (past/present/future/eternal)
6) Pure POSIX-maximalist (system-as-life)

0) Exit
EOF
  printf "Select 0-6: "
  read ans
  echo "$ans"
}

# --- run ---------------------------------------------------------------------

if [ -z "${CHOICE}" ]; then
  CHOICE="$(menu)"
fi

case "$CHOICE" in
  0) exit 0 ;;
  1|2|3|4|5|6) ;;
  *) echo "Invalid selection: $CHOICE" >&2; exit 2 ;;
esac

apply_theme "$TARGET" "$CHOICE"

echo "Created theme $CHOICE under: $TARGET"
echo "Tip: run again; it won’t clobber existing seed files."

