# Build a local PDF proof of the JOSS paper from paper.md.
#
# This produces a readable proof for review using locally available tools
# (pandoc + a LaTeX engine).  The official, branded JOSS PDF is generated by
# the Open Journals "inara" pipeline (pandoc + the JOSS LaTeX template, run in
# a Docker container) when the paper is submitted at https://joss.theoj.org/ ;
# this Makefile does not attempt to reproduce that exact layout.

PAPER := paper
SRC   := $(PAPER).md
BIB   := $(PAPER).bib
PDF   := $(PAPER).pdf

PANDOC := pandoc

# Use the first LaTeX PDF engine found on this machine.
PDF_ENGINE := $(firstword $(foreach e,pdflatex xelatex lualatex tectonic,\
                $(if $(shell command -v $(e) 2>/dev/null),$(e))))

PANDOC_FLAGS := --citeproc \
                --from=markdown \
                --resource-path=.:images \
                -V geometry:margin=1in \
                -V colorlinks=true \
                -M author="Axel Kohlmeyer"

all: $(PDF) joss-11185.preprint.pdf

joss-11185.preprint.pdf: joss-11185.preprint.tex paper.bib
	latexmk -pdfxe $<

$(PDF): $(SRC) $(BIB) | check-tools
	$(PANDOC) $(PANDOC_FLAGS) --pdf-engine=$(PDF_ENGINE) $(SRC) -o $@
	@echo "Built $@ with pandoc + $(PDF_ENGINE)."

# Verify the required tools exist; if not, explain what to install.
check-tools:
	@command -v $(PANDOC) >/dev/null 2>&1 || { \
	  echo "ERROR: 'pandoc' not found.  Install it, e.g.:"; \
	  echo "    Fedora/RHEL  : sudo dnf install pandoc"; \
	  echo "    Debian/Ubuntu: sudo apt install pandoc"; \
	  echo "    macOS (brew) : brew install pandoc"; \
	  exit 1; }
	@test -n "$(PDF_ENGINE)" || { \
	  echo "ERROR: no LaTeX PDF engine found"; \
	  echo "       (need one of: pdflatex, xelatex, lualatex, tectonic)."; \
	  echo "  Install a TeX distribution, e.g.:"; \
	  echo "    Fedora/RHEL  : sudo dnf install texlive-scheme-basic \\"; \
	  echo "                     texlive-collection-latexrecommended"; \
	  echo "    Debian/Ubuntu: sudo apt install texlive-latex-recommended \\"; \
	  echo "                     texlive-latex-extra"; \
	  echo "    macOS (brew) : brew install --cask mactex-no-gui   # or: brew install tectonic"; \
	  exit 1; }
	@echo "Tools OK: $$($(PANDOC) --version | head -1), engine=$(PDF_ENGINE)."

clean:
	rm -vf $(PDF) \
	joss-11185.preprint.fls \
	joss-11185.preprint.aux \
	joss-11185.preprint.xdv \
	joss-11185.preprint.log \
	joss-11185.preprint.pdf \
	joss-11185.preprint.fdb_latexmk \
	*~

.PHONY: all check-tools clean
