# This makefile provides a simple regression test for a couple of the
# example scripts.  The intention is not to thoroughly exercise them,
# but to confirm there isn't any basic breakage.
#
# It would be reasonable for these tests to grow, since to some extent
# the scripts in this directory are part of the suite that beastie
# delivers.  That also suggests that I should pick a different name
# from examples/, to suggest that.
#
# This file is part of Beastie <https://purl.org/nxg/dist/beastie>
# SPDX-FileCopyrightText: 2024 Norman Gray <https://nxg.me.uk>
# SPDX-License-Identifier: BSD-2-Clause

BEASTIE=../beastie


all: check

# The test-examples.scm script checks the following files:
#
#    build/bibdemo.xexpr (check parses and makes sense)
#    build/extract.bib   (ditto)
#    build/extract2.bbl and build/extract.yaml (same keys)
#
# The other dependencies are implicit checks,
# which are deemed to pass if they are built with a success exit status
check: build/stamp \
		build/bibdemo.xhtml build/bibdemo.xexpr \
		build/bibdemo-alt.xhtml build/refs.xhtml \
		build/extract.bib build/extract.crossref build/extract.yaml build/extract2.bbl
	$(BEASTIE) ../test/s7unit.scm test-examples.scm

# The pre-test actions below depend on $(BEASTIE),
# because we want to re-run these when the executable is rebuilt.
#
# That means we must also add a do-nothing rule here, (a) to ensure we
# fail if for some reason the executable we're testing doesn't exist
# (we could add `cd ..;make`, but... what's going on?), and (b) to
# avoid spuriously building the executable in some cases (eg, within
# CI, where this should already be present as a build artefact, but
# possibly with a slightly unpredictable creation date).
# If we don't have this rule, then the CI tries to build the executable
# using a default rule, which is wrong.
# In the CI case, the executable will be present because ../.gitlab-ci.yml
# includes it in the 'artifacts' list.
$(BEASTIE):
	echo "$(BEASTIE) doesn't exist, when starting to run the tests!"
	false

# bibliography-in-markdown.scm...
#
# Convert Markdown including citations.
# We don't check the XHTML output here -- we just check it builds without error.
# However the test-examples.scm test does do some minimal testing.
build/bibdemo.xhtml: bibdemo.md $(BEASTIE) build/stamp
	BIBINPUTS=../test $(BEASTIE) bibliography-in-markdown.scm $< >$@.tmp && mv $@.tmp $@
build/bibdemo.xexpr: bibdemo.md $(BEASTIE) build/stamp
	BIBINPUTS=../test $(BEASTIE) bibliography-in-markdown.scm -s $< >$@.tmp && mv $@.tmp $@

# ...and the alternate version.
# We don't do any tests on the output of this --
# we're only checking that it runs without error.
build/bibdemo-alt.xhtml: bibdemo-alt.md $(BEASTIE) build/stamp
	BIBINPUTS=../test $(BEASTIE) bibliography-in-markdown-alt.scm $< bib-t01-simple.bib >$@.tmp && mv $@.tmp $@


# extract-bib.scm...
# Note: extract-bib and aux-to-refs both implicitly test plain.scm.
build/extract.aux: Makefile build/stamp
	printf '\\citation{*}\n\\bibdata{bib-t01-simple}\n\\bibdata{bib-t02-strings}\n\\bibdata{bib-t03-torture}\n' >$@
# To validate the following, make build/extract2.bbl
build/extract.bib: build/extract.aux $(BEASTIE)
	BIBINPUTS=../test $(BEASTIE) extract-bib.scm build/extract.aux >$@ 2>$@.stderr
# It would be good to run the build/extract.yaml through yq, to validate it.
build/extract.yaml: build/extract.aux $(BEASTIE)
	BIBINPUTS=../test $(BEASTIE) extract-bib.scm -O yaml build/extract.aux >$@ 2>$@.stderr
# ...similarly xsltproc .../identity.xslt build/extract.crossref
build/extract.crossref: build/extract.aux $(BEASTIE)
	BIBINPUTS=../test $(BEASTIE) extract-bib.scm -O crossref build/extract.aux >$@ 2>$@.stderr

# Re-parse the build/extract.bib output using `bibtex`.
# This is checked to be compatible with the keys in build/extract.yaml in test-examples.scm
build/extract2.aux: Makefile build/stamp
	printf '\\citation{*}\n\\bibdata{extract}\n\\bibstyle{bst-t01-minimal}\n'>$@
# bibtex's errors and warnings go to stdout!
# see build/extract2.blg for log messages
# If there is no bibtex binary (which will be the case on the test runner,
# if we haven't installed it; cf notes in .gitlab-ci.yml),
# then write \bibitem{nobibtex} to this file,
# which test-examples.scm uses as a flag to skip this test
build/extract2.bbl: build/extract2.aux build/extract.bib
	if command -v bibtex>/dev/null; then \
	  cd build; BSTINPUTS=../../test bibtex extract2 >/dev/null; \
	else \
	  printf '\\bibitem{nobibtex}\n'>$@; \
	fi

# aux-to-refs...
# test: xsltproc to convert .xhtml to xexpr?
build/refs.xhtml: build/refs.aux plain.scm $(BEASTIE)
	rm -f $@
	{ echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'; \
	  echo '<html xmlns="http://www.w3.org/1999/xhtml"><head><title>refs</title></head><body><ul>'; \
	  BIBINPUTS=../test $(BEASTIE) aux-to-refs.scm build/refs.aux && \
	  echo '</ul></body></html>'; \
	} >$@

build/refs.aux: Makefile build/stamp
	printf '\\citation{*}\n\\bibdata{alltypes}\n\\bibstyle{plain.scm}\n' >$@

build/stamp:
	test -d build || mkdir build

# The collection of examples for distribution
# (see ../doc/Makefile)
build/bundle-manifest: . build/stamp
	ls *.scm | grep -v '^test' >$@

# the collection of examples for distribution
build/dist-examples.tar: .
	tar cf $@ --exclude-vcs --exclude build .

clean:
	rm -Rf build
