#!/usr/bin/env bash
# Real demos for P051 "OpenRouter vs Direct API Keys: Which One Should You Use?". Runs on the Mac (Docker Desktop):
# both keys live only on the Mac, never on render01.
# One container, ce-p051-laptop: a stock Ubuntu 24.04 with curl, jq and the official OpenAI Python library
# (lab/Dockerfile), Kari's grocery list app in ~/grocery. Every command shown is typed into a real interactive bash
# in a pty (demo_driver.py) and sent to the real APIs:
#  - Google's door: the Gemini API's own OpenAI-compatible endpoint, with the key of a throwaway Google Cloud project
#    that has no billing (ce-freetier-p045e, made for P045), so only its free tier exists and nothing can cost money.
#  - OpenRouter's door: the free-tier OpenRouter key with a $0 spending limit (owner rule: free models only).
# Off camera, the driver reads that project's own free-tier limits from the Service Usage API with gcloud's login
# on the Mac (numbers only) and the docs sentences the video quotes. The driver refuses any output with a key in it.
set -euo pipefail
cd "$(dirname "$0")"
export PROJECT=${PROJECT:-ce-freetier-p045e}
[ "$(gcloud billing projects describe "$PROJECT" --format='value(billingEnabled)')" = "False" ] ||
  { echo "the lab project must have no billing"; exit 1; }
P=ce-p051
ROOT="$(git rev-parse --show-toplevel)"
ORKEY="$ROOT/.openrouter_api_key"; [ -f "$ORKEY" ] || ORKEY="$HOME/cloudexplainers/.openrouter_api_key"
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
OPENROUTER_API_KEY=$(cat "$ORKEY")
GOOGLE_API_KEY=$(cat "${GKEYFILE:-$HOME/.ce-secrets/p045e_key}")
export OPENROUTER_API_KEY GOOGLE_API_KEY
docker run -d --name $P-laptop --hostname laptop -e OPENROUTER_API_KEY -e GOOGLE_API_KEY $P-laptop sleep infinity >/dev/null
python3 - "$P" < demo_driver.py
