# Makefile to run latex, dvips, and pdflatex on the thesis guide
# Can also run feynmf/feynmp/tikz on files in a directory

GUIDE := thesis_guide
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
TIKZFILES = $(TIKZDIR)/$(file).tex
PYFEYNFILES = $(PYFEYNDIR)/$(file).py
PYFEYNHANDFILES = $(PYFEYNHANDDIR)/$(file).py
endif
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: guide11 phdsubmit \
	axodraw feynmf feynmp pyfeynhand tikz \
	cleanguide cleansubfile \
	cleanaxodraw cleanfeynmf cleanfeynmp cleanpyfeynhand cleantikz cleanpictpdf \
	cleanglo cleanphd \
	help test

guide: run_latexmk

guidexe: run_xelatexmk

guidelua: run_lualatexmk

# Guide needs -shell-escape for glossary and index.
# latexmk -pdf $(GUIDE)
run_latexmk: *.tex
	latexmk -e '$$pdflatex=q/pdflatex %O -shell-escape %S/' -pdf $(GUIDE)

run_lualatexmk: *.tex
	latexmk -e '$$lualatex=q/lualatex %O -shell-escape %S/' -lualatex $(GUIDE)

run_xelatexmk: *.tex
	latexmk -e '$$xelatex=q/xelatex %O -shell-escape %S/' -xelatex $(GUIDE)

guide11:
	pdflatex   $(EXTRACMD) $(GUIDE)
	biber      $(GUIDE)
	makeindex  $(GUIDE)
	makeglossaries $(GUIDE)
	pdflatex   $(EXTRACMD) $(GUIDE)
	pdflatex   $(GUIDE)

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

# pyfeyn:
# 	make -f ../Makefile PYFEYNDIR=$(PYFEYNDIR) PYFEYNFILES="$(PYFEYNFILES)" pyfeyn

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

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

phdsubmit:
	pdflatex  PhD_submit
	pdflatex  PhD_submit

cleanall: clean cleanaxodraw cleanfeynmf cleanfeynmp cleanpyfeynhand

clean: cleanguide cleansubfile cleanphd

LATEX_TEMP_EXTS = .aux .log .out .pdf .xdv \
	.bcf .fdb_latexmk .fls .run.xml .synctex.gz \
	.bbl-SAVE-ERROR .bcf-SAVE-ERROR \
	.bbl .blg .brf -blx.bib \
	.lof .lot .toc \
	.ax1 .ax2 .idx .ilg .ind \
	.listing .wrt \
	.acn .acr .alg .glg .glo .gls .ist \
	.tex.bak

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

cleanguide: BASENAME := $(GUIDE)
cleanguide:
	$(foreach ext, $(LATEX_TEMP_EXTS), $(call RM_F, $(BASENAME)$(ext));)

cleansubfile: BASENAME := guide_*
cleansubfile:
	$(foreach ext, $(LATEX_TEMP_EXTS), $(call RM_F, $(BASENAME)$(ext)))

cleanaxodraw:
	make -f ../Makefile cleanaxodraw AXODRAWDIR=$(AXODRAWDIR)

cleanfeynmf:
	make -f ../Makefile cleanfeynmf FEYNDIR=$(FEYNDIR)

cleanfeynmp:
	make -f ../Makefile cleanfeynmp FEYNDIR=$(FEYNDIR)
	$(call RM_F, *.1)
	$(call RM_F, *.mp)

# cleanpyfeyn:
# 	make -f ../Makefile cleanpyfeyn PYFEYNDIR=$(PYFEYNDIR)

cleanpyfeynhand:
	make -f ../Makefile cleanpyfeynhand PYFEYNHANDDIR=$(PYFEYNHANDDIR)

cleantikz:
	make -f ../Makefile cleantikz TIKZDIR=$(TIKZDIR)

cleanpictpdf:
	make -f ../Makefile cleanpictpdf FEYNDIR=$(FEYNDIR) TIKZDIR=$(TIKZDIR) \
		PYFEYNHANDDIR=$(PYFEYNHANDDIR)

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

cleanphd: BASENAME := PhD_submit
cleanphd:
	$(foreach ext, $(LATEX_TEMP_EXTS), $(call RM_F, $(BASENAME)$(ext)))

help:
	@echo "Possible commands:"
	@echo "guide: compile thesis guide using latexmk"
	@echo "guidexe: compile thesis guide using xelatex and latexmk"
	@echo "guidelua: compile thesis guide using lualatex and latexmk"
	@echo "guide11: compile thesis guide using pdflatex and without using latexmk"
	@echo "feynmf: run feynmf for all .tex files in $(FEYNDIR)"
	@echo "feynmp: run feynmp for all .tex files in $(FEYNDIR)"
	@echo "pyfeynhand: make Feynman grpahs with PyFeynHand in $(PYFEYNHANDDIR)"
	@echo "tikz: run tikz for all .tex files in $(TIKZDIR)"
	@echo "phdsubmit: summary of PhD submission instructions"
	@echo "cleanall: thorough cleanup"
	@echo "clean: thorough cleanup except for feynmf and feynmp files"
	@echo "cleanguide: clean up guide  LaTeX output files"
	@echo "cleansubfile: clean up guide subfile LaTeX 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 pyfeynhnad output files"
	@echo "cleantikz: clean up tikz output files"
	@echo "cleanpictpdf: clean up tikz, feynmp and pyfeynhand output PDF files"

test:
	@echo "Guide $(GUIDE)"
	@echo "Axodraw Feynman graphs dir: $(AXODRAWDIR)"
	@echo "Feynmf/feynmp Feynman graphs dir: $(FEYNDIR)"
	@echo "PyFeynHand Feynman graphs dir: $(PYFEYNHANDDIR)"
	@echo "TikZ Feynman graphs dir: $(TIKZDIR)"
