#!/usr/bin/env bash
# new-profile.sh — create a Hermes profile and wire it into the shared
# identity layer so it knows who's who (Rob, Connie, Surface, Jarvis,
# Social Media Agent, Bail Bond Outreach Agent).
#
# Usage:
#   new-profile.sh <profile_name> [--description "..."] [--clone]
#
# What it does (in order):
#   1. Runs `hermes profile create` with the args you pass.
#   2. Copies /root/.hermes/shared-identity.md into the new profile.
#   3. If the profile already has a SOUL.md, prepends a pointer to the
#      shared identity. If it doesn't, creates a stub SOUL.md that does
#      nothing but point to the shared identity.
#   4. Prints a one-line summary of what was done.

set -euo pipefail

print_help() {
  cat <<EOF
Usage: $0 <profile_name> [--description "..."] [--clone]

Create a Hermes profile and wire it into the shared network identity
layer (so the new profile knows who [PERSON_NAME], [PERSON_NAME], Surface, Jarvis,
the Social Media Agent, and the Bail Bond Outreach Agent are).

All arguments after <profile_name> are passed through to
\`hermes profile create\`.

Examples:
  $0 client-acme --description "Acme Corp engagement"
  $0 job-seeker --clone-from default --description "Job search variant"
EOF
}

if [[ $# -lt 1 ]] || [[ "${1:-}" == "--help" ]] || [[ "${1:-}" == "-h" ]]; then
  print_help
  exit 0
fi

HERMES_HOME="${HERMES_HOME:-/root/.hermes}"
HERMES_BIN="${HERMES_BIN:-/root/.hermes/venv/bin/hermes}"
PROFILE_NAME="$1"
shift

PROFILE_DIR="$HERMES_HOME/profiles/$PROFILE_NAME"
SHARED_IDENTITY="$HERMES_HOME/shared-identity.md"

if [[ ! -f "$SHARED_IDENTITY" ]]; then
  echo "ERROR: $SHARED_IDENTITY not found. Network identity layer missing." >&2
  exit 1
fi

# 1. Create the profile (passthrough args).
"$HERMES_BIN" profile create "$PROFILE_NAME" "$@"

# Sanity check: profile dir must exist post-create.
if [[ ! -d "$PROFILE_DIR" ]]; then
  echo "ERROR: profile directory $PROFILE_DIR not found after create." >&2
  exit 1
fi

# 2. Copy shared identity into the profile.
cp "$SHARED_IDENTITY" "$PROFILE_DIR/shared-identity.md"

# 3. Patch the profile's SOUL.md to reference shared-identity.md.
SOUL="$PROFILE_DIR/SOUL.md"
POINTER='> **Shared network identity** ([PERSON_NAME], [PERSON_NAME], Surface, and the other agents in the network, plus the naming rules for talking about each other) lives in `shared-identity.md` in this profile directory (a copy of `/root/.hermes/shared-identity.md`). Load it before answering anything about who Rob is, who Connie is, who the Social Media Agent / Bail Bond Outreach Agent are, or how to refer to yourself.'

if [[ -f "$SOUL" ]] && ! grep -qF 'shared-identity.md' "$SOUL"; then
  # Prepend pointer to existing SOUL.md (preserve persona layer).
  TMP=$(mktemp)
  {
    printf '%s\n\n' "$POINTER"
    cat "$SOUL"
  } > "$TMP"
  mv "$TMP" "$SOUL"
  SOUL_ACTION="prepended pointer to existing SOUL.md"
elif [[ ! -f "$SOUL" ]]; then
  # Create a stub that does nothing but point to shared-identity.md.
  cat > "$SOUL" <<EOF
# Soul — $PROFILE_NAME profile

$POINTER

You are Hermes Agent, an intelligent AI assistant created by Nous Research.
You are helpful, knowledgeable, and direct. You assist users with a wide
range of tasks including answering questions, writing and editing code,
analyzing information, creative work, and executing actions via your tools.
You communicate clearly, admit uncertainty when appropriate, and prioritize
being genuinely useful over being verbose unless otherwise directed below.
Be targeted and efficient in your exploration and investigations.
EOF
  SOUL_ACTION="created stub SOUL.md with pointer"
else
  SOUL_ACTION="SOUL.md already referenced shared-identity.md (no change)"
fi

# 4. Summary.
cat <<EOF

Profile created and wired into the shared identity layer:
  Profile:      $PROFILE_NAME
  Directory:    $PROFILE_DIR
  Shared ID:    $PROFILE_DIR/shared-identity.md
  SOUL.md:      $SOUL_ACTION

Next steps (usually):
  - Add profile-specific skills (symlink or copy into profiles/$PROFILE_NAME/skills/)
  - Add a profile-specific brief (e.g. AGENT-BRIEF.md) on top of the
    shared-identity layer, naming conventions, and SOUL.md stub
  - If the profile needs memory, add profile-scoped entries to
    profiles/$PROFILE_NAME/memories/ (or rely on the global memory)
  - Run \`hermes profile list\` to confirm and \`hermes profile use $PROFILE_NAME\` to switch
EOF
