#!/usr/bin/env bash
# Real demos for P181 "Every Git Setting to Change on Day One". Made on the Mac (Docker Desktop).
# Linda and Rob keep their household chore chart in Git. One container from a stock Ubuntu 24.04 image (lab/Dockerfile)
# is the home computer: Linda has just started on it with no Git settings at all, Rob's account is already set up, and
# the shared repository is a bare repository on the same box. Linda types every command into one real interactive bash
# in a pty (demo_driver.py); Rob's pushes and file edits (what either would 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-p181-laptop
cleanup() { docker rm -f $C >/dev/null 2>&1 || true; }
cleanup
[ -n "${KEEP:-}" ] || trap cleanup EXIT
docker build -q -t ce-p181 lab >/dev/null
# laptop.home: like a home router's DHCP name, so Git can guess an email from it
docker run -d --name $C --hostname laptop.home ce-p181 sleep infinity >/dev/null
as() { docker exec -u "$1" -w "/home/$1" -e HOME="/home/$1" $C bash -c "$2"; }
as rob "git init -q --bare --shared=0777 -b main /srv/git/chores.git \
  && git config --global user.name Rob && git config --global user.email rob@example.com \
  && git config --global init.defaultBranch main && git config --global pull.rebase true \
  && git config --global --add safe.directory '*'"
# The shared repository belongs to Linda's account (Git refuses a repository another user owns), so her clone needs no
# setting; Rob's account allows it with safe.directory, off screen.
as rob "git clone -q /srv/git/chores.git 2>/dev/null && cd chores \
  && printf '# Chore chart\n\nDishes: Rob\nLaundry: Linda\nBins: Linda\nHoovering: Rob\n' > chores.md \
  && git add . && git commit -qm 'Start the chore chart' && sleep 1 \
  && printf '# Chore chart\n\nDishes: Rob\nLaundry: Linda\nBins: Linda\nHoovering: Rob\nPlants: Linda\n' > chores.md \
  && git commit -qam 'Add the plants' && git push -q origin main"
docker exec $C chown -R linda /srv/git/chores.git
python3 demo_driver.py "$C"
