# vim:ft=make
-include config.mk

.PHONY: all clean dist-clean chapters chapters-clean

### Variables
MAIN_SRC  ?= main.tex
TEXENGINE ?= latexmk --pdf
CHAPTERS_DIR = ./chapters


MAIN_TARGET = $(patsubst %.tex,%.pdf,$(MAIN_SRC))
CHAPTER_SRC = $(wildcard $(CHAPTERS_DIR)/*.tex)
CHAPTER_TARGET = $(patsubst %.tex,%.pdf,$(CHAPTER_SRC))
CHAPTER_TARGET_NAMES = $(notdir $(CHAPTER_TARGET))

### Targets
all: $(MAIN_TARGET)

dist: all clean

chapters: $(CHAPTER_TARGET)
	make -C "chapters" all
chapters-clean:
	make -C "chapters" clean

$(CHAPTER_TARGET) $(CHAPTER_TARGET_NAMES) : %.pdf:
	#$(TEXENGINE) -cd $^
	make -C "chapters" $(patsubst $(CHAPTERS_DIR),,$@)

$(MAIN_TARGET): $(MAIN_SRC) $(CHAPTER_SRC) preamble.tex
	$(TEXENGINE) $<

dist-clean: clean
	@rm -vf *.pdf *.eps *.dvi *.ps

clean:
	@rm -vf *.dat *.log *.out *.aux *.nav *.snm *.toc *.vrb *~ *.fls *.fdb_latexmk *-blx.bib *.bbl *.blg *.run.xml
