# ============================================================
# Makefile for Operational-Premise Taxonomy (OPT)
# Builds: arXiv (article), IEEE, ACM, Kaobook
# Author: Wesley R. Elsberry
# ============================================================

LATEXMK := $(shell command -v latexmk 2>/dev/null)
PDFLATEX := pdflatex
BIBTEX := bibtex

ARXIV = main
IEEE  = main_ieee
ACM   = main_acm
KAO   = main_kaobook

ALLPDF = $(ARXIV).pdf $(IEEE).pdf $(ACM).pdf $(KAO).pdf

.PHONY: all arxiv ieee acm kaobook clean distclean help

# ------------------------------------------------------------
# Top-level build targets
# ------------------------------------------------------------
all: $(ALLPDF)

arxiv: $(ARXIV).pdf
ieee:  $(IEEE).pdf
acm:   $(ACM).pdf
kaobook: $(KAO).pdf

# ------------------------------------------------------------
# Pattern rule for building PDFs
# ------------------------------------------------------------
ifeq ($(LATEXMK),)
# --- Fallback to manual build sequence
%.pdf: %.tex references.bib
	$(PDFLATEX) -interaction=nonstopmode $*
	$(BIBTEX) $* || true
	$(PDFLATEX) -interaction=nonstopmode $*
	$(PDFLATEX) -interaction=nonstopmode $*
else
# --- Preferred build with latexmk
%.pdf: %.tex references.bib
	latexmk -pdf -interaction=nonstopmode -halt-on-error $<
endif

# ------------------------------------------------------------
# Cleanup targets
# ------------------------------------------------------------
clean:
	@rm -f *.aux *.bbl *.blg *.log *.out *.toc *.lof *.lot *.nav *.snm *.fls \
	        *.fdb_latexmk *.run.xml *.bcf *.synctex.gz *.tdo *.dvi *.ps

distclean: clean
	@rm -f $(ALLPDF)

help:
	@echo "Usage:"
	@echo "  make all        # build all versions (default)"
	@echo "  make arxiv      # build main.pdf (arXiv/preprint)"
	@echo "  make ieee       # build main_ieee.pdf"
	@echo "  make acm        # build main_acm.pdf"
	@echo "  make kaobook    # build main_kaobook.pdf"
	@echo "  make clean      # remove aux files"
	@echo "  make distclean  # remove aux + PDFs"
