#!/usr/bin/env bash
# Builds a project with native pdflatex + bibtex from the same official TeX Live image the tree comes
# from. Used to tell engine differences (ours vs native) from TeX Live version differences.
# Usage: scripts/native-reference.sh <project-dir> <main.tex> <out.pdf>
set -euo pipefail
HERE="$(cd "$(dirname "$0")/.." && pwd)"
IMAGE="$(cat "$HERE/scripts/tree/texlive-image.txt")"
SRC="$(cd "$1" && pwd)"
MAIN="$2"
OUT="$(cd "$(dirname "$3")" && pwd)/$(basename "$3")"
WORK="$(mktemp -d)"
cp -R "$SRC/." "$WORK/"
rm -f "$WORK/${MAIN%.tex}.pdf"
docker run --rm -v "$WORK:/w" -w /w "$IMAGE" sh -c \
  "pdflatex -interaction=nonstopmode $MAIN >/dev/null; if grep -q bibdata ${MAIN%.tex}.aux; then bibtex ${MAIN%.tex} >/dev/null; fi; pdflatex -interaction=nonstopmode $MAIN >/dev/null; pdflatex -interaction=nonstopmode $MAIN >/dev/null; true"
cp "$WORK/${MAIN%.tex}.pdf" "$OUT"
rm -rf "$WORK"
echo "$OUT"
