##################################################################
#                           Detect OS                            #
##################################################################

OS_ID := $(shell . /etc/os-release && echo $$ID)
OS_VER := $(shell . /etc/os-release && echo $$VERSION_ID)
VENV_DIR := ./venv-$(OS_ID)$(OS_VER)


##################################################################
#                        Exports targets                         #
##################################################################

.PHONY: all html html-all-in-one epub pdf venv clean clean-venv serve

all: html html-all-in-one pdf epub

html: public_html/.built
epub: LdapSaisie.epub
pdf:  LdapSaisie.pdf


##################################################################
#              Build virtual environment (by OS)                 #
##################################################################

$(VENV_DIR):
	python3 -m venv $(VENV_DIR)

$(VENV_DIR)/.deps-installed: requirements.txt | $(VENV_DIR)
	$(VENV_DIR)/bin/python -m pip install --upgrade pip setuptools
	$(VENV_DIR)/bin/python -m pip install -r requirements.txt
	touch $@

venv: $(VENV_DIR) $(VENV_DIR)/.deps-installed


##################################################################
#                          HTML exports                          #
##################################################################

public_html/.built: $(VENV_DIR)/.deps-installed mkdocs.yml
	$(VENV_DIR)/bin/mkdocs build -s
	touch $@

html-all-in-one: public_html/.built clean-all-in-one-html-file.py
	$(VENV_DIR)/bin/htmlark public_html/print_page/index.html -o LdapSaisie.tmp.html
	$(VENV_DIR)/bin/python clean-all-in-one-html-file.py LdapSaisie.tmp.html LdapSaisie.html
	rm -f LdapSaisie.tmp.html


##################################################################
#                           PDF export                           #
##################################################################

LdapSaisie.pdf: LdapSaisie.html
	docker run --rm \
		-v $(CURDIR):/workspace pink33n/html-to-pdf \
			--url file:///workspace/LdapSaisie.html \
			--pdf $@


##################################################################
#                           EPUB export                          #
##################################################################

LdapSaisie.epub: LdapSaisie.html
	pandoc \
		LdapSaisie.html \
		-f html \
		--toc \
		--standalone \
		--top-level-division=chapter \
		--highlight-style style/pygments.theme \
		--css style/epub.css \
		--metadata=title:"Documentation LdapSaisie" \
		--metadata=author:"Benjamin Renard" \
		--metadata=lang:"fr-FR" \
		--metadata=cover-image:"../logos/ldapsaisie-logo.png" \
		--pdf-engine=weasyprint \
		-o $@


##################################################################
#                            Cleaning                            #
##################################################################
clean:
	rm -fr public_html LdapSaisie.html LdapSaisie.pdf LdapSaisie.epub

clean-venv:
	rm -fr $(VENV_DIR)


##################################################################
#                               Dev                              #
##################################################################

serve: $(VENV_DIR)/.deps-installed
	$(VENV_DIR)/bin/mkdocs serve


##################################################################
#                         Debian package                         #
# Note: target call from debian package rules file to build doc. #
##################################################################

debian: html-all-in-one