# Load the configure script variables
-include ../configure.mk

##
## Options
##

# Pandoc flags
FLAGS      = --filter pandoc-crossref --citeproc --toc --number-sections
PDF_FLAGS  = --pdf-engine=xelatex
HTML_FLAGS = --standalone --mathml --lua-filter filters/add-anchors.lua
MATHFONT ?= $(shell fc-match --format=%{file} 'Libertinus Math')

# Rebuild everything if the makefile changed
.EXTRA_PREREQS += Makefile

# User manual
DOCDIR   = ../build/share/doc
SECTIONS = $(wildcard [0-9].*.md)
MANUALS  = $(addprefix $(DOCDIR)/,manual.html manual.pdf res)

##
## Targets
##

.PHONY: all clean

all: $(MANUALS) $(MANPAGES)

clean:
	rm -r build

$(DOCDIR)/manual.html: $(SECTIONS) res | $(DOCDIR)
	pandoc $(FLAGS) $(HTML_FLAGS) $(filter %.md, $^) -o '$@'

$(DOCDIR)/res: res | $(DOCDIR)
	cp -Tfr $^ '$@'
	cp $(MATHFONT) '$@/mathfont.otf'

$(DOCDIR)/manual.pdf: $(SECTIONS) | $(DOCDIR)
	pandoc $(FLAGS) $(PDF_FLAGS) $^ -o '$@'

# Create directories
$(DOCDIR):
	mkdir -p '$@'