#!/usr/bin/env bash
# Real demos for P197 "How the Amazon Ergo AI Model Works". Made on the Mac (Docker Desktop) against REAL Amazon
# Bedrock in our own cloudexplainers AWS account (us-east-1), inside the owner's real-AWS demo budget (under $1 a
# month; one run costs about 15 cents: Ergo 1.0 at $1 in / $3 out per million tokens, about 50,000 output tokens,
# most of them the seven 600-word-guide calls), and against the public Hugging Face Hub (read-only, no account).
#   laptop   your laptop: stock Ubuntu 24.04, the AWS CLI v2 copied from the official amazon/aws-cli image, curl, jq.
# Credentials: a one-hour STS session (assume-role from the Mac's default profile into the account's admin role) cut
# down by a session policy (lab/session-policy.json: list models and profiles, call models, read prices; nothing
# else), passed into the container as environment variables and never printed. No IAM user or key is created, and
# the demo creates nothing in the account.
# One real interactive bash in a pty (demo_driver.py).
set -euo pipefail
cd "$(dirname "$0")"
P=ce-p197
ROLE=arn:aws:iam::346882411366:role/OrganizationAccountAccessRole
cleanup() { docker rm -f $P-laptop >/dev/null 2>&1 || true; }
cleanup
[ -n "${KEEP:-}" ] || trap cleanup EXIT
read -r AK SK ST < <(aws --profile default sts assume-role --role-arn $ROLE --role-session-name ergo-laptop \
  --policy file://lab/session-policy.json --duration-seconds 3600 \
  --query 'Credentials.[AccessKeyId,SecretAccessKey,SessionToken]' --output text)
docker build -q -t $P lab >/dev/null
docker run -d --name $P-laptop --hostname laptop -e AWS_ACCESS_KEY_ID="$AK" -e AWS_SECRET_ACCESS_KEY="$SK" \
  -e AWS_SESSION_TOKEN="$ST" -e AWS_REGION=us-east-1 $P >/dev/null
python3 demo_driver.py
