#!/usr/bin/env bash
# Real demos for P074 "The Linux Roadmap for Beginners". Made on the Mac (owner OK 2026-11-05), Docker Desktop.
# Two containers on their own network 192.168.50.0/24: "server", a stock Ubuntu 24.04 with systemd as PID 1
# (lab/Dockerfile.server: systemctl, journalctl and cron run for real; nginx is installed on camera), and
# "laptop", a plain Ubuntu 24.04 with ssh and curl. Every command shown is typed into a real interactive bash
# in a pty (demo_driver.py); sudo's and ssh's password prompts are answered by the driver, so the window shows
# the prompt and nothing typed. The server reaches the Ubuntu archive for apt, like any server.
set -euo pipefail
cd "$(dirname "$0")"
P=ce-p074
cleanup() { docker rm -f $P-server $P-laptop >/dev/null 2>&1 || true; docker network rm $P-net >/dev/null 2>&1 || true; }
cleanup
[ -n "${KEEP:-}" ] || trap cleanup EXIT
docker build -q -t $P-server -f lab/Dockerfile.server lab >/dev/null
docker build -q -t $P-laptop -f lab/Dockerfile.laptop lab >/dev/null
docker network create --subnet 192.168.50.0/24 $P-net >/dev/null
docker run -d --name $P-server --hostname server --network $P-net --ip 192.168.50.10 \
  --privileged --cgroupns=host -v /sys/fs/cgroup:/sys/fs/cgroup:rw --tmpfs /run --tmpfs /run/lock $P-server >/dev/null
docker run -d --name $P-laptop --hostname laptop --network $P-net --ip 192.168.50.20 $P-laptop sleep infinity >/dev/null
for i in $(seq 30); do
  s=$(docker exec $P-server systemctl is-system-running 2>/dev/null || true)
  [ "$s" = running ] || [ "$s" = degraded ] && break; sleep 1
done
docker exec $P-server systemctl is-active ssh cron >/dev/null
python3 - "$P" < demo_driver.py
