#!/usr/bin/env bash
set -euo pipefail
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
OUT="$HERE/build_sft3"
mkdir -p "$OUT"
cp "$HERE/../sft3/sft3_target_inference_and_certificates.tex" "$OUT/"
cp "$HERE/../sft3/COC_definition.tex" "$OUT/"
cp "$HERE/../sft3/COC_checklist.tex" "$OUT/"
cp "$HERE/../sft3/COC_universality_schema.tex" "$OUT/"
cp "$HERE/../sft3/COC_rotation_proof.tex" "$OUT/"
cp "$HERE/../sft3/COC_embedded_loop_proof.tex" "$OUT/"
cp "$HERE/../sft3/COC_multicycle_proof.tex" "$OUT/"
cp "$HERE/../sft3/WELL_POSEDNESS_SPINE.tex" "$OUT/"
cp "$HERE/../sft3/TARGET_CORRECTNESS_AXIOMS.tex" "$OUT/"
cp "$HERE/../sft3/TARGET_IDENTIFIABILITY_THEOREM.tex" "$OUT/"
cp "$HERE/../sft3/TARGET_DECISION_FLOW.tex" "$OUT/"
cp "$HERE/../sft3/PREDICATE_EXHAUSTION_LEMMAS.tex" "$OUT/"
cp "$HERE/../sft3/VALIDATOR_PREDICATE_SPEC.tex" "$OUT/"
cp "$HERE/../sft3/WOB_CATALOG_COMPLETENESS.tex" "$OUT/"
cp "$HERE/../sft3/sft3_bibliography.bib" "$OUT/"
cp "$HERE/../sft3/sft3_target_inference_and_certificates.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 sft3_target_inference_and_certificates.tex
if command -v bibtex >/dev/null 2>&1; then
  bibtex sft3_target_inference_and_certificates || true
fi
pdflatex -interaction=nonstopmode -halt-on-error -file-line-error sft3_target_inference_and_certificates.tex
pdflatex -interaction=nonstopmode -halt-on-error -file-line-error sft3_target_inference_and_certificates.tex
echo "Built in $OUT"
