#!/usr/bin/env bash
# Real demos for P100 "How Linux systemd Works". Made on the Mac (Docker Desktop), like P074.
# One container, "shop": a stock Ubuntu 24.04 with systemd as PID 1 (lab/Dockerfile.server), run with
# --restart always so a real `sudo reboot` inside it ends systemd and Docker boots it again. Ana's tea shop app
# (lab/teashop/) and its unit file (lab/teashop.service) are copied in, as if she wrote them in nano. Every command
# shown is typed into a real interactive bash in a pty (demo_driver.py); sudo's password prompt is answered by the
# driver, so the window shows the prompt and nothing typed. The app only listens inside the container.
set -euo pipefail
cd "$(dirname "$0")"
cleanup() { docker rm -f ce-p100-server >/dev/null 2>&1 || true; }
cleanup
[ -n "${KEEP:-}" ] || trap cleanup EXIT
docker build -q -t ce-p100-server -f lab/Dockerfile.server lab >/dev/null
docker run -d --name ce-p100-server --hostname shop --restart always \
  --privileged --cgroupns=private -v /sys/fs/cgroup:/sys/fs/cgroup:rw --tmpfs /run --tmpfs /run/lock \
  ce-p100-server >/dev/null
python3 - ce-p100 < demo_driver.py
