#!/usr/bin/env bash
# Real demos for P027 "Every Linux File Permission You Need to Know" (Linux for Beginners, episode 6).
# The series' Ubuntu 24.04 machine (hostname "ubuntu"): stock image plus sudo, as a Docker container on render01.
# Two people work on it: ana (the admin, in the sudo group) and ben; both are in a group called team.
# Each terminal is a real login (`su - <user>` in a pty, so PAM sets the umask as on a real Ubuntu login),
# every command runs as the user the prompt shows, and its real output goes to demo_output.json;
# build_script.py reads every value from there. Nothing outside the container is touched.
set -euo pipefail
cd "$(dirname "$0")"
BOX=ce-p027
W=/tmp/ce-p027
cleanup() { docker rm -f $BOX >/dev/null 2>&1 || true; }
cleanup
[ -n "${KEEP:-}" ] || trap cleanup EXIT
rm -rf $W && mkdir -p $W
cat > $W/Dockerfile <<'DOCKER'
FROM ubuntu:24.04
RUN apt-get update -qq && DEBIAN_FRONTEND=noninteractive apt-get install -y -qq sudo procps >/dev/null \
 && rm -rf /var/lib/apt/lists/* && (userdel -r ubuntu 2>/dev/null || true)
DOCKER
docker build -q -t ce-p027-ubuntu $W >/dev/null
docker run -d --name $BOX --hostname ubuntu ce-p027-ubuntu sleep infinity >/dev/null
python3 - "$BOX" <demo_driver.py
