#!/usr/bin/env bash
# Real demos for P219 "Git Merge vs Rebase: Which One Should You Use?". Made on the Mac (Docker Desktop).
# One container from a stock Ubuntu 24.04 image (lab/Dockerfile) is your laptop; the team's shared repository is a bare
# repository on the same box. The history so far is made off camera: two commits on main, your branch login with two
# commits (already pushed), then a teammate's two new commits on main. Every command after that is typed into one real
# interactive bash in a pty (demo_driver.py); file edits (what you'd do in an editor) are made off screen and recorded
# as labelled steps. Every line and commit ID in video.json is read from demo_output.json.
set -euo pipefail
cd "$(dirname "$0")"
C=ce-p219-laptop
cleanup() { docker rm -f $C >/dev/null 2>&1 || true; }
cleanup
[ -n "${KEEP:-}" ] || trap cleanup EXIT
docker build -q -t ce-p219 lab >/dev/null
docker run -d --name $C --hostname laptop ce-p219 sleep infinity >/dev/null
as() { docker exec -u "$1" -w "/home/$1" -e HOME="/home/$1" $C bash -c "$2"; }
# --shared also sets receive.denyNonFastForwards, which refuses every forced push; hosted Git servers allow one on
# an unprotected branch, so the lab server does too.
as dev "git init -q --bare --shared=0777 -b main /srv/git/app.git \
  && git -C /srv/git/app.git config receive.denyNonFastForwards false"
for u in dev mate; 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
# main: the app's first two commits.
as dev "git clone -q /srv/git/app.git 2>/dev/null && cd app \
  && printf '# My app\n' > README.md && git add . && git commit -qm 'Start the app' && sleep 1 \
  && printf '<h1>Welcome</h1>\n<p>Home page</p>\n' > index.html && git add . && git commit -qm 'Add the home page' \
  && git push -q origin main && sleep 1"
# Your branch, login: two commits, pushed (so the server has them).
as dev "cd app && git switch -qc login \
  && printf '<form>\n  <input name=\"email\">\n  <input name=\"password\" type=\"password\">\n</form>\n' > login.html \
  && git add . && git commit -qm 'Add the login form' && sleep 1 \
  && printf 'def check(password):\n    return len(password) >= 12\n' > login.py \
  && git add . && git commit -qm 'Check the password' && git push -qu origin login 2>/dev/null && sleep 1"
# Meanwhile a teammate adds two commits to main.
as mate "git clone -q /srv/git/app.git && cd app \
  && printf '<h1>Prices</h1>\n<p>Basic: 5 a month</p>\n' > prices.html && git add . && git commit -qm 'Add the prices page' && sleep 1 \
  && printf '<h1>Welcome to the shop</h1>\n<p>Home page</p>\n' > index.html && git commit -qam 'Change the home page title' \
  && git push -q origin main && sleep 1"
# Your local main catches up (git pull on main, off camera); you stay on login.
as dev "cd app && git switch -q main && git pull -q && git switch -q login && git remote set-head origin -d"
python3 demo_driver.py "$C"
