#!/usr/bin/make -f

DPKG_EXPORT_BUILDFLAGS = 1
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
include /usr/share/dpkg/default.mk
export MAN_PAGE_DATE ?= $(shell LC_ALL=C date -u -d"@$(SOURCE_DATE_EPOCH)" "+%d %b %Y")

%:
	dh $@

override_dh_autoreconf:
	## Instead of repackaging the upstream source, we remove the files included
	## from gnulib before the build.
	rm -rf doc/complexity.info doc/gendocs_template doc/gendocs_template_min doc/gpl-3.0.texi m4/ lib/
	## Regenerate src/opts.[ch] from src/opts.def - current upstream source uses
	## newer autogen version than the system one
	cd src && autogen opts.def
	## This is unfortunate, but we are forced to invoke gnulib-tool manually
	## and list modules here, since bootstrap script assumes presence of git
	## and network connection, and policy is to avoid bundled copies of
	## another project code.
	## Invoke gnulib-tool to set up m4/ and lib/ infrastructure.
	## Only doc-helper modules are imported; all C portability modules
	## (close, malloc-posix, snprintf, stdbool) are provided by modern glibc.
	gnulib-tool --import gendocs gpl-3.0
	## liboptschk.m4 was bundled m4/ alongside with gnulib macros,
	## but we dropped it. This way is even better, since we for sure get
	## the newest version of liboptschk.m4.
	ln -s /usr/share/autogen/liboptschk.m4 m4/
	dh_autoreconf

execute_after_dh_auto_build:
	## The autogen agman-cmd template emits ".it 1 an-trap" inside the .NOP
	## macro definition.  Modern groff no longer defines an-trap, which causes
	## "name 'an-trap' not defined" and "cannot nest .TP inside .TP" warnings.
	## Strip that line from the generated man page.
	sed -i '/^\.de1 NOP$$/,/^\.\.$$/{ /^\.  it 1 an-trap$$/d }' doc/complexity.1
