# List of all input files. Each file is converted into a .html file at the same path.
#
# The format of each line is: $path $URL $title
#
# If no $URL is given or the $URL is '-', then the input file is assumed to be
# in dat/, otherwise it will be fetched from $URL.
#
# A $title should be given for .pod and .log files, it is ignored for .md files
# because those already have a title embedded in the file.
#
# Supported file types:
#   .md:  Converted directly into .html with pandoc.
#   .pod: Perl's Plain Old Documentation, converted through HTML into a .md
#         file which is then converted into .html again with the proper template.
#   .log: A ChangeLog-formatted file, converted through .md into .html.
PAGES=\
	"doc.md"\
	"doc/commvis.md"\
	"doc/dcstats.md"\
	"doc/easyipc.md"\
	"doc/funcweb.md"\
	"doc/sqlaccess.md"\
	"dump.md"\
	"dump/awshrink.md"\
	"dump/btrfssize.md"\
	"dump/demo.md"\
	"dump/grenamr.md"\
	"dump/insbench.md"\
	"dump/nccolour.md"\
	"globster.md"\
	"globster/api.pod           https://g.blicky.net/globster.git/plain/doc/api.pod             The Globster D-Bus API"\
	"globster/ctl.pod           https://g.blicky.net/globster.git/plain/doc/globsterctl.pod     The globsterctl(1) Man Page"\
	"globster/daemon.pod        https://g.blicky.net/globster.git/plain/doc/globster.pod        The globster(1) Man Page"\
	"globster/launch.pod        https://g.blicky.net/globster.git/plain/doc/globster-launch.pod The globster-launch(1) Man Page"\
	"index.md"\
	"ncdc.md"\
	"ncdc/changes.log           https://g.blicky.net/ncdc.git/plain/ChangeLog                   Ncdc Release History"\
	"ncdc/faq.md"\
	"ncdc/install.md"\
	"ncdc/man.pod               -                                                               Ncdc Manual"\
	"ncdc/scr.md"\
	"ncdu.md"\
	"ncdu/changes.log           https://g.blicky.net/ncdu.git/plain/ChangeLog                   Ncdu Release History"\
	"ncdu/jsonfmt.md"\
	"ncdu/man.pod               https://g.blicky.net/ncdu.git/plain/doc/ncdu.pod                Ncdu Manual"\
	"ncdu/scr.md"\
	"nginx-confgen.md"\
	"nginx-confgen/changes.log  https://g.blicky.net/nginx-confgen.git/plain/ChangeLog          Nginx-confgen Release History"\
	"nginx-confgen/man.pod      https://g.blicky.net/nginx-confgen.git/plain/nginx-confgen.pod  The nginx-confgen(1) Man Page"\
	"tuwf.md"\
	"tuwf/changes.log           https://g.blicky.net/tuwf.git/plain/ChangeLog                   TUWF Release History"\
	"tuwf/man.pod               https://g.blicky.net/tuwf.git/plain/lib/TUWF.pod                TUWF Documentation"\
	"tuwf/man/db.pod            https://g.blicky.net/tuwf.git/plain/lib/TUWF/DB.pod             TUWF::DB Documentation"\
	"tuwf/man/intro.pod         https://g.blicky.net/tuwf.git/plain/lib/TUWF/Intro.pod          TUWF::Intro Documentation"\
	"tuwf/man/misc.pod          https://g.blicky.net/tuwf.git/plain/lib/TUWF/Misc.pod           TUWF::Misc Documentation"\
	"tuwf/man/request.pod       https://g.blicky.net/tuwf.git/plain/lib/TUWF/Request.pod        TUWF::Request Documentation"\
	"tuwf/man/response.pod      https://g.blicky.net/tuwf.git/plain/lib/TUWF/Response.pod       TUWF::Response Documentation"\
	"tuwf/man/validate.pod      https://g.blicky.net/tuwf.git/plain/lib/TUWF/Validate.pod       TUWF::Validate Documentation"\
	"tuwf/man/xml.pod           https://g.blicky.net/tuwf.git/plain/lib/TUWF/XML.pod            TUWF::XML Documentation"\
	"ylib.pod                   https://g.blicky.net/ylib.git/plain/README.pod                  Ylib"\
	"yxml.md"\
	"yxml/man.md                https://g.blicky.net/yxml.git/plain/yxml.md"


# Files generated by mkfeed.pl
FEEDS=\
	pub/feed.atom\
	pub/globster/feed.atom\
	pub/ncdc/feed.atom\
	pub/ncdu/feed.atom\
	pub/tuwf/feed.atom\
	pub/yxml/feed.atom


