#!/usr/bin/make -f

#export DH_VERBOSE=1

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

# Can likely be dropped when updating to >= 4.3.0
export DEB_CFLAGS_MAINT_APPEND = -std=gnu17

%:
	dh $@

override_dh_auto_configure:
	# upstream build system regenerates this from a .in, to embed the httpd version number
	# keep a copy before building, and restore it afterwards, to ensure a clean build tree
	# upstream bug #39
	[ ! -f contrib/ipv6calc.spec.bak ] && mv contrib/ipv6calc.spec contrib/ipv6calc.spec.bak || true
	dh_auto_configure -- \
		--enable-mmdb

override_dh_auto_build:
	# use the system copies of the IEEE data to generate header files etc.
	debian/use-system-ieee-data.sh regenerate
	# some hardening flags (such as fortify) are in CPPFLAGS, but
	# upstream's Makefiles do not currently employ CPPFLAGS at all, so
	# workaround this by passing them to DEFAULT_CFLAGS. Upstream bug #37.
	dh_auto_build -- DEFAULT_CFLAGS="$(CPPFLAGS)"

override_dh_auto_test:
	# only run ipv6calc's test suite; the ipv6log* binaries error out when uid == 0
	# and even though we do not require root to build (see Rules-Requires-Root),
	# we do need to support building as root
	cd ipv6calc && make test

execute_after_dh_auto_install:
	# these are really source-level tools, generating headers etc. Upstream bug #38.
	rm -rf debian/ipv6calc/usr/share/ipv6calc/tools/
	rmdir -p --ignore-fail-on-non-empty debian/ipv6calc/usr/share/ipv6calc

execute_after_dh_auto_clean:
	[ -f contrib/ipv6calc.spec.bak ] && mv contrib/ipv6calc.spec.bak contrib/ipv6calc.spec || true
	# restore pregenerated header files to their pristine condition
	debian/use-system-ieee-data.sh restore
