#  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/>.


#  REQUIRED
#
#    - gpp:      $ sudo apt install gpp
#    - pandoc:   VERSION 2.14.2
#    			 https://github.com/jgm/pandoc/releases
#    			 https://pandoc.org/installing.html
#    - jabref:   https://www.jabref.org/
#
#    .deb packages are installed with: $ sudo dpkg -i package.deb


#  USAGE
#
#    $ make


#  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.')
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)
LINK_BY := [CC BY](https://creativecommons.org/licenses/by/4.0/)<br/>[![[M↓]](../../images/markdown.transparent.png)Markdown]($(NAME).md), [makefile](makefile)
LINK_BYNCSA := [CC BY-NC-SA](https://creativecommons.org/licenses/by-nc-sa/4.0/)<br/>[![[M↓]](../../images/markdown.transparent.png)Markdown]($(NAME).md), [makefile](makefile)
AFTER_BY := ../../pandoc/index/en/by/after.html
AFTER_BYNCSA := ../../pandoc/index/en/by-nc-sa/after.html

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

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

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

all: html
F5:  html

html: index.html

index.html: $(NAME).md
	echo $(PANDOC)
	rm -f ../$(NAME).bib.bak
	if [ -a ../doc/update ]; then ../doc/update; fi;
	pandoc <(sed 's|$$(BY)|$(LINK_BY)|g; s|$$(BYNCSA)|$(LINK_BYNCSA)|g; s|$$@|$$@|g; s|(http://hamwaves\.com|(../..|g; s|(https://hamwaves\.com|(../..|g; $(SEDSCRIPTS)' $< \
			|gpp -I../ -U "\\" "" "{" "}{" "}" "{" "}" "#" "") \
		-V lang='en' \
		--from='markdown+smart' --to='html5' \
		--template='../../pandoc/index/screen.html' \
		--css='../../css/index.css' \
		--email-obfuscation=javascript \
		--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 ../../pandoc/index/before.html \
		-A <(sed 's|$$(NAME)|$(NAME)|g; s|$$(DATE)|$(DATE)|g; s|$$(PANDOC)|$(PANDOC)|g' $(AFTER)) \
		--columns=999 \
		--output=$@ && \
	sed -i 's| type="text/javascript"||g;    s|href="ftp|target="_blank" href="ftp|g;    s|a href="http|a target="_blank" href="http|g;    s|href="../doc/|target="_blank" href="../doc/|g;    s|^<li>​</li>|<li class="ad"><ins class="adsbygoogle" style="display:block" data-ad-format="fluid" data-ad-layout-key="-hm+l-2r-7o+of" data-ad-client="ca-pub-9998293660978778" data-ad-slot="8016605485" data-restrict-data-processing="1"></ins><script>(adsbygoogle = window.adsbygoogle \|\| []).push({});</script></li>|g;    s|^<li>d</li>|<li class="donate"><a href="https://www.paypal.com/cgi-bin/webscr?cmd=_donations\&amp;business=MDY3AC25GHVFC\&amp;lc=US\&amp;amount=10\&amp;currency_code=EUR\&amp;item_name=One\%20month\%20worth\%20of\%20hosting\%20expenses\%20donated\%20to\%20hamwaves.com\&amp;item_number=Single,\%20one-time\%20donation\&amp;bn=PP-DonationsBF:btn_donate_LG.gif:NonHosted" target="_blank">Keep this site on‑line<br/><img src="../../images/donate.png" alt="Donate"/></a></li>|g' $@

clean:
	rm -f index.html
