#!/usr/bin/env bash
# Builds the R1 identity-parity fixture under $1 (fresh dir).
set -euo pipefail
F="$1"; rm -rf "$F"; mkdir -p "$F/ws" "$F/claude/projects" "$F/codex"
cd "$F/ws"
export GIT_CONFIG_NOSYSTEM=1 HOME="$F"
git config --global user.email ev@x; git config --global user.name ev; git config --global init.defaultBranch main
# 1 normal repo
git init -q normal && git -C normal remote add origin https://github.com/acme/normal.git
git -C normal commit -q --allow-empty -m i
# 2 linked worktree of a non-bare repo
git -C normal worktree add -q ../normal-wt -b wt1
# 3 bare repo + its linked worktree
git init -q --bare bare.git && git -C bare.git remote add origin git@github.com:acme/bare.git
git -C normal push -q ../bare.git main
git -C bare.git worktree add -q ../bare-wt main
# 4 submodule-style pointer to a non-worktree gitdir with a config
mkdir -p modules/sub sub && git init -q --bare modules/sub && git -C modules/sub config core.bare false \
  && git -C modules/sub remote add origin https://github.com/acme/sub.git
echo "gitdir: ../modules/sub" > sub/.git
# 5 .git dir with no config file
mkdir -p noconfig/.git
# 6 .git pointer with garbage
mkdir -p garbage && echo "this is not a pointer" > garbage/.git
# 7 cwd that is a regular file
echo hi > regular-file
# 8 plain non-git dir
mkdir -p plain
i=0
for d in normal normal-wt bare-wt sub noconfig garbage regular-file plain; do
  i=$((i+1))
  mkdir -p "$F/claude/projects/p$i"
  printf '{"type":"user","cwd":"%s","sessionId":"s%d"}\n{"type":"assistant"}\n' "$F/ws/$d" "$i" > "$F/claude/projects/p$i/a.jsonl"
  touch -d "2026-05-0${i}T00:00:00Z" "$F/claude/projects/p$i/a.jsonl"
done
cat sub/.git; cat bare-wt/.git; cat normal-wt/.git
