SHELL = /bin/sh

SRCDIR := src
BUILDDIR := build

INSTALLDIR_HTML := public_html/eh/eh/eh/eh-eh/
INSTALLDIR_ANSI := public_ansi/

ARTICLEDIR := articles
PANDOCDIR := pandoc
STYLEDIR := styles
IMAGEDIR := images

# The name of the stylesheet. This needs to be copied to any directory containing html files that use it.
style := $(STYLEDIR)/style.css

# Main html template that pandoc uses to generate .html from .md files
template_html := $(PANDOCDIR)/html-template.html
template_ansi := $(PANDOCDIR)/ansi-template
template_rss := $(PANDOCDIR)/rss-template.xml
# Template for extracting metadata of .md files in json format
list_template := $(PANDOCDIR)/metadata-template.json

# Location of the shark.js
shark_js := $(PANDOCDIR)/shark.js

# List of articles with their metadata to be used in the creation of the article list in index.html 
article_list := $(SRCDIR)/articles.json

# article targets
articles_md := $(wildcard $(SRCDIR)/$(ARTICLEDIR)/*.md)

articles_html := $(patsubst $(SRCDIR)/%.md, $(BUILDDIR)/%.html, $(articles_md))
articles_ansi := $(patsubst $(SRCDIR)/%.md, $(BUILDDIR)/%, $(articles_md))

all: $(BUILDDIR)/index.html $(BUILDDIR)/index $(BUILDDIR)/rss.xml

install:
	cp -r $(BUILDDIR)/index.html $(BUILDDIR)/rss.xml $(articles_html) $(STYLEDIR) $(IMAGEDIR) $(INSTALLDIR_HTML); \
	cp -r $(BUILDDIR)/index $(articles_ansi) $(INSTALLDIR_ANSI);

clean:
	echo '{"articles": []}' > $(article_list); \
	rm -r $(BUILDDIR)

$(BUILDDIR):
	mkdir $(BUILDDIR) $(BUILDDIR)/$(ARTICLEDIR)

$(BUILDDIR)/$(ARTICLEDIR)/%.html: $(SRCDIR)/$(ARTICLEDIR)/%.md | $(BUILDDIR)
	jq 'del(.articles[] | select(.filename == "$(patsubst %.html,%,$(notdir $@))"))' $(article_list) > articles.json.tmp; \
	mv articles.json.tmp $(article_list); \
	# Build articles using pandoc
	pandoc $< --template $(template_html) --css $(style) --highlight-style zenburn --mathml --toc --output $@; \
	# Extract metadata from .md file, append filename field to the article object.
	# This is used to link to the article from index.html. Append article object to the article list.
	jq '.articles += [$(shell pandoc $< --template $(list_template) | jq '. += {"filename": "$(patsubst %.html,%,$(notdir $@))", "pubdate": "$(shell date -d $(shell pandoc $< --template $(list_template) | jq '.date') --rfc-2822)"}')]' $(article_list) > articles.json.tmp; \
	mv articles.json.tmp $(article_list);
	

$(BUILDDIR)/$(ARTICLEDIR)/%: $(SRCDIR)/$(ARTICLEDIR)/%.md | $(BUILDDIR)
	pandoc $< --template $(template_ansi) --to ansi --output $@

$(BUILDDIR)/index.html: $(SRCDIR)/index.md $(articles_html) | $(BUILDDIR)
	# Sort articles decending by date (date need to be in ISO 8601 format)
	jq '.articles |= sort_by(.date) | .articles |= reverse' ${article_list} > articles.json.tmp; \
	mv articles.json.tmp ${article_list}; \
	# Build index.html with metadata injected from articles_list
	pandoc $< --template $(template_html) --include-after-body $(shark_js) --css $(style) --metadata-file $(article_list) --highlight-style zenburn --mathml --toc --output $@;

$(BUILDDIR)/index: $(SRCDIR)/index.md $(articles_ansi) | $(BUILDDIR)
	pandoc $< --template $(template_ansi) --metadata-file $(article_list) --to ansi --output $@

$(BUILDDIR)/rss.xml: $(SRCDIR)/rss.md $(articles_html) | $(BUILDDIR)
	pandoc $< --template $(template_rss) --metadata-file $(article_list) --metadata=pubdate:"$(shell jq -r '.articles[0] | .pubdate' $(article_list))" --metadata=builddate:"$(shell date --rfc-2822)" --to html --output $@
