#!/usr/bin/env bash
# Real demos for P176 "How to Fix Git 'Detached HEAD' State". Made on the Mac (Docker Desktop).
# Jess and Joe are volunteers who keep a heritage steam railway's timetable website in Git. One container from a
# stock Ubuntu 24.04 image (lab/Dockerfile) is Jess's laptop; the station's shared repository is a bare repository on
# the same box. The history so far (three tagged timetables, two later commits) is made off camera; every command after
# that is typed into one real interactive bash in a pty (demo_driver.py), and file edits (what she'd do in an editor)
# are made off screen. Every line and commit ID in video.json is read from demo_output.json.
set -euo pipefail
cd "$(dirname "$0")"
C=ce-p176-laptop
cleanup() { docker rm -f $C >/dev/null 2>&1 || true; }
cleanup
[ -n "${KEEP:-}" ] || trap cleanup EXIT
docker build -q -t ce-p176 lab >/dev/null
docker run -d --name $C --hostname laptop ce-p176 sleep infinity >/dev/null
as() { docker exec -u "$1" -w "/home/$1" -e HOME="/home/$1" $C bash -c "$2"; }
as jess "git init -q --bare --shared=0777 -b main /srv/git/timetable.git"
for u in jess joe; do U=$(echo ${u:0:1} | tr a-z A-Z)${u:1}
  as $u "git config --global user.name $U && git config --global user.email $u@example.com \
    && git config --global init.defaultBranch main && git config --global --add safe.directory '*'"
done
# The history: Joe made the first timetables, Jess the winter one (printed on the station's posters as v1.2).
as joe "git clone -q /srv/git/timetable.git 2>/dev/null && cd timetable \
  && printf '<h1>Oak Hill Steam Railway</h1>\n<p>Saturday: 10:15 Oak Hill to Millbrook</p>\n' > timetable.html \
  && git add . && git commit -qm 'First timetable' && sleep 1 && git tag v1.0 \
  && printf '<h1>Oak Hill Steam Railway</h1>\n<p>Saturday: 10:15 Oak Hill to Millbrook</p>\n<p>Sunday: 11:30 Oak Hill to Millbrook</p>\n' > timetable.html \
  && git commit -qam 'Add Sunday trains' && sleep 1 && git tag v1.1 && git push -q --tags origin main"
as jess "git clone -q /srv/git/timetable.git 2>/dev/null && cd timetable \
  && printf '<h1>Oak Hill Steam Railway: winter</h1>\n<p>Saturday: 10:15 Oak Hill to Millbrook</p>\n<p>Sunday: 11:30 Oak Hill to Millbrook</p>\n<p>Last train: 15:40</p>\n' > timetable.html \
  && git commit -qam 'Winter timetable' && git tag v1.2 && sleep 1 \
  && printf '<h1>Santa Specials</h1>\n<p>December weekends, booking essential</p>\n' > santa.html \
  && git add santa.html && git commit -qm 'Add the Santa Specials page' && sleep 1 \
  && printf '<h1>Book a seat</h1>\n<p>Phone the booking office</p>\n' > booking.html \
  && git add booking.html && git commit -qm 'Add the booking page' && git push -q --tags origin main"
python3 demo_driver.py "$C"
