#!/usr/bin/env bash
# Usage: bash reproduce.sh /absolute/path/to/main-checkout /absolute/path/to/scratch
# Requires sudo, bubblewrap, vp and ghdrop. Put root-folders-separate.e2e.ts in scripts/scenarios.
set -euo pipefail
checkout="$1"
scratch="$2"
mkdir -p "$scratch/root/app" "$scratch/root/tools"
printf 'app sample\n' > "$scratch/root/app/README.md"
printf 'tools sample\n' > "$scratch/root/tools/README.md"
git -C "$scratch/root" init -q
git -C "$scratch/root" add .
git -C "$scratch/root" -c user.name=Scenario -c user.email=scenario@example.invalid commit -qm 'Sample folders'
git -C "$scratch/root" remote add origin https://github.com/example-org/root-repo.git
printf '[safe]\n\tdirectory = /\n' > "$scratch/gitconfig"
cd "$checkout"
base=$(git merge-base origin/main ec4c7570598cea048982dd7c487e7888705ce529)
exec sudo -n bwrap --die-with-parent --tmpfs / \
 --ro-bind /usr /usr --symlink usr/bin /bin --symlink usr/sbin /sbin \
 --symlink usr/lib /lib --symlink usr/lib64 /lib64 --ro-bind /etc /etc \
 --bind /home /home --bind /tmp /tmp --bind /var /var --bind /run /run \
 --proc /proc --dev /dev \
 --bind "$scratch/root/.git" /.git --bind "$scratch/root/app" /app --bind "$scratch/root/tools" /tools \
 --chdir "$checkout" /usr/bin/setpriv --reuid="$(id -u)" --regid="$(id -g)" --init-groups \
 /usr/bin/env HOME="$HOME" PATH="$PATH" \
 GIT_DISCOVERY_ACROSS_FILESYSTEM=1 GIT_CONFIG_GLOBAL="$scratch/gitconfig" \
 T3_SCENARIO_INHERIT_ENV=GIT_DISCOVERY_ACROSS_FILESYSTEM,GIT_CONFIG_GLOBAL T3CODE_DEV_SEED=0 \
 vp run scenarios:prove scripts/scenarios/root-folders-separate.e2e.ts \
 --base "$base" --head ec4c7570598cea048982dd7c487e7888705ce529 --upload-command 'ghdrop upload'