# Files we need to download
FETCH      := $(shell for i in ${PAGES}; do echo "$$i" | grep -Eo '^[^ ]+ +[^ -][^ ]+' | sed -E 's/^([^ ]+).*/dat\/\1/'; done)

# List of generated .html files
HTML_OUT   := $(shell for i in ${PAGES}; do echo "$$i" | sed -E 's/^([^ ]+)\.[^\. ]+.*$$/pub\/\1.html/'; done)

# List of .md files generated from .pod files
POD_MD     := $(shell for i in ${PAGES}; do echo "$$i" | grep -Eo '^[^ ]+\.pod' | sed -E 's/(.+)\.pod$$/dat\/\1.md/'; done)

# List of .md files generated from .log files
CHANGES_MD := $(shell for i in ${PAGES}; do echo "$$i" | grep -Eo '^[^ ]+\.log' | sed -E 's/(.+)\.log$$/dat\/\1.md/'; done)

# All fetched & generated files
CLEAN      := ${FETCH} ${POD_MD} ${CHANGES_MD} ${HTML_OUT} ${FEEDS}


.PHONY: all clean

all: .gitignore ${HTML_OUT} ${FEEDS}


${FEEDS}: mkfeed.pl dat/index.md
	@echo "FEED   $@"
	@mkdir -p $$(dirname "$@")
	@./mkfeed.pl "$@" <dat/index.md >"$@"


${FETCH}: dat/%:
	@echo "FETCH  $*"
	@mkdir -p $$(dirname "$@")
	@curl -s ${shell for i in ${PAGES}; do case "$$i" in "$* "*) echo "$$i" | awk '{print$$2}';; esac; done} -o "$@"


# There is a 'pod2markdown' program, but going through HTML with a little bit
# of Perl magic tends to give better results, if only because definition lists
# are properly converted this way and I have more control over links.
# The final pass through perl is to fix a pandoc bug where a code block as the
# first thing inside a definition does not survive a round-trip.
${POD_MD}: dat/%.md: dat/%.pod mkpod.pl
	@echo "POD    $*"
	@cat "$<" | ./mkpod.pl |\
		pandoc -f html -t markdown -s \
			--metadata title="${shell for i in ${PAGES}; do case "$$i" in "$*.pod "*) echo "$$i" | sed -E 's/[^ ]+ +[^ ]+ +//';; esac; done}" |\
		perl -e '$$/=undef; print (scalar(<>) =~ s/:   ([^\s].*)\n        ([^\s].*)/:       $$1\n        $$2/gr)' >"$@"


${CHANGES_MD}: dat/%.md: dat/%.log mkchangelog.pl
	@echo "MD     $*"
	@./mkchangelog.pl "$*" "${shell for i in ${PAGES}; do case "$$i" in "$*.log "*) echo "$$i" | sed -E 's/[^ ]+ +[^ ]+ +//';; esac; done}" <"$<" >"$@"


# TODO: Add --strip-comments to pandoc when I have a version that supports it.
${HTML_OUT}: pub/%.html: dat/%.md template.html
	@echo "HTML   $*"
	@mkdir -p $$(dirname "$@")
	@cat "$<" |\
		perl -pe 's{\[dllink ([^ \]]+)\]}{<a href="/download/$$1">$$1</a><b class="sig"><a href="/download/$$1.asc">pgp</a>-<a href="/download/$$1.sha1">sha1</a>-<a href="/download/$$1.md5">md5</a></b>}' |\
		pandoc -f markdown -t html5 --template template.html \
			--metadata path1=$$(echo "$*" | sed 's/\/.*//') \
			--metadata path2=$$(echo "$*" | sed 's/\//-/' | sed 's/\/.*//') \
			--metadata path3=$$(echo "$*" | sed 's/\//-/g') \
			--variable menu-$$(case "$*" in\
					globster*)      echo "globster";;\
					ncdc*)          echo "ncdc";;\
					ncdu*)          echo "ncdu";;\
					nginx-confgen*) echo "nginx-confgen";;\
					tuwf*)          echo "tuwf";;\
					yxml*)          echo "yxml";;\
					*) echo "main";;\
				esac)\
			-o "$@"


.gitignore: Makefile
	@echo "GIT"
	@echo '*.zip'  >$@
	@echo '*.gz'  >>$@
	@echo '*.pdf' >>$@
	@for i in ${CLEAN}; do echo "$$i"; done | sort >>$@


clean:
	rm -rf ${CLEAN}
	find dat pub -type d -empty -print -delete
