
export HGPLAIN=
HG=hg
Z     = gzip --force --keep --no-name --best

# Variables holding names of files of various types
MD   = $(shell $(HG) files -I '**.md' -X '_dirindex.md')
META = $(patsubst %.md,%.yaml,$(MD))
NOER = $(filter-out 4%.md,$(MD))
JSON = $(patsubst %.md,%.json,$(NOER))
FTS5 = $(patsubst %.md,%.sql,$(NOER))
TEXT = $(patsubst %.md,%.txt,$(NOER))
HTML = $(patsubst %.md,%.html,$(MD)) \
			 _dirindex_header.html _dirindex_footer.html
SVG  = $(shell $(HG) files -I '**.svg')
CSS  = $(shell $(HG) files -I '**.css')
OTHER= $(shell $(HG) files -I '**.svg' -I '**.css' -I '**.txt' -I '**.json' -X '_*')
GZ   = $(patsubst %,%.gz,index.html $(SVG) $(CSS) sitemap.xml)

OUTPUT = $(HTML) sitemap.xml

TITLE=Prosody IM
SCHEME=https:
DOMAIN=prosdy.docs.cloud9p.org
RELATIVE=//$(DOMAIN)/
ABSOLUTE=$(SCHEME)$(RELATIVE)
SOURCEREPO=//hg.$(DOMAIN)/site
SEARCH=

# Path to Bootstrap (CSS framework)
# This default path works on Debian when viewing files locally
BSPATH=/usr/share/javascript

# Absolute path to some local resources
# Set to empty string for live build
BASE=${PWD}

all: $(OUTPUT) $(GZ)
	chmod a+r $(OTHER)

# Extract page metadata from Mercurial
.SECONDARY: $(META)
%.yaml: %.md
	echo file: $(patsubst %.md,%,$^) > $@
	echo lang: en >> $@
	echo generator: $(shell $(MAKE) -v | head -n 1), $(shell pandoc -v | head -n 1) >> $@
#	$(HG) log $^ -T 'revision: {node|short}\ndate: {date|shortdate}\nauthor:\n' -l 1 >> $@
#	$(HG) log $^ -T '-   {author|person}\n' | sort -u >> $@
	echo "built '$@'"

%.json: %.md %.yaml _template.json
	pandoc \
		-T "$(TITLE)" \
		-f markdown+emoji \
		-t plain \
		--metadata-file="$*.yaml" \
		--template="$(lastword $^)" \
		--standalone \
		-o "$@" "$(firstword $^)"
	chmod a+r "$@"
	echo "built '$@'"

%.txt: %.md _template.txt
	pandoc \
		-f markdown+emoji \
		-t plain \
		--template="$(lastword $^)" \
		--standalone \
		-o "$@" "$(firstword $^)"
	chmod a+r "$@"
	echo "built '$@'"

# Main rule for turning markdown (and metadata) into HTML
%.html: %.md %.yaml _template.html
	pandoc \
		-T "$(TITLE)" \
		-V baseurl="$(RELATIVE)" \
		-V canonical="$(patsubst %.html,%,$@)" \
		-V urischeme="$(SCHEME)" \
		-V sourcerepo="$(SOURCEREPO)" \
		-V search="${SEARCH}" \
		-f markdown+emoji \
		-t html5 \
		--metadata-file="$*.yaml" \
		--template="$(lastword $^)" \
		--css=$(BSPATH)/bootstrap/css/bootstrap.min.css \
		--css=$(BASE)/prosody.css \
		--default-image-extension=svg \
		--standalone \
		--section-divs \
		--toc --toc-depth=3 \
		-o "$@" "$(firstword $^)"
	sed -i 's/<table>/<table class="table table-bordered">/' $@
	sed -i 's/<dl>/<dl class="dl-horizontal">/' $@
	chmod a+r "$@"
	echo "built '$@'"

# No metadata for special pages
# It doesn't make as much sense for these
401.yaml 403.yaml 404.yaml 410.yaml _dirindex.yaml:
	echo lang: en > $@
	echo generator: $(shell $(MAKE) -v | head -n 1), $(shell pandoc -v | head -n 1) >> $@

# Two parts for the nginx Fancy Index module
# A header and a footer are built here, the directory listing itself is
# inserted between them
_dirindex_header.html: _dirindex.html
	sed '/rel="\(canonical\|alternate\)"/d;s/<\/h1>/ /;T;q' $^ > $@
	chmod a+r "$@"

_dirindex_footer.html: _dirindex.html
	sed '0,/<\/h1>/d;/<footer/,/<\/footer>/d' < $^ > $@
	chmod a+r "$@"

sitemap.xml: $(META)
	sed -sn \
		-e 's|^file: \(.*\)|<url><loc>$(ABSOLUTE)\1</loc>|p' \
		-e 's|^date: \(.*\)|<lastmod>\1</lastmod></url>|p' -- $^ | \
		sed -e '1i<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' \
		-e '$$a</urlset>' > $@
	chmod a+r "$@"
	echo "built '$@'"

site.tar: $(MD) $(HTML) $(OTHER) sitemap.xml
	tar cf $@ \
		--posix \
		--exclude-vcs \
		--sort=name \
		--mtime=./sitemap.xml \
		--group=www-data:33 \
		--owner=www-data:33 \
		--mode=u=rwX,go=rX \
		--no-acls --no-selinux --no-xattrs \
	 	$^

site.zip: $(MD) $(HTML) $(OTHER) sitemap.xml
	zip -q $@ $^
	chmod a+r "$@"

site.sqlite3: $(FTS5) $(TEXT) $(JSON)
	cat _begin.sql $(filter %.sql,$?) _commit.sql | sqlite3 -batch -init _site.sql $@

%.sql: %.json %.txt
	printf "INSERT INTO \"site\" (\"filename\", \"metadata\", \"content\") VALUES ('%s', json(readfile('%s')), readfile('%s')) ON CONFLICT (\"filename\") DO UPDATE SET \"metadata\"=json(readfile('%s')), \"content\"=readfile('%s');\\n" $* $^ $^ > $@

%.gz: %
	$(Z) "$<"
	chmod a+r "$@"

# Rebuild only modified files
dev:
	$(MAKE) $(patsubst %.md,%.html,$(shell $(HG) stat -m -a -q -n -I '**.md'))

# Cleanup rules
clean:
	rm -fv $(OUTPUT) $(GZ) $(FTS5) $(JSON) $(TEXT)

drop-cache:
	rm -fv $(META)

cleaner: clean drop-cache

.DELETE_ON_ERROR:

.SILENT:

.PHONY: all clean drop-cache cleaner
