# MAKEFLAGS := --jobs=$(shell nproc)
# MAKEFLAGS += --output-sync=target

# ======================================================================
# EDIT THESE OPTIONS
#
# Change this to the base name of the document (without .tex)
DOCUMENT=test_report
#======================================================================

# Path to the test artifacts: generated by the CI stages or test test files for test test report
TEST_ARTIFACTS_PATH?=$(PWD)/

$(info TEST_ARTIFACTS_PATH: $(TEST_ARTIFACTS_PATH))

# Repository-specific likely hand-written tex files
TEX_CONTENT_PATH?=$(BASE_PROJECT_DIR)/report

export REPORT_XML?=report.xml

# Don't edit below this line
# --------------------------

# TODO: requirements_traceability.tex is currently skipped, return once Jama is usable
GENERATED_TEX:=unittests.tex coverage.tex pylint.tex integration_tests.tex sonarqube.tex customization.tex dependencies.tex dependencies_detailed.tex toolkitinfo.tex
COPIED_TEX:=$(wildcard $(TEX_CONTENT_PATH)/*.tex)

COPIED_TEX:=$(subst $(TEX_CONTENT_PATH)/,,$(COPIED_TEX))

$(info GENERATED_TEX: $(GENERATED_TEX))
$(info COPIED_TEX: $(COPIED_TEX))


# ensure the document class in in computing-latex-class are found
INPUTVARS=BIBINPUTS=computing-latex-class: TEXINPUTS=build:computing-latex-class:
GLOSSARIES=$(wildcard cta-computing-glossary/glossary-*.yaml)

all: build/$(DOCUMENT).pdf build/$(DOCUMENT).tgz
report: all

# Utilities and options needed to generate the documents
LATEXMK=$(INPUTVARS) latexmk -r computing-latex-class/latexmkrc -file-line-error -synctex=1
GLOSS2LATEX=cta-computing-glossary/glossary-to-latex.py
GLOSS2LATEX_OPTS=--source --cross-ref --hide-source "ctao-computing"

# the full glossary of everything listed in GLOSSARIES
build/glossary.tex: $(GLOSSARIES) $(GLOSS2LATEX) | build
	$(GLOSS2LATEX) $(GLOSS2LATEX_OPTS) $(GLOSSARIES) > $@

# Main rule to build the document
build/$(DOCUMENT).pdf: $(DOCUMENT).tex build/glossary.tex references.bib $(foreach fn,$(GENERATED_TEX) $(COPIED_TEX),build/$(fn))
	$(LATEXMK) $<

build/$(DOCUMENT).tgz: $(DOCUMENT).tex build/glossary.tex references.bib $(foreach fn,$(GENERATED_TEX) $(COPIED_TEX),build/$(fn))
	echo -e "build/$(DOCUMENT).pdf:\n\t$(LATEXMK)" > build/Makefile.for-tgz
	tar --transform='flags=r;s|build/Makefile.for-tgz|Makefile|' --exclude .git -cvzf $@ build/Makefile.for-tgz computing-latex-class $^


# rule to generate a DOCX (Microsoft Word) document from the source
# in case this is needed for some reason
build/%.docx: %.tex
	pandoc --default-image-extension=pdf --toc $< -t docx -o $@

clean::
	$(LATEXMK) -c
	$(RM) -r build

build:
	mkdir -p $@

$(foreach fn,$(GENERATED_TEX),build/$(fn)) &: $(TEST_ARTIFACTS_PATH)/coverage.xml $(TEST_ARTIFACTS_PATH)/report.xml $(TEST_ARTIFACTS_PATH)/pylint.json | build
	aiv-report -c $(AIV_CONFIG) all \
		--toolkitinfo-output build/toolkitinfo.tex \
		--dependencies-output build/dependencies.tex \
		--dependencies_detailed-output build/dependencies_detailed.tex \
		--customization-output build/customization.tex \
		--sonarqube-output build/sonarqube.tex \
		--pylint-output build/pylint.tex \
		--coverage-output build/coverage.tex \
		--unittests-output build/unittests.tex \
		--integration_tests-output build/integration_tests.tex \
		$(TEST_ARTIFACTS_PATH)/coverage.xml $(TEST_ARTIFACTS_PATH)/report.xml $(TEST_ARTIFACTS_PATH)/pylint.json $(TEST_ARTIFACTS_PATH)/report.xml


$(info build/%.tex: $(TEX_CONTENT_PATH)/%.tex)

build/%.tex: $(TEX_CONTENT_PATH)/%.tex | build
	cp -fv $< $@

FORCE:

.PHONY: all clean FORCE
