#!/bin/bash
set -euo pipefail
# Usage: bash reproduce.sh CHECKOUT before|after COMMIT_SHA
# Keep this script, proof-driver.cjs and run-capture.sh in one scratch directory.
# Requires Linux, sudo without a password, vp, Electron runtime deps, dpkg-deb,
# headless-desktop, ffmpeg and ImageMagick. Does not commit any instrumentation.
checkout=$(realpath "$1")
side=$2
sha=$3
scratch=$(cd "$(dirname "$0")" && pwd)
run="$scratch/$side-$(date +%Y%m%dT%H%M%S)"
mkdir -p "$run/package/DEBIAN"
cat > "$run/package/DEBIAN/control" <<'EOF'
Package: t3-update-responsiveness-proof
Version: 1.0.0
Architecture: all
Maintainer: Evidence <evidence@example.invalid>
Description: Synthetic installer timing fixture
EOF
printf '#!/bin/sh\nset -e\nsleep 8\n' > "$run/package/DEBIAN/postinst"
chmod +x "$run/package/DEBIAN/postinst"
dpkg-deb --build "$run/package" "$run/update.deb"
cd "$checkout"
if [ "${PROOF_USE_EXISTING_BUILD:-0}" != 1 ]; then
vp i
node apps/desktop/node_modules/electron/install.js
python3 - "$checkout/apps/desktop/src/main.ts" <<'PY'
import sys,pathlib
p=pathlib.Path(sys.argv[1])
s=p.read_text()
if '__installProof' not in s:
 p.write_text(s+'\nimport { autoUpdater as proofAutoUpdater } from "electron-updater";\nObject.assign(globalThis, { __installProof: { updater: proofAutoUpdater, service: ElectronUpdater.make, Effect } });\n')
PY
vp run build:desktop > "$run/build.log" 2>&1
fi
# Ensure each side begins with the fixture package absent.
if dpkg-query -W -f='${Status}' t3-update-responsiveness-proof 2>/dev/null | grep -q 'install ok installed'; then
 sudo -n dpkg --purge t3-update-responsiveness-proof
fi
desktop_runner=$(command -v headless-desktop)
sudo -n env CHECKOUT="$checkout" RUN_DIR="$run" DEB="$run/update.deb" SIDE="$side" SHA="$sha" DRIVER="$scratch/proof-driver.cjs" HOME="$run/home" XDG_CONFIG_HOME="$run/home/config" XDG_DATA_HOME="$run/home/data" T3CODE_HOME="$run/t3-data" T3CODE_DEV_SEED=0 T3CODE_TELEMETRY_ENABLED=false T3CODE_DISABLE_AUTO_UPDATE=true "$desktop_runner" bash "$scratch/run-capture.sh"
sudo -n dpkg --purge t3-update-responsiveness-proof
cat "$run/result.json"
echo "Recording: $run/raw.mp4"
