# This file is part of Beastie <https://purl.org/nxg/dist/beastie>
#
# SPDX-FileCopyrightText: 2023 Norman Gray <https://nxg.me.uk>
# SPDX-License-Identifier: BSD-2-Clause

# Build the documentation and webpage.

CSS=build/nxg.css
ASSEMBLE_FOR_DIST=-l $(CSS)

TARBALL_NAME=beastie-@PACKAGE_VERSION@

# if we wanted to get fancy, we could extract this list from the parsed .md files
IMAGES=klipspringer.jpg

DIST=dist



all: $(DIST)/beastie.xhtml

# not-quite-dummy target to ensure that ../beastie does exist
../beastie:
	echo "../beastie doesn't exist, when starting to build the distribution!"
	false

# The following also builds dist/beastie.xhtml and dist/index.xhtml,
# and various other files, most particularly the contents of the
# examples directory.
#
# A portion of the documentation is extracted from the in-code
# comments within ../beastie.
#
# This target is needed when building dist in the parent makefile, as
# well as when assembling the webpage.
#
# This target also depends on *.md, but that's not reflected here.
# The list of modules built into the documentation is controlled via
# the `*modules*` variable in assemble.docs.scm.
$(DIST)/beastie.xhtml: ../beastie assemble-docs.scm \
		beastie.md vs-bibtex.md webpage.md scheme.md $(CSS) \
		build/examples-manifest $(DIST)/beastie.1.txt $(IMAGES:%=$(DIST)/%)
	rm -f $@
	../beastie assemble-docs.scm $(ASSEMBLE_FOR_DIST) $(DIST) || rm -f $@

$(DIST)/beastie.1.txt: beastie.1
	test -d $(DIST) || mkdir $(DIST)
	cp beastie.1 $(DIST)
	man -P cat ./beastie.1 | col -bx >$@

$(DIST)/%.jpg: %.jpg
	cp $< $@

# stock the contents of dist/examples, and leave information in
# build/examples-manifest
# (the dependency on beastie.1.txt ensures dist/ pre-exists,
# and so avoids a make -j race)
build/examples-manifest: ../examples $(DIST)/beastie.1.txt build/stamp
	test -d $(DIST)/examples || mkdir -p $(DIST)/examples
	cd ../examples; $(MAKE) build/bundle-manifest
	cat ../examples/build/bundle-manifest \
	  | while read f; do cp ../examples/$$f $(DIST)/examples/$$f.txt; done
	awk 'BEGIN {printf "("} END {print ")"} { printf "(\"%s\" \"%s.txt\")\n", $$1, $$1}' \
	  ../examples/build/bundle-manifest >$@.tmp && mv $@.tmp $@

build/stamp:
	test -d build || mkdir build
	date >$@

build/webpage-tarball.tar: build/stamp
	if test -d $(DIST); then rm -Rf $(DIST); fi
	rm -f build/examples-manifest
	mkdir build/$(TARBALL_NAME)
	$(MAKE) ASSEMBLE_FOR_DIST= DIST=build/$(TARBALL_NAME)
	cd build; tar cf ../$@ $(TARBALL_NAME)

build/dist-doc.tar: .
	tar cf $@ --exclude build --exclude dist --exclude-vcs .

$(CSS): build/stamp
	curl https://nxg.me.uk/style/base.css >$@

clean:
	rm -Rf webpage-tarball.tar build $(DIST)
# compiler symbol tables (only on macOS?)
	rm -Rf *.dSYM
