#!/usr/bin/env bash
# Create ~/.nanobot/workspaces/{ilia,family,wife}/ from repo templates (Option B).
# Does not overwrite existing files — safe to re-run.

set -euo pipefail

REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
NANOBOT_HOME="${NANOBOT_HOME:-$HOME/.nanobot}"
DEST="${NANOBOT_HOME}/workspaces"
SKEL="${REPO_ROOT}/agent_workspaces"

if [[ ! -d "${SKEL}/ilia" ]]; then
  echo "error: missing ${SKEL}/ilia — run from nanobot repo root" >&2
  exit 1
fi

install_skel() {
  local agent="$1"
  local d="${DEST}/${agent}"
  mkdir -p "${d}/memory"
  for f in AGENTS.md USER.md SOUL.md; do
    if [[ ! -f "${d}/${f}" ]]; then
      cp "${SKEL}/${agent}/${f}" "${d}/${f}"
      echo "created ${d}/${f}"
    else
      echo "skip (exists): ${d}/${f}"
    fi
  done
  for f in MEMORY.md HISTORY.md; do
    if [[ ! -f "${d}/memory/${f}" ]]; then
      cp "${SKEL}/${agent}/memory/${f}" "${d}/memory/${f}"
      echo "created ${d}/memory/${f}"
    else
      echo "skip (exists): ${d}/memory/${f}"
    fi
  done
}

echo "NANOBOT_HOME=${NANOBOT_HOME}"
echo "DEST=${DEST}"
mkdir -p "${DEST}"

for agent in ilia family wife; do
  echo "--- ${agent} ---"
  install_skel "${agent}"
done

echo "done. Fix ownership if needed, e.g.:"
echo "  sudo chown -R \"\$(whoami):\$(whoami)\" \"${DEST}\""
