#!/usr/bin/env bash
set -euo pipefail
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
OUT="$HERE/build_sft2"
mkdir -p "$OUT"
cp "$HERE/../sft2/sft2_syncre_stability_modulus.tex" "$OUT/"
cp "$HERE/../sft2/COC_definition.tex" "$OUT/"
cp "$HERE/../sft2/COC_checklist.tex" "$OUT/"
cp "$HERE/../sft2/COC_universality_schema.tex" "$OUT/"
cp "$HERE/../sft2/COC_rotation_proof.tex" "$OUT/"
cp "$HERE/../sft2/COC_embedded_loop_proof.tex" "$OUT/"
cp "$HERE/../sft2/COC_multicycle_proof.tex" "$OUT/"
cp "$HERE/../sft2/WELL_POSEDNESS_SPINE.tex" "$OUT/"
cp "$HERE/../sft2/TARGET_CORRECTNESS_AXIOMS.tex" "$OUT/"
cp "$HERE/../sft2/TARGET_DECISION_FLOW.tex" "$OUT/"
cp "$HERE/../sft2/PREDICATE_EXHAUSTION_LEMMAS.tex" "$OUT/"
cp "$HERE/../sft2/VALIDATOR_PREDICATE_SPEC.tex" "$OUT/"
cp "$HERE/../sft2/WOB_CATALOG_COMPLETENESS.tex" "$OUT/"
cp "$HERE/../sft2/sft2_bibliography.bib" "$OUT/"
cp "$HERE/../sft2/sft2_syncre_stability_modulus.bbl" "$OUT/" 2>/dev/null || true
cd "$OUT"
if ! command -v pdflatex >/dev/null 2>&1; then
  echo "pdflatex not found; compile locally in $OUT"
  exit 0
fi
pdflatex -interaction=nonstopmode -halt-on-error -file-line-error sft2_syncre_stability_modulus.tex
if command -v bibtex >/dev/null 2>&1; then
  bibtex sft2_syncre_stability_modulus || true
fi
pdflatex -interaction=nonstopmode -halt-on-error -file-line-error sft2_syncre_stability_modulus.tex
pdflatex -interaction=nonstopmode -halt-on-error -file-line-error sft2_syncre_stability_modulus.tex
echo "Built in $OUT"
