#!/usr/bin/env bash
# Real demos for P107 "SQL Databases: SQLite vs PostgreSQL". Made on the Mac (Docker Desktop). A small real estate
# office's listings site, from one stock Ubuntu 24.04 image (lab/Dockerfile):
#   office   the office server that runs the site: Ubuntu's sqlite3 3.45.1 and PostgreSQL 16, Python 3.12; user joe
#   jess-pc  Jess's PC on the office network: Ubuntu's sqlite3 and psql (client only); user jess
# Every command shown is typed into a real interactive bash (and sqlite3 / psql) in a pty (demo_driver.py); every
# error, time and count is real.
set -euo pipefail
cd "$(dirname "$0")"
P=ce-p107
cleanup() {
  for b in office pc; do docker rm -f -v $P-$b >/dev/null 2>&1 || true; done
  docker network rm $P >/dev/null 2>&1 || true
}
cleanup
[ -n "${KEEP:-}" ] || trap cleanup EXIT
docker build -q --target office -t $P-office lab >/dev/null
docker build -q --target pc -t $P-pc lab >/dev/null
docker network create $P >/dev/null
docker run -d --name $P-office --hostname office --network $P $P-office >/dev/null
docker run -d --name $P-pc --hostname jess-pc --network $P $P-pc >/dev/null
for i in $(seq 60); do
  docker exec $P-office su postgres -c "pg_isready -q" 2>/dev/null && break
  sleep 1
done
[ -n "${NODRIVER:-}" ] || python3 demo_driver.py $P
