#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

%:
	dh $@ --with autoreconf

override_dh_clean:
	dh_clean
	-rm debian/bgpdump.1
	-rm -rf debian/tests/test_out/

override_dh_auto_clean:
	# some extra loops for cleanup *after* build
	if [ -d m4 ] ; then \
		cp -v debian/Makefile.am debian/configure.ac . ; \
	fi
	dh_auto_clean
	-rm Makefile.am configure.ac

override_dh_autoreconf_clean:
	dh_autoreconf_clean
	# restore upstream files
	for file in Makefile.in configure.in ; do \
		if [ -f $$file.orig ] ; then \
			mv -v $$file.orig $$file ; \
		fi ; \
	done

override_dh_update_autotools_config:
	cp -v debian/Makefile.am debian/configure.ac .
	mkdir -p m4
	# move upstream files out of the way
	for file in Makefile.in configure.in ; do \
		if [ -f $$file ] && [ ! -f $$file.orig ] ; then \
			mv -v $$file $$file.orig ; \
		fi ; \
	done
	dh_update_autotools_config

override_dh_auto_configure:
	dh_auto_configure
	VERSION_AC="$$(awk '($$2=="VERSION"){print $$3}' bgpdump-config.h | cut -d'"' -f2)" ; \
	VERSION_DEB="$$(dpkg-parsechangelog -SVersion | cut -d- -f1)" ; \
	if [ "$$VERSION_AC" != "$$VERSION_DEB" ] ; then \
		echo "Mismatch! Debian: $$VERSION_DEB, AC: $$VERSION_AC" ; \
		echo "Fix debian/configure.ac to match debian/changelog" ; \
		false ; \
	fi

override_dh_install:
	dh_install
	# don't ship the library for the time being, see README.Debian
	rm -rf debian/bgpdump/usr/lib/
	rm -rf debian/bgpdump/usr/include/

override_dh_installman:
	a2x --format=manpage --no-xmllint debian/bgpdump.txt
	dh_installman
