## Source files
## (Adjust to your needs. Order of markdown files in $(SRC) matters!)
PANDOC 									=	pandoc
DOC 										= Docs
META                    = Assets/metadata.yaml
SRC                     = $(DOC)/Project.md
BIBFILE                 = Assets/Citations.bib
TARGET                  = $(DOC)/Project.pdf



###############################################################################
## Internal setup (do not change)
###############################################################################


## Auxiliary files
## (Do not change!)
TITLEPAGE               = titlepage.tex
TMP                     = Assets/$(TITLEPAGE:%.tex=__%.filled.tex)


## Pandoc options
AUX_OPTS                =

OPTIONS                 = -f markdown
OPTIONS                += --pdf-engine=pdflatex
OPTIONS                += --filter=pandoc-citeproc
OPTIONS                += --metadata-file=$(META)
OPTIONS                += -M bibliography=$(BIBFILE)
OPTIONS                += --listings
OPTIONS                += --include-in-header=$(TMP)
OPTIONS                += --resource-path=./Assets/Diagrams


## Template variables
TEMPLATE_DL_DIR         = .tmp_template_dl

CLEANTHESIS_TEMPLATE    = Assets/cleanthesis.sty

TEMPLATE_FILES          = $(CLEANTHESIS_TEMPLATE)

###############################################################################
## Main targets (do not change)
###############################################################################

$(SRC): $(DOC)/Project.org
	$(PANDOC) -o $@ $<

## Use Clean Thesis template (https://github.com/derric/cleanthesis)
cleanthesis: TEMPLATE_FILE    += $(CLEANTHESIS_TEMPLATE)
cleanthesis: TEMPLATE_REPO    += $(CLEANTHESIS_REPO)
cleanthesis: TEMPLATE_VERSION += $(CLEANTHESIS_VERSION)
cleanthesis: AUX_OPTS         += -M cleanthesis=true -M cleanthesisbibfile=$(BIBFILE:%.bib=%)
cleanthesis: OPTIONS          += --include-in-header=Assets/include-header.tex $(AUX_OPTS)
cleanthesis: $(CLEANTHESIS_TEMPLATE) $(TARGET)


## Clean-up: Remove temporary (generated) files and download folder
clean:
	rm -rf $(TMP) $(TEMPLATE_DL_DIR)


## Clean-up: Remove also genereated thesis and template files
distclean: clean
	rm -f $(TARGET)



###############################################################################
## Auxiliary targets (do not change)
###############################################################################


## Download template files

## Build thesis
${TARGET}: $(SRC) $(META) $(BIBFILE) $(TMP)
	$(PANDOC) ${OPTIONS} -o $@ $(SRC)


## Build auxiliary files (title page, references)
$(TMP): Assets/__%.filled.tex: Assets/%.tex $(META)
	$(PANDOC) $(AUX_OPTS) --template=$< --metadata-file=$(META) -o $@ $<


###############################################################################
## Declaration of phony targets
###############################################################################


.PHONY: cleanthesis clean distclean
