#  LICENSE
#
#    Copyright 2013-2023 Serge Y. Stroobandt
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.


#  REQUIRES
#
#    - gpp:      $ sudo apt install gpp
#    - pandoc:   VERSION 2.14.2
#    			 https://github.com/jgm/pandoc/releases
#    			 https://pandoc.org/installing.html
#    - pandoc-xnos:
#    	 $ pip install --upgrade pandoc-eqnos pandoc-fignos pandoc-secnos pandoc-tablenos
#    - jabref:   https://www.jabref.org/
#    - tex2svg:
#        $ sudo apt install npm
#        $ sudo npm install --global mathjax-node-cli
#    - tex-gyre: $ sudo apt install tex-gyre
#    - prince:   https://www.princexml.com/download/
#
#    .deb packages are installed with: $ sudo dpkg -i package.deb


#  USAGE
#
#    $ make
#    $ make pdf


#  CONTACT
#
#    echo c2VyZ2VAc3Ryb29iYW5kdC5jb20K |base64 -d


SHELL := /usr/bin/env bash
NAME := $(basename $(wildcard *.md))
DATE := $(shell LC_TIME='en_IE.UTF-8' date '+%A, %B %e, %Y.')
PDFDATE := $(shell date '+%FT%T')
PANDOC := $(shell pandoc --version |awk 'NR==1{print $$2}')

SEDSCRIPTS := $(shell sed 's:.*:/```/,/```/! s/\\(^\\| \\|\\*\\|_\\)&\ /\\1&\\\\\\ /g;:' '../../pandoc/article/en/shortbeginwords')
# These are nested substitutions.
# The first substitution serves for changing the lines in the list of short begin words in secondary substitution commands.
# Without escaping the entire nested substitution would read:
#     s:.*:/```/,/```/! s/\(^| |*|_\)&\ /\1&\\ /g;:
#
# /```/,/```/! prevents any secondary substitution from occurring in <PRE>-formatted blocks.
# Any secondary substitution changes a space behind a short begin word with a non-breaking space.

BY := $(shell grep '$$(BY)' $(NAME).md)
BYNCSA := $(shell grep '$$(BYNCSA)' $(NAME).md)
PDM := $(shell grep '$$(PDM)' $(NAME).md)
LINK_BY := licensed under [Creative Commons BY](https://creativecommons.org/licenses/by/4.0/)
LINK_BYNCSA := licensed under [Creative Commons BY-NC-SA](https://creativecommons.org/licenses/by-nc-sa/4.0/)
LINK_PDM := This work, identified by Serge Y. Stroobandt, is [free\\ of\\ known\\ copyright\\ restrictions](https://creativecommons.org/publicdomain/mark/1.0/).
AFTER_BY := ../../pandoc/article/en/by/after.html
AFTER_BYNCSA := ../../pandoc/article/en/by-nc-sa/after.html
AFTER_PDM := ../../pandoc/article/en/pdm/after.html

ifneq ($(BY),)
	AFTER := $(AFTER_BY)
endif

ifneq ($(BYNCSA),)
	AFTER := $(AFTER_BYNCSA)
endif

ifneq ($(PDM),)
	AFTER := $(AFTER_PDM)
endif

ifeq ($(wildcard ../$(NAME).bib),)
	BIB :=
else
	BIB := --citeproc --bibliography=../$(NAME).bib
endif

all: html
F5:  html
F6:  html pdf
pdf: a4 letter

html:   index.html
a4:     $(NAME).a4.pdf
letter: $(NAME).letter.pdf

index.html: $(NAME).md
	rm -f ../$(NAME).bib.bak
	if [ -a ../doc/update ]; then ../doc/update; fi;
	pandoc \
		../../pandoc/article/header-includes.yaml \
		<(sed 's|$$(BY)|$(LINK_BY)|g; s|$$(BYNCSA)|$(LINK_BYNCSA)|g; s|$$(PDM)|$(LINK_PDM)|g; s|$$@|$$@|g; s|(http://hamwaves\.com|(../..|g; s|(https://hamwaves\.com|(../..|g; $(SEDSCRIPTS)' \
			$< |gpp -I../ -U "\\" "" "{" "}{" "}" "{" "}" "#" "") \
		-V lang='en' \
		--from='markdown+smart' --to='html4' \
		--template='../../pandoc/article/screen.xhtml' \
		--css='../../css/screen/screen.css' \
		--toc --toc-depth=1 \
		--email-obfuscation='javascript' \
		--filter='pandoc-xnos' \
			-M xnos-cleveref=True \
			-M xnos-capitalise=True \
			-M xnos-number-by-section=False \
			-M fignos-plus-name='Figure' \
		--mathjax='https://cdn.jsdelivr.net/npm/mathjax@2/MathJax.js?config=TeX-AMS-MML_HTMLorMML' \
		$(BIB) \
		--csl='../../pandoc/article/csl/en.csl' \
			-M link-citations=True \
		-B <(sed 's|$$(NAME)|$(NAME)|g' ../../pandoc/article/en/before.html) \
		-A <(sed 's|$$(NAME)|$(NAME)|g; s|$$(DATE)|$(DATE)|g; s|$$(PANDOC)|$(PANDOC)|g' $(AFTER)) \
		--columns=999 \
		--output=$@ && \
	sed -i 's|href="ftp|target="_blank" href="ftp|g; s|href="http|target="_blank" href="http|g; s|<span class="math">\\\[|<span class="math display">\\\[|g' \
			$@

$(NAME).a4.pdf: $(NAME).md
	pandoc \
		../../pandoc/article/header-includes.yaml \
		<(sed 's|$$(BY)|$(LINK_BY)|g; s|$$(BYNCSA)|$(LINK_BYNCSA)|g; s|$$(PDM)|$(LINK_PDM)|g; s|$$@|$$@|g; s|(../doc/|(https://hamwaves\.com/$(NAME)/doc/|g; $(SEDSCRIPTS)' \
			$< |gpp -I../ -U "\\" "" "{" "}{" "}" "{" "}" "#" "") \
		-V lang='en' \
		-V pdfdate='$(PDFDATE)' \
		--from='markdown+smart' --to='html4' \
		--template='../../pandoc/article/print.xhtml' \
		--css='../../css/print/a4.css' \
		--filter='pandoc-xnos' \
			-M xnos-cleveref=True \
			-M xnos-capitalise=True \
			-M xnos-number-by-section=False \
			-M fignos-plus-name='Figure' \
		--mathml --lua-filter='../../pandoc/article/math2svg.lua' \
			-M math2svg_tex2svg='/usr/local/bin/tex2svg' \
			-M math2svg_font='Gyre-Pagella' \
		$(BIB) \
		--csl='../../pandoc/article/csl/en.csl' \
			-M link-citations=True \
		-A <(sed 's|$$(NAME)|$(NAME)|g; s|$$(DATE)|$(DATE)|g; s|$$(PANDOC)|$(PANDOC)|g' $(AFTER)) \
		--columns=999 \
		--output=$(NAME).print.xhtml
	prince --input=xml $(NAME).print.xhtml --output=$@ --pdf-profile='PDF/A-3b'

$(NAME).letter.pdf: $(NAME).md
	sed -i '0,/a4.css/ s/a4.css/letter.css/' $(NAME).print.xhtml
	prince --input=xml $(NAME).print.xhtml --output=$@ --pdf-profile='PDF/A-3b'
	rm $(NAME).print.xhtml

clean:
	rm -f *.print.xhtml
