# -*- coding: utf-8 -*-

MAIN_VARIANT        := screen
OTHER_VARIANTS      := fullscreen print
ALL_VARIANTS        := $(MAIN_VARIANT) $(OTHER_VARIANTS)

all: $(ALL_VARIANTS) # ps

LATEX    := pdflatex -output-format dvi
PDFLATEX := pdflatex

# LATEX_ARGS    := --src-specials \\nonstopmode
LATEX_ARGS      := \\nonstopmode
PDFLATEX_ARGS   := \\nonstopmode
TEX_FINAL_RUNS  := 2

SRC_BASE_NAME := notes-RDO
SRC           := $(SRC_BASE_NAME).tex $(shell kpsewhich flo-maths.bib)

$(SRC_BASE_NAME).dvi: $(SRC)
	$(LATEX) $(LATEX_ARGS) '\input{$<}'
	biber '$(SRC_BASE_NAME)'

	for i in $$(seq 1 $(TEX_FINAL_RUNS)); do \
           $(LATEX) $(LATEX_ARGS) '\input{$<}'; \
        done

# cf. GNU Make Manual (“Syntax of Functions”) and
# <https://stackoverflow.com/questions/28615974/wildcard-to-variable-to-comma-joined-string>
comma := ,
empty :=
space := $(empty) $(empty)
# Function that converts a space-separated list into a comma-separated list
comma-separate = $(subst ${space},${comma},$(strip $(1)))

# Paramétrage du document PDF produit via \jobname et le package 'parmoutput'.
# $(1): nom de la variante
# $(2): nom du fichier de sortie, sans l'extension
# $(3): liste d'options, séparées par des *espaces*, à passer au
#       package 'parmoutput' (pour la virgule, il y a malheureusement conflit
#       avec la syntaxe de la fonction 'call' de Make)
define customPDF =
$(1): $(2).pdf

$(2).pdf: $$(SRC)
	$$(PDFLATEX) --jobname='$(2)' \
         $$(PDFLATEX_ARGS)'\PassOptionsToPackage{' \
            '$(call comma-separate,$(3)) }{parmoutput} \input{$$<}'
	biber '$(2)'

	for i in $$$$(seq 1 $$(TEX_FINAL_RUNS)); do \
           $$(PDFLATEX) --jobname='$(2)' \
            $$(PDFLATEX_ARGS)'\PassOptionsToPackage{' \
            '$(call comma-separate,$(3)) }{parmoutput} \input{$$<}'; \
        done
endef

$(eval $(call \
  customPDF,screen,$(SRC_BASE_NAME), outputType=screen fullScreen=false))
$(eval $(call \
  customPDF,fullscreen,$(SRC_BASE_NAME)-fullscreen, outputType=screen fullScreen=true))
$(eval $(call customPDF,print,$(SRC_BASE_NAME)-print, outputType=paper))

%.ps: %.dvi
	dvips -o '$@' '$<'

pdf: $(ALL_VARIANTS)
dvi: $(SRC_BASE_NAME).dvi
ps: $(SRC_BASE_NAME).ps

print-variants:
	@echo $(ALL_VARIANTS)

print-main-variant:
	@echo $(MAIN_VARIANT)

print-other-variants:
	@echo $(OTHER_VARIANTS)

clean:
	for ext in dvi ps pdf out aux log idx ind ilg toc bbl blg bcf run.xml; \
        do                                                            \
            rm -f "$(SRC_BASE_NAME).$$ext";                           \
                                                                      \
            for variant in $(OTHER_VARIANTS); do                      \
                rm -f "$(SRC_BASE_NAME)-$$variant.$$ext";             \
            done                                                      \
        done
	rm -f missfont.log

        # Stuff from preview-latex
	for ext in fmt ini log; do \
            rm -f "prv_$(SRC_BASE_NAME).$$ext"; \
        done

	rm -rf auctex-auto '$(SRC_BASE_NAME).prv' _region_.prv
	for ext in tex pdf log run.xml; do \
            rm -f "_region_.$$ext"; \
        done

.PHONY: all clean pdf dvi ps $(ALL_VARIANTS) print-variants print-main-variant \
        print-other-variants
