#!/usr/bin/env bash
# Real demos for P037 "Linux Cron Jobs Will Finally Make Sense After This".
# One Ubuntu 24.04 LXD system container, "shop" (Ana's tea shop server), on its own bridge (ce-p037, 10.37.0.0/24),
# deleted at the end. Ana's nightly backup script (lab/backup.sh) calls a small tool installed by hand in
# ~/.local/bin (lab/shop-export). Every terminal is a real login shell in a pty (demo_driver.py); every cron run
# is the real cron daemon firing at a real minute, so the demo waits for the clock (~8 minutes).
#
# Needs: the LXD snap on render01 (ubuntu:24.04 image; the image ships cron, no mail server).
set -euo pipefail
cd "$(dirname "$0")"
P=ce-p037
NET=ce-p037
cleanup() {
  lxc delete -f $P-shop >/dev/null 2>&1 || true
  lxc network delete $NET >/dev/null 2>&1 || true
}
cleanup
[ -n "${KEEP:-}" ] || trap cleanup EXIT
lxc network create $NET ipv4.address=10.37.0.1/24 ipv4.nat=true ipv6.address=none >/dev/null
lxc init -q ubuntu:24.04 $P-shop --network $NET
lxc start $P-shop
until lxc exec $P-shop -- systemctl is-system-running 2>/dev/null | grep -qE 'running|degraded'; do sleep 1; done
lxc exec $P-shop -- cloud-init status --wait >/dev/null || true
lxc exec $P-shop -- hostnamectl set-hostname shop
lxc exec $P-shop -- systemctl restart rsyslog cron
lxc file push -q lab/backup.sh lab/shop-export lab/orders.txt $P-shop/root/
python3 - "$P" <demo_driver.py
