# Makefile to create a new thesis skeleton.
# Can also run axodraw/feynmf/feynmp/pyfeynhand/tikz on files in a directory.

THESIS ?= mythesis

# Check for SUBFILE argument
ifdef SUBFILE
SUBFILEDIR = _subfile
else
SUBFILEDIR =
endif
# EXTRACMD = --shell-escape
ifndef AXODRAWDIR
AXODRAWDIR := axodraw
endif
ifndef AXODRAWFILES
AXODRAWFILES = $(wildcard $(AXODRAWDIR)/*.tex)
endif
ifndef FEYNDIR
FEYNDIR := feynmf
endif
ifndef FEYNFILES
FEYNFILES = $(wildcard $(FEYNDIR)/*.tex)
endif
ifndef PYFEYNHANDDIR
PYFEYNHANDDIR = pyfeynhand
endif
ifndef PYFEYNHANDFILES
PYFEYNHANDFILES = $(wildcard $(PYFEYNHANDDIR)/*.py)
endif
ifndef TIKZDIR
TIKZDIR := tikz
endif
ifndef TIKZFILES
TIKZFILES = $(wildcard $(TIKZDIR)/*.tex)
endif
ifdef file
AXODRAWFILES = $(AXODRAWDIR)/$(file).tex
FEYNFILES = $(FEYNDIR)/$(file).tex
PYFEYNHANDFILES = $(PYFEYNHANDDIR)/$(file).py
TIKZFILES = $(TIKZDIR)/$(file).tex
endif
ifndef AWKDIR
AWKDIR := .
endif
ifndef MPOSTDIR
MPOSTDIR := mpost_tmp
endif
LATEXMK = latexmk -pdf
# LATEXMK = latexmk -e '$$pdflatex=q/pdflatex %O -shell-escape %S/' -pdf $(GUIDE)

# Portable file removal: works on Linux, macOS, and Windows (cmd/PowerShell)
# This is quite fancy (thanks to Claude) so it also works in subdirectories.
ifeq ($(OS), Windows_NT)
    RM_F      = del /f /q
    fix_path  = $(subst /,\,$1)
    clean_ext = $(if $(wildcard $1/*$2), $(RM_F) $(call fix_path,$(wildcard $1/*$2));,)
else
    RM_F      = rm -f
    fix_path  = $1
    clean_ext = $(if $(wildcard $1/*$2), $(RM_F) $(wildcard $1/*$2);,)
endif

# 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: skelcopy \
	axodraw feynmf feynmp pyfeynhand tikz \
	cleanaxodraw cleanfeynmf cleanfeynmp cleanpyfeynhand cleantikz cleanpictpdf \
	help test

#  New thesis.
new: TYPE := physics
new: skelcopy

# New thesis with astrophysics style of references.
astro: TYPE := astro
astro: skelcopy

# New thesis
skelcopy:
	mkdir $(THESIS)
	mkdir $(THESIS)/bib
	mkdir $(THESIS)/figs
	cp thesis$(SUBFILEDIR)_skel/thesis_$(TYPE)_skel.tex $(THESIS)/$(THESIS).tex
	sed 's/MYTHESISMAIN/$(THESIS)/' thesis$(SUBFILEDIR)_skel/thesis_$(TYPE)_intro.tex    > $(THESIS)/thesis_intro.tex
	sed 's/MYTHESISMAIN/$(THESIS)/' thesis$(SUBFILEDIR)_skel/thesis_$(TYPE)_appendix.tex > $(THESIS)/thesis_appendix.tex
	sed 's/MYTHESISMAIN/$(THESIS)/' thesis$(SUBFILEDIR)_skel/thesis_acknowledge.tex      > $(THESIS)/thesis_acknowledge.tex
	cp thesis_skel/thesis_defs.sty        $(THESIS)/
	cp thesis_skel/thesis_cv.tex          $(THESIS)/
	cp thesis_skel/cover_only.tex         $(THESIS)/
	cp thesis_skel/summary_only.tex       $(THESIS)/
	cp thesis_skel/abstract_only.tex      $(THESIS)/
	cp ubonn-thesis.sty                   $(THESIS)/
	cp ubonn-biblatex.sty                 $(THESIS)/
	cp thesis_skel/thesis_refs.bib        $(THESIS)/bib/
	cp refs/standard_refs-biber.bib       $(THESIS)/bib/
	cp -R cover                           $(THESIS)/
	cp -R figs/cover                      $(THESIS)/figs/
	sed 's/^THESIS ?=.*/THESIS ?= ${THESIS}/'  thesis_skel/Makefile > $(THESIS)/Makefile

# Do not update Makefile.
# sed 's/^THESIS =.*/THESIS = ${THESIS}/'  thesis_skel/Makefile > $(THESIS)/Makefile
update:
	-cp -i ubonn-thesis.sty                $(THESIS)/
	-cp -i ubonn-biblatex.sty              $(THESIS)/
	-cp -i refs/standard_refs-biber.bib    $(THESIS)/bib/
	-cp -i -R cover                        $(THESIS)/
	-cp -i -R figs/cover                   $(THESIS)/

axodraw: $(AXODRAWFILES)
	@echo "Axodraw Feynman graph files: $^"
	(cd $(AXODRAWDIR); $(foreach axodrawfile, $^, \
		pdflatex $(notdir $(axodrawfile)); \
		axohelp $(notdir $(basename $(axodrawfile))); \
		pdflatex $(notdir $(axodrawfile));))

feynmf: $(FEYNFILES)
	@echo "Feymf Feynman graph files: $^"
	$(call RM_F, feynmf_files.inp)
	touch feynmf_files.inp
	$(foreach feynfile, $^, echo $(feynfile) >>feynmf_files.inp;)
	cat feynmf_files.inp
	cat feynmf_files.inp | awk -f $(AWKDIR)/awk_feynmf >feynmf_all.tex
	-pdflatex feynmf_all
	$(foreach feynfile, $^, $(AWKDIR)/run_mf $(feynfile);)
	pdflatex feynmf_all; pdflatex feynmf_all; pdflatex feynmf_all

feynmp: $(FEYNFILES)
	@echo "Feynmp Feynman graph files: $^"
	$(foreach feynfile, $^, $(AWKDIR)/run_mp $(feynfile);)

# pyfeyn: $(PYFEYNFILES)
# 	@echo "PyFeyn Feynman graph files: $^"
# 	(cd $(PYFEYNDIR); $(foreach pyfeynfile, $^, ./$(notdir $(pyfeynfile));))

# Make color and B&W PDF versions of TikZ-FeynHand graphs.
pyfeynhand: $(PYFEYNHANDFILES)
	@echo "PyFeynHand Feynman graph files: $^"
	(cd $(PYFEYNHANDDIR); $(foreach pyfeynhandfile, $^, ./$(notdir $(pyfeynhandfile)) --pdf;))
	(cd $(PYFEYNHANDDIR); $(foreach pyfeynhandfile, $^, ./$(notdir $(pyfeynhandfile)) --pdf --BW;))

tikz: $(TIKZFILES)
	@echo "TikZ Feynman graph files: $^"
	(cd $(TIKZDIR); $(foreach tikzfile, $^, pdflatex $(notdir $(tikzfile));))

AXODRAW_TEMP_EXTS := .aux .log \
	.fdb_latexmk .fls .synctex.gz \
	.ax1 .ax2

FEYNMF_TEMP_EXTS := .log .mf .tfm .t1 .600gf .600pk

FEYNMP_TEMP_EXTS := .log .1 .mp .t1 .aux _mp.log _mp.pdf _mp.tex

cleanall: clean cleanpictpdf

clean: cleanaxodraw cleanfeynmf cleanfeynmp cleanpyfeynhand cleantikz

cleanaxodraw:
	$(foreach ext, $(AXODRAW_TEMP_EXTS), $(call clean_ext,$(AXODRAWDIR),$(ext)))

cleanfeynmf:
	$(foreach ext, $(FEYNMF_TEMP_EXTS), $(call clean_ext,.,$(ext)))
	$(RM_F) feynmf_all.*
	$(RM_F) feynmf_files.inp

cleanfeynmp:
	$(foreach ext, $(FEYNMP_TEMP_EXTS), $(call clean_ext,$(MPOSTDIR),$(ext)))

cleanpyfeynhand:
	$(call clean_ext,$(PYFEYNHANDDIR),aux)
	$(call clean_ext,$(PYFEYNHANDDIR),log)

cleantikz:
	$(call clean_ext,$(TIKZDIR),aux)
	$(call clean_ext,$(TIKZDIR),log)

cleanpictpdf:
	$(call clean_ext,$(AXODRAWDIR),pdf)
	$(call clean_ext,$(FEYNDIR),pdf)
	$(call clean_ext,$(PYFEYNHANDDIR),pdf)
	$(call clean_ext,$(PYFEYNHANDDIR),tex)
	$(call clean_ext,$(TIKZDIR),pdf)

help:
	@echo "Possible commands:"
	@echo "new   [THESIS=dirname] [SUBFILE=1]: create a new thesis skeleton"
	@echo "astro [THESIS=dirname] [SUBFILE=1]: create a new astrophysics thesis skeleton"
	@echo "update [THESIS=dirname]: update to a newer version of ubonn-thesis"
	@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 picture auxiliary output files"
	@echo "cleanpictpdf: clean up picture PDF output files"
	@echo "cleanaxodraw: clean up axodraw2 auxiliary output files"
	@echo "cleanfeynmf: clean up feynmf auxiliary output files"
	@echo "cleanfeynmp: clean up feynmp auxiliary output files"
	@echo "cleanpyfeynhand: clean up pyfeynhand auxiliary output files"
	@echo "cleantikz: clean up tikz auxiliary output files"

test:
	@echo "Axodraw Feynman graphs dir: $(AXODRAWDIR)"
	@echo "Axodraw Feynman graphs files: $(AXODRAWFILES)"
	@echo "Feynmf Feynman graphs dir: $(FEYNDIR)"
	@echo "Feynmf Feynman graphs files: $(FEYNFILES)"
	@echo "PyFeynHand Feynman graphs dir: $(PYFEYNHANDDIR)"
	@echo "PyFeynHand Feynman graphs files: $(PYFEYNHANDFILES)"
	@echo "TikZ   Feynman graphs dir: $(TIKZDIR)"
	@echo "TikZ   Feynman graphs files: $(TIKZFILES)"
