# -*- sh -*-
# Build script for IPBT.

module ipbt

set Version $(!builddate).$(vcsid)

# use perl to avoid inconsistent behaviour of echo '\v'
in ipbt/doc do perl -e 'print "\n\\\\versionid ipbt version $$ARGV[0]\n"' $(Version) >> man-ipbt.but
in ipbt do perl -e 'print "$#define PACKAGE_VERSION \"$$ARGV[0]\"\n"' $(Version) > version.h

# In cmake/gitcommit.cmake, replace the default output "unavailable"
# with the commit string generated by bob, so that people rebuilding
# the source archive will still get a useful value.
in ipbt do sed -i '/set(DEFAULT_COMMIT/s/unavailable/$(vcsfullid)/' cmake/gitcommit.cmake

# Build the man page.
in . do mkdir build-doc
in build-doc do cmake ../ipbt/doc
in build-doc do make -j$(nproc)
in build-doc do cp ipbt.1 ../ipbt

# Test-build the program itself, in STRICT mode. We should ensure this
# works before being willing to ship any tarball based on it.
in . do mkdir build-test
in build-test do cmake ../ipbt -DSTRICT=ON
in build-test do make -j$(nproc)

# Build a source archive with the right name.
in . do ln -s ipbt ipbt-$(Version)
in . do tar chzvf ipbt-$(Version).tar.gz ipbt-$(Version)

# Build the web version of the man page. This is fiddly because
# man-ipbt.but is organised as a bunch of \S within a \H, matching the
# PuTTY man pages in order to minimise the diff between the repos. But
# for this purpose we need to turn the \S into \title, and turn all
# the \H into \U.
in ipbt/doc do perl -l12 -pe 's[^\\H\{.*?\}][\\title];s[^\\S\{.*?\}][\\U]' man-ipbt.but | halibut --html=ipbt.html -

deliver ipbt-$(Version).tar.gz $@
deliver ipbt/doc/ipbt.html $@
