# Makefile for creating documentation for ATLAS LaTeX.
# Usually called from a subdirectory.

PDFLATEX = pdflatex
BIBTEX = biber
BASENAME = dummy

# Set up -bibtex option for latexmk if bibtex is used.
ifeq ($(BIBTEX),biber)
	LATEXMK_BIBTEX =
else
	LATEXMK_BIBTEX = -bibtex
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: clean cleanpdf

# Remove -pdf option to run latex instead of pdflatex.
run_latexmk:
	latexmk -pdf $(LATEXMK_BIBTEX) $(BASENAME)

run_pdflatex: $(BASENAME).pdf
	@echo "Made $<"

# Change year for tweaks.
# tweakyear: $(BASENAME).tex
# 	if [ $(TWEAKYEAR) -ge 2013 -a $(TWEAKYEAR) -lt 2100 ]; then \
# 	  echo "Changing tweak year in $<"; \
# 	  sed -i'.bak' 's/AtlasTweakYear}{20[0-9][0-9]/AtlasTweakYear}{$(TWEAKYEAR)/' $<; \
# 	else echo "Invalid value for TWEAKYEAR: $(TWEAKYEAR)"; fi;

# Standard LaTeX targets.
$(BASENAME).pdf: $(BASENAME).tex
	$(PDFLATEX) $<
	-$(BIBTEX)  $(basename $<)
	$(PDFLATEX) $<
	$(PDFLATEX) $<

# Add extra extensions here if needed.
LATEX_TEMP_EXTS = .aux .log .out \
	.bcf .fdb_latexmk .fls .run.xml .synctex.gz -SAVE-ERROR \
	.bbl .blg .brf -blx.bib \
	.ind .idx .ilg .inx .lb .tdo \
	.lof .lot .toc\
	.listing

clean:
	$(foreach ext, $(LATEX_TEMP_EXTS), $(call RM_F, *$(ext));)

cleanpdf:
	$(call RM_F, $(BASENAME).pdf)
