# Environment vars
DIST_DIR := dist

# Computed vars
SRC_HTMLS := $(shell find src -name '*.src.html')
SRC_MDS := $(shell find src -name '*.src.md')

GEN_HTMLS := $(SRC_HTMLS:%.src.html=%.gen.html)
GEN_MDS := $(SRC_MDS:%.src.md=%.gen.html)
GEN_FILES := $(GEN_HTMLS) $(GEN_MDS)

INCLUDED_FILES := $(shell find src/include -type f)
DOWNLOAD_FILES := $(shell find src/download -type f)

DIST_GEN_FILES := $(GEN_FILES:src/%.gen.html=$(DIST_DIR)/%.html)
DIST_INC_FILES := $(INCLUDED_FILES:src/%=$(DIST_DIR)/%)
DIST_DWN_FILES := $(DOWNLOAD_FILES:src/%=$(DIST_DIR)/%)
DIST_FILES := $(DIST_GEN_FILES) $(DIST_INC_FILES) $(DIST_DWN_FILES) $(DIST_DIR)/favicon.ico

PANDOC_GEN := pandoc --standalone --wrap=none --template src/templates/main.html --metadata-file src/meta.yml

.PHONY: build dist
build: $(GEN_FILES)
	$(info HTML generated)

%.gen.html: %.src.html
	$(PANDOC_GEN) --output $@ $<

%.gen.html: %.src.md
	$(PANDOC_GEN) --output $@ $<

dist: $(DIST_DIR)
	$(info Distributed folder generated)

$(DIST_DIR): $(DIST_FILES)

$(DIST_DIR)/include/%: src/include/%
	mkdir --parents $(dir $@)
	cp $< $@

$(DIST_DIR)/download/%: src/download/%
	mkdir --parents $(dir $@)
	cp $< $@

$(DIST_DIR)/%.html: src/%.gen.html
	mkdir --parents $(dir $@)
	cp $< $@

$(DIST_DIR)/favicon.ico: src/favicon.ico
	cp $< $@

.PHONY: clean
clean:
	rm -f $(GEN_FILES)

clean-dist:
	rm -f $(DIST_FILES)

.PHONY: debug
debug:
	$(info SRC_HTMLS $(SRC_HTMLS))
	$(info SRC_MDS $(SRC_MDS))
	$(info GEN_HTMLS $(GEN_HTMLS))
	$(info GEN_MDS $(GEN_MDS))
	$(info GEN_FILES $(GEN_FILES))
	$(info DIST_FILES $(DIST_FILES))
