#!/usr/bin/env bash
# Real demos for P204 "Ollama: Full Tutorial for Beginners". Made on the Mac (Docker Desktop: 10 CPUs, 7.7 GB of
# memory, no GPU), no account or key. Two computers on one lab network, both a stock Ubuntu 24.04 with systemd as
# PID 1 (lab/Dockerfile):
#   laptop   where Ollama is installed on camera with Ollama's own install script (curl -fsSL https://ollama.com/install.sh | sh),
#            which makes the ollama user and the systemd service; every model is pulled on camera from ollama.com
#   desktop  a second computer that asks the laptop's Ollama for answers once it listens on the network
# Every command shown is typed into a real interactive bash in a pty, one window per computer (demo_driver.py).
# Every line in video.json is read from demo_output.json; the two files written off camera are in demo_files.json.
set -euo pipefail
cd "$(dirname "$0")"
P=ce-p204
cleanup() {
  for b in laptop desktop; do docker rm -f $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 -t $P lab >/dev/null
docker network create $P >/dev/null
for b in laptop desktop; do
  docker run -d --name $P-$b --hostname $b --network $P --network-alias $b --privileged --cgroupns=private \
    --tmpfs /run --tmpfs /run/lock $P >/dev/null
done
for b in laptop desktop; do
  for i in $(seq 60); do docker exec $P-$b systemctl is-system-running 2>/dev/null | grep -qE 'running|degraded' && break; sleep 1; done
done
python3 demo_driver.py $P
