#!/usr/bin/env bash
set -euo pipefail

ROLES_DIR="${HOME}/.openclaw/ClawRoles/roles"

if [[ $# -lt 1 ]]; then
  echo "Usage: role-code-mapper <role>" >&2
  exit 1
fi

ROLE="$1"
ROLE_FILE="${ROLES_DIR}/${ROLE}/ROLE.md"

if [[ ! -f "$ROLE_FILE" ]]; then
  echo "Error: ROLE.md not found for role '${ROLE}'" >&2
  exit 1
fi

# Extract role-code from YAML frontmatter
CODE=$(sed -n '/^---$/,/^---$/{ /^role-code:/{ s/^role-code:[[:space:]]*//; p; q; } }' "$ROLE_FILE")

if [[ -z "$CODE" ]]; then
  echo "Error: role-code not found in ${ROLE_FILE}" >&2
  exit 1
fi

echo "$CODE"
