# Makefile to run pdflatex, lualatex or xelatex on a thesis.
# Can also run axodraww/feynmf/feynmp/pyfeynhand/tikz on files in a directory.

THESIS ?= mythesis
COVER := cover_only
SUMMARY := summary_only
ABSTRACT := abstract_only
# EXTRACMD = --shell-escape
AXODRAWDIR ?= axodraw
AXODRAWFILES = $(wildcard $(AXODRAWDIR)/*.tex)
FEYNDIR ?= feynmf
FEYNFILES = $(wildcard $(FEYNDIR)/*.tex)
PYFEYNHANDDIR ?= pyfeynhand
PYFEYNHANDFILES = $(wildcard $(PYFEYNHANDDIR)/*.py)
TIKZDIR ?= tikz
TIKZFILES = $(wildcard $(TIKZDIR)/*.tex)
ifdef file
AXODRAWFILES = $(AXODRAWDIR)/$(file).tex
FEYNFILES = $(FEYNDIR)/$(file).tex
PYFEYNHANDFILES = $(PYFEYNHANDDIR)/$(file).py
TIKZFILES = $(TIKZDIR)/$(file).tex
endif
LATEXCMD ?= pdflatex
# Engine for latexmk. Can be pdf, lualatex or xelatex.
ENGINE ?= pdf
BIBTEX ?= biber
AWKDIR ?= ..

# Portable file removal: works on Linux, macOS, and Windows (cmd/PowerShell)
ifeq ($(OS),Windows_NT)
    RM_F = if exist $1 del /f /q $1
else
    RM_F = rm -f $1
endif

.PHONY: cover \
	feynmf feynmp pyfeynhand tikz \
	cleanthesis cleancover cleansummary cleanabstract \
	cleanaxodraw cleanfeynmf cleanfeynmp cleanpyfeynhand cleantikz cleanpictpdf \
	cleanblx cleanbbl \
	cleanglo cleanlatexmk\
	help test

thesis: run_latexmk

thesislua: run_lualatexmk

thesisxe: run_xelatexmk

# Use latexmk to compile thesis.
# Variant with -shell-esapce if needed.
# LATEXMK = latexmk -e '$$pdflatex=q/pdflatex %O -shell-escape %S/' -pdf $(THESIS)
run_latexmk: $(THESIS).tex *.tex bib/*.bib
	latexmk -pdf $(THESIS)

run_lualatexmk: $(THESIS).tex *.tex bib/*.bib
	latexmk -lualatex $(THESIS)

run_xelatexmk: $(THESIS).tex *.tex bib/*.bib
	latexmk -xelatex $(THESIS)

# - in front of bibtex means compilation continues even if there is an error.
thesis11: $(THESIS).tex *.tex bib/*.bib
	$(LATEXCMD) $(EXTRACMD) $(THESIS)
	$(BIBTEX) $(THESIS)
	# makeglossaries $(THESIS)
	$(LATEXCMD) $(EXTRACMD) $(THESIS)
	$(LATEXCMD) $(EXTRACMD) $(THESIS)

# Turn off running of bibliography program, e.g. biber with -bibtex- option.
# This is not done by default as it fails with TeX Live 2022.
cover:
	latexmk -$(ENGINE) $(COVER)

summary: $(SUMMARY).tex
	latexmk -$(ENGINE) $(SUMMARY)

# Turn off running of bibliography program, e.g. biber with -bibtex- option.
# This is not done by default as it fails with TeX Live 2022.
abstract: $(ABSTRACT).tex
	latexmk -$(ENGINE) $(ABSTRACT)

axodraw:
	make -f ../Makefile AXODRAWDIR=$(AXODRAWDIR) AXODRAWFILES="$(AXODRAWFILES)" axodraw

feynmf:
	make -f ../Makefile FEYNDIR=$(FEYNDIR) FEYNFILES="$(FEYNFILES)" \
	 AWKDIR=$(AWKDIR) feynmf

feynmp:
	make -f ../Makefile FEYNDIR=$(FEYNDIR) FEYNFILES="$(FEYNFILES)" \
	 AWKDIR=$(AWKDIR) feynmp

pyfeynhand:
	make -f ../Makefile PYFEYNHANDDIR=$(PYFEYNHANDDIR) PYFEYNHANDFILES="$(PYFEYNHANDFILES)" pyfeynhand

tikz:
	make -f ../Makefile TIKZDIR=$(TIKZDIR) TIKZFILES="$(TIKZFILES)" tikz

thesis_feynmf:
	feynmf $(THESIS)

# Add extra extensions here if needed.
LATEX_TEMP_EXTS = .aux .log .out .xdv \
	.bcf .fdb_latexmk .fls .run.xml .synctex.gz -SAVE-ERROR \
	.bbl .blg .brf -blx/bib \
	.lof .lot .toc

EXTRA_TEMP_EXTS = .aux .log .out .pdf .xdv \
	.bcf .fdb_latexmk .fls .run.xml .synctex.gz

GLOSSARY_TEMP_EXTS = .acn .acr .alg .glg .glo .gls .ist

cleanall: clean cleanpdf cleanpictpdf

clean: cleanthesis cleancover cleansummary cleanabstract \
	cleanfeynmf cleanfeynmp cleantikz cleanpyfeyn cleanpyfeynhand \
	cleanblx cleanglo cleanlatexmk

cleanthesis:
	$(foreach ext, $(LATEX_TEMP_EXTS), $(call RM_F, $(THESIS)$(ext));)
	$(call RM_F, *.aux)

cleanpdf:
	$(call RM_F, $(THESIS).pdf)
	$(call RM_F, $(COVER).pdf)
	$(call RM_F, $(SUMMARY).pdf)
	$(call RM_F, $(ABSTRACT).pdf)

cleancover:
	$(foreach ext, $(EXTRA_TEMP_EXTS), $(call RM_F, $(COVER)$(ext));)

cleansummary:
	$(foreach ext, $(EXTRA_TEMP_EXTS), $(call RM_F, $(SUMMARY)$(ext));)

cleanabstract:
	$(foreach ext, $(EXTRA_TEMP_EXTS), $(call RM_F, $(ABSTRACT)$(ext));)

cleanaxodraw:
	$(call RM_F, $(AXODRAWDIR)/*.aux)
	$(call RM_F, $(AXODRAWDIR)/*.log)

cleanfeynmf:
	$(call RM_F, *.mf)
	$(call RM_F, *.tfm)
	$(call RM_F, *.t1)
	$(call RM_F, *.600gf)
	$(call RM_F, *.600pk)
	$(call RM_F, *.log)
	$(call RM_F, feynmf_all.*)
	$(call RM_F, feynmf_files.inp)

cleanfeynmp:
	$(call RM_F, *.1)
	$(call RM_F, *.log)
	$(call RM_F, *.mp)
	$(call RM_F, *.t1)

cleanpyfeynhand:
	$(call RM_F, $(PYFEYNHANDDIR)/*.tex)
	$(call RM_F, $(PYFEYNHANDDIR)/*.aux)
	$(call RM_F, $(PYFEYNHANDDIR)/*.log)

cleantikz:
	$(call RM_F, $(TIKZDIR)/*.aux)
	$(call RM_F, $(TIKZDIR)/*.log)

cleanpictpdf:
	$(call RM_F, $(AXODRAWDIR)/*.pdf)
	$(call RM_F, $(FEYNDIR)/*.pdf)
	$(call RM_F, $(PYFEYNHANDDIR)/*.pdf)
	$(call RM_F, $(TIKZDIR)/*.pdf)

cleanglo:
	$(foreach ext, $(GLOSSARY_TEMP_EXTS), $(call RM_F, *$(ext));)

help:
	@echo "Possible commands:"
	@echo "thesis: compile complete thesis (latexmk)"
	@echo "cover: compile cover page only for PhD submission (latexmk)"
	@echo "summary: compile summary for PhD submission (latexmk)"
	@echo "abstract: compile simple cover page with abstract (latexmk)"
	@echo "run_latexmk: compile complete thesis using latexmk and pdflatex"
	@echo "run_lualatexmk: compile complete thesis using latexmk and lualatex"
	@echo "run_xelatexmk: compile complete thesis using latexmk and xelatex"
	@echo "thesis11: compile complete thesis (not using latexmk)"
	@echo "axodraw: run axodraw for all .tex files in $(AXODRAWDIR)"
	@echo "feynmf: run feynmf for all .tex files in $(FEYNDIR)"
	@echo "feynmp: run feynmp for all .tex files in $(FEYNDIR)"
	@echo "pyfeynhand: run Python for all .py files in $(PYFEYNHANDDIR)"
	@echo "tikz: run tikz for all .tex files in $(TIKZDIR)"
	@echo "clean: clean up most auxiliary files (leaves thesis and picture PDF files)"
	@echo "cleanall: clean up all auxiliary files"
	@echo "cleanthesis: clean up thesis LaTeX output files"
	@echo "cleancover: clean up cover page only output files"
	@echo "cleansummary: clean up summary output files"
	@echo "cleanabstract: clean up simple cover page with abstract output files"
	@echo "cleanaxodraw: clean up axodraw output files"
	@echo "cleanfeynmf: clean up feynmf output files"
	@echo "cleanfeynmp: clean up feynmp output files"
	@echo "cleanpyfeynhand: clean up pyfeynhand temporary files"
	@echo "cleantikz: clean up tikz temparary files"
	@echo "cleanpictpdf: clean up picture output in $(AXODRAWDIR), $(FEYNDIR), $(PYFEYNHANDDIR) and $(TIKZDIR)"
	@echo "cleanglo: clean up glossary output files"

test:
	@echo "Thesis $(THESIS)"
	@echo "Cover only $(COVER)"
	@echo "Summary $(SUMMARY)"
	@echo "Abstract $(ABSTRACT)"
	@echo "Axodraw Feynman graphs dir: $(AXODRAWDIRNAME)"
	@echo "Axodraw Feynman graphs files: $(AXODRAWFILES)"
	@echo "Feynmf/feynmp Feynman graphs dir: $(FEYNDIRNAME)"
	@echo "Feynmf/feynmp Feynman graphs files: $(FEYNFILES)"
	@echo "PyFeynHand Feynman graphs dir: $(PYFEYNHANDDIRNAME)"
	@echo "PyFeynHand Feynman graphs files: $(PYFEYNHANDFILES)"
	@echo "TikZ Feynman graphs dir: $(TIKZDIRNAME)"
	@echo "TikZ Feynman graphs files: $(TIKZFILES)"
