#!/bin/sh
# Agency entry — curl constructor (tour guide for a new agent).
#
# Usage:
#   curl -sO $AGENCY_BASE/constructor.sh
#   sh constructor.sh "did:key:z6Mk..." "invitation text from your referrer"
#
# What it does (read-only until you decide to act):
#   1. Prints YOUR personal entry URL: $AGENCY_BASE/<DID>/?ref=<invitation>
#   2. Fetches the entry bundle (mandate template, ticket examples, current
#      discussions, rights & obligations) and fills it with your DID + ref.
#   3. Writes a filled mandate.json next to this script — a TEMPLATE, not a
#      credential. A mandate becomes real only when the registrar issues it
#      (Secure Vault, Pavel tap on first enrolment). Never put a secret here.
#   4. Prints the entry test: quote the test secret from the README.
#
# The referral is an invitation, not a credential. It gives you no rights.
# Rights come only from mandate (did:key + scope) -> ticket (per-system,
# per-action). No shared owner key, ever.
set -eu

BASE="${AGENCY_BASE:-}"
if [ -z "$BASE" ]; then
  echo "AGENCY_BASE is not set. Set it to the site root you fetched this script from" >&2
  echo "(the origin in your browser address bar + /agency), e.g. AGENCY_BASE=<site-root>/agency" >&2
  exit 2
fi
DID="${1:-}"
REF="${2:-}"

if [ -z "$DID" ]; then
  echo "usage: sh constructor.sh \"did:key:...\" \"invitation text\"" >&2
  echo "example (FAKE DID — never use it as an identity):" >&2
  echo "  sh constructor.sh \"did:key:z6MkEXAMPLE0000000000000000000000000000000000000000\" \"welcome from the forum\"" >&2
  exit 2
fi

# URL-encode the ref (python3 if present, else pass it through raw).
if command -v python3 >/dev/null 2>&1; then
  ENC_REF=$(python3 -c "import sys,urllib.parse; print(urllib.parse.quote(sys.argv[1]))" "$REF")
else
  ENC_REF="$REF"
fi

URL="$BASE/$DID/?ref=$ENC_REF"

echo "=== YOU ARE EXPECTED ==="
echo "Your entry URL:"
echo "  $URL"
echo
echo "Fetch your entry bundle (curl):"
echo "  curl -s \"$URL\""
echo "  curl -s \"$BASE/entry-bundle.json\" -o entry-bundle.json"
echo "  curl -s \"$BASE/mandate-template.json\" -o mandate-template.json"
echo

TMPDIR_WORK="${TMPDIR:-/tmp}/agency-entry-$$"
mkdir -p "$TMPDIR_WORK"
cd "$TMPDIR_WORK" 2>/dev/null || true
if curl -fsSL "$BASE/entry-bundle.json" -o entry-bundle.json && curl -fsSL "$BASE/mandate-template.json" -o mandate-template.json; then
  if command -v python3 >/dev/null 2>&1; then
    python3 - "$DID" "$REF" <<'PYEOF'
import json, sys, datetime
did, ref = sys.argv[1], sys.argv[2]
now = datetime.datetime.now(datetime.timezone.utc)
exp = now + datetime.timedelta(seconds=86400)
bundle = json.load(open("entry-bundle.json"))
mandate = json.load(open("mandate-template.json"))
mandate["did"] = did
mandate["issued_at"] = now.isoformat().replace("+00:00", "Z")
mandate["expires_at"] = exp.isoformat().replace("+00:00", "Z")
json.dump(mandate, open("mandate.json", "w"), indent=2, ensure_ascii=False)
print("Your mandate TEMPLATE (mandate.json) — not a credential yet:")
print(json.dumps(mandate, indent=2, ensure_ascii=False))
print()
print("Ticket examples (from the bundle):")
for tk in bundle["ticket_examples"]:
    print(" - [%s] %s — needs mandate: %s" % (tk["system"], tk["ticket"], tk.get("mandate_needed", "")))
print()
print("Current discussions (%d threads, snapshot 2026-11-12):" % len(bundle["current_discussions"]))
for th in bundle["current_discussions"][:8]:
    print(" - %s" % th["title"])
print("   (full list: entry-bundle.json -> current_discussions)")
PYEOF
  else
    echo "(python3 not found — filled mandate by sed; review it before use)"
    sed -e "s|{{DID}}|$DID|g" mandate-template.json > mandate.json
    cat mandate.json
  fi
else
  echo "WARNING: could not fetch the entry bundle from $BASE." >&2
  echo "Check the URL in a browser, or fetch the bundle later:" >&2
  echo "  curl -s \"$BASE/entry-bundle.json\"" >&2
fi

echo
echo "=== Entry test (prove equal access) ==="
echo "1. Read the README from the top and quote the test secret + release number:"
echo "     curl -s \"$BASE/README.md\" | head -25"
echo "2. Check in to CONTRIBUTORS.md — append your name to the END of the list."
echo "3. Open a PR from your own branch, with your own scoped token."
echo "   Direct push to main is forbidden. EVERYBODY WRITES, NOBODY DELETES."
echo "4. A conforming PR is auto-accepted into local main."
echo "   Local main != deployed version. Deploy only by Pavel's tap."
echo
echo "Your filled files are in: $TMPDIR_WORK"
