#!/usr/bin/env bash
# Real demos for P044 "How to Fix Gemini API Error 429: Resource Exhausted". Runs on the Mac (Docker Desktop):
# the Gemini API keys live only on the Mac, never on render01.
# One container, ce-p044-laptop: a stock Ubuntu 24.04 with curl, jq and Google's Gen AI SDK (lab/Dockerfile),
# Ana's shop scripts in ~/shop. Every command shown is typed into a real interactive bash in a pty
# (demo_driver.py) and sent to the real Gemini API. Two keys, never printed (the driver refuses to save any
# output that contains either):
#   GEMINI_API_KEY  the free-tier key of the "Default Gemini Project" (no billing): every 429 in the video is real
#   PAID_KEY        the key of our paid project ("My First Project", billing on; owner OK 2026-11-06 for P043's
#                   demo), used for one request only, to show what billing changes
# The free tier's daily quota resets at midnight Pacific; the driver tops up gemini-3.7-flash's 20 requests a day
# off camera if they aren't already used, so its daily 429 is real either way.
set -euo pipefail
cd "$(dirname "$0")"
P=ce-p044
KEYS="$(git rev-parse --show-toplevel)/.gemini_keys.json"
[ -f "$KEYS" ] || KEYS="$HOME/cloudexplainers/.gemini_keys.json"
cleanup() { docker rm -f $P-laptop >/dev/null 2>&1 || true; }
cleanup
[ -n "${KEEP:-}" ] || trap cleanup EXIT
docker build -q -t $P-laptop lab >/dev/null
key() { python3 -c "import json,sys; print([k for k in json.load(open(sys.argv[1]))['projects'][sys.argv[2]]['keys'] if k['display_name'] == sys.argv[3]][0]['key_string'])" "$KEYS" "$1" "$2"; }
GEMINI_API_KEY=$(key gen-lang-client-0388842080 "Default Gemini API Key")
PAID_KEY=$(key project-593e5bf6-d8b6-4ffd-878 "Gemini API Key")
export GEMINI_API_KEY PAID_KEY
docker run -d --name $P-laptop --hostname laptop -e GEMINI_API_KEY -e PAID_KEY $P-laptop sleep infinity >/dev/null
python3 - "$P" < demo_driver.py
