# The full path to the Makefile
MAKEFILE_PATH := $(abspath $(lastword $(MAKEFILE_LIST)))
# The path to the directory of the Makefile
MAKEFILE_DIR := $(dir $(MAKEFILE_PATH))

.SHELLFLAGS := -e -o pipefail -c

include $(MAKEFILE_DIR)/common.mk

SRC_DIRS := $(shell find "(" -not -path "./templates/*" -a -not -path "./latex/*" -a -not -path "*/Aufgaben/*" -a "(" -name "*.md" -o -name "*.tex" ")" ")" -printf "%h\n" | uniq)

# Hide or not the calls depending of VERBOSE
ifeq ($(VERBOSE),TRUE)
	HIDE :=
else
	HIDE := @
endif

.DEFAULT_GOAL := all
all: $(SRC_DIRS)

define generateRules
$(1):
	@echo \#\# Building $$@ \#\#
	@pushd $$(@) >/dev/null && lesson-make || popd >/dev/null
endef

$(foreach dir, $(SRC_DIRS), $(eval $(call generateRules, $(dir))))
$(eval $(call generateRules, templates))

.PHONY: $(SRC_DIRS) templates

