#!/usr/bin/env bash
set -euo pipefail
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PAPER_DIR="$HERE/../paper"
OUT_DIR="$HERE/build"
mkdir -p "$OUT_DIR"
cp "$PAPER_DIR/"*.tex "$OUT_DIR/"
cp "$PAPER_DIR/"*.bib "$OUT_DIR/"
cp "$PAPER_DIR/"*.bbl "$OUT_DIR/" 2>/dev/null || true
cd "$OUT_DIR"
if ! command -v pdflatex >/dev/null 2>&1; then
  echo "pdflatex not found. Compile locally with:"
  echo "  pdflatex sft_form_theory.tex"
  echo "  bibtex sft_form_theory"
  echo "  pdflatex sft_form_theory.tex"
  echo "  pdflatex sft_form_theory.tex"
  exit 0
fi
pdflatex -interaction=nonstopmode -halt-on-error -file-line-error sft_form_theory.tex
if command -v bibtex >/dev/null 2>&1; then
  bibtex sft_form_theory || true
fi
pdflatex -interaction=nonstopmode -halt-on-error -file-line-error sft_form_theory.tex
pdflatex -interaction=nonstopmode -halt-on-error -file-line-error sft_form_theory.tex
echo "Build output in $OUT_DIR"
