#!/usr/bin/env bash
# Real demos for P061 "How to Fix Git Merge Conflicts". Runs on render01 (Docker).
# A small lab on this machine only, like P005's: a Git server standing in for GitHub (git-server: SSH + a bare
# repository, shop.git) and two laptops, Ana's and Ben's, who both edit the tea shop's menu page.
# Commands are typed into real interactive shells (one pty per laptop, demo_driver.py), as the viewer sees them;
# commands + real output go to demo_output.json. File edits (what Ana or Ben would do in an editor) are made
# off-screen; every commit, push, pull, merge and rebase is on screen.
# Every line, commit ID and message in video.json is read from here. Re-run = new IDs = rebuild the script.
set -euo pipefail
cd "$(dirname "$0")"
P=ce-p061
BOXES="git-server ana-laptop ben-laptop"
cleanup() {
  for b in $BOXES; do docker rm -f $P-$b >/dev/null 2>&1 || true; done
  docker network rm $P-net >/dev/null 2>&1 || true
}
cleanup
trap cleanup EXIT
W=/tmp/ce-p061
rm -rf $W && mkdir -p $W/img

cat > $W/img/Dockerfile <<'EOF'
FROM ubuntu:24.04
RUN apt-get update -qq && DEBIAN_FRONTEND=noninteractive apt-get install -y -qq --no-install-recommends \
      git openssh-client openssh-server ca-certificates >/dev/null \
 && rm -rf /var/lib/apt/lists/* && userdel -r ubuntu 2>/dev/null; \
    useradd -m -s /usr/bin/git-shell git && useradd -m -s /bin/bash ana && useradd -m -s /bin/bash ben
EOF
docker build -q -t ce-p061-git $W/img >/dev/null
docker network create $P-net >/dev/null
for b in $BOXES; do
  docker run -d --name $P-$b --hostname $b --network $P-net --network-alias $b ce-p061-git sleep infinity >/dev/null
done
ex() { docker exec "$P-$1" bash -c "$2"; }
as() { docker exec -u "$2" -w "/home/$2" -e HOME="/home/$2" "$P-$1" bash -c "$3"; }

# The server: SSH, user git (git-shell: it can only run git), one bare repository.
ex git-server "mkdir -p /run/sshd && /usr/sbin/sshd"; sleep 2
as git-server git "git init -q --bare -b main shop.git && mkdir -p .ssh && chmod 700 .ssh"
for u in ana ben; do
  # pull.rebase false: git pull merges (Git 2.27+ asks you to choose; merging is the classic default).
  as $u-laptop $u "mkdir -p -m 700 .ssh && ssh-keygen -q -t ed25519 -N '' -f .ssh/id_ed25519 && ssh-keyscan git-server >> .ssh/known_hosts 2>/dev/null \
    && git config --global user.name ${u^} && git config --global user.email $u@example.com \
    && git config --global init.defaultBranch main && git config --global pull.rebase false"
  docker exec $P-$u-laptop cat /home/$u/.ssh/id_ed25519.pub | docker exec -i -u git $P-git-server sh -c 'cat >> ~/.ssh/authorized_keys'
done
ex git-server "chmod 600 /home/git/.ssh/authorized_keys"

# The project so far: Ana made the menu page and pushed it; Ben cloned it.
as ana-laptop ana "git clone -q git@git-server:shop.git 2>/dev/null && cd shop \
  && printf '<h1>Tea menu</h1>\n<p>Sencha: \$4</p>\n<p>Matcha: \$5</p>\n<p>Earl Grey: \$3</p>\n<p>Chai: \$4</p>\n<footer>Open 9 to 5</footer>\n' > menu.html \
  && git add menu.html && git commit -qm 'Add the tea menu' && git push -q origin main 2>/dev/null"
as ben-laptop ben "git clone -q git@git-server:shop.git"

python3 demo_driver.py "$P"
