#!/usr/bin/env bash
# Run after the evidence worktree has been cleaned; no PR checkout edits.
set -euo pipefail
ROOT="/tmp/lead-23005-r2/capture/reproduce-$(date -u +%Y%m%dT%H%M%S)"
mkdir -p "$ROOT/src-head" "$ROOT/tmp" "$ROOT/artifacts/base-old" "$ROOT/artifacts/head-old" "$ROOT/artifacts/head-target"
git -C /home/ubuntu/t3code worktree add /home/ubuntu/wt/pr-23005-base 8d3b061121fe65669d5d43d011b48cd3027a4108
git -C /home/ubuntu/wt/pr-23005 archive 87ac5b3a34fe2c12561bf16f6f823e0376fda004 > "$ROOT/head.tar"
tar -xf "$ROOT/head.tar" -C "$ROOT/src-head"
python3 - "$ROOT" <<'PY'
import yaml,json,pathlib,sys
r=pathlib.Path(sys.argv[1]);v=yaml.safe_load(pathlib.Path('/home/ubuntu/wt/pr-23005-base/pnpm-workspace.yaml').read_text());o=v['overrides']
for k,val in o.items():
 if val=='catalog:':o[k]=str(v['catalog'][k.split('>')[-1].split('@^')[0]])
o['node-pty']='1.2.0-beta.15';(r/'build-overrides.json').write_text(json.dumps(o))
PY
for source in /home/ubuntu/wt/pr-23005-base "$ROOT/src-head"; do
 (cd "$source" && vp install && node apps/desktop/node_modules/electron/install.js)
 for crate in resource-monitor kde-snap-shot hyprland-snap-shot; do
  ln -s "$ROOT/cargo-target" "$source/native/$crate/target"
 done
done
build() {
 [ "$(df -BG --output=avail / | tail -1 | tr -dc 0-9)" -ge 5 ] || exit 55
 env -u VITE_DEV_SERVER_URL -u VITE_HTTP_URL -u VITE_WS_URL TMPDIR="$ROOT/tmp" CARGO_TARGET_DIR="$ROOT/cargo-target" pnpm_config_overrides="$(cat "$ROOT/build-overrides.json")" T3CODE_DESKTOP_UPDATE_REPOSITORY= GITHUB_REPOSITORY= vp run dist:desktop:linux --mock-updates --mock-update-server-port 19305 --build-version "$1" --output-dir "$2" --verbose
}
(cd /home/ubuntu/wt/pr-23005-base && build 90.23005.0 "$ROOT/artifacts/base-old") > "$ROOT/build-base-old.log" 2>&1
(cd "$ROOT/src-head" && build 90.23005.0 "$ROOT/artifacts/head-old") > "$ROOT/build-head-old.log" 2>&1
(cd "$ROOT/src-head" && build 90.23005.1 "$ROOT/artifacts/head-target") > "$ROOT/build-head-target.log" 2>&1
