#!/usr/bin/make -f

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

# aclocal (called from autoreconf) finds gettext *.m4 files
GETTEXT_TOP_FILES=	ABOUT-NLS config.rpath
GETTEXT_PO_FILES=	Makefile.in.in Rules-quot boldquot.sed \
			en@boldquot.header en@quot.header insert-header.sin \
			quot.sed remove-potcdate.sin
BUILD_DATE=$(shell dpkg-parsechangelog --show-field Date)

%:
	dh $@

override_dh_auto_configure:
	# Move everything we don't need away
	tar cvf debian/not-needed-for-build.tar \
		intl ABOUT-NLS po/Makefile.in.in aclocal.m4 config.guess config.sub \
		config.h.in configure depcomp install-sh ltmain.sh missing \
		mkinstalldirs src/ltdl.* `find . -name Makefile.in` pcre
	# Use external PCRE and libtool only
	rm -rf src/ltdl.* ltmain.sh pcre

	test -d m4 || mkdir m4
	for f in ${GETTEXT_TOP_FILES}; do \
		cp -f /usr/share/gettext/$$f .; \
	done
	for f in ${GETTEXT_PO_FILES}; do \
		cp -f /usr/share/gettext/po/$$f po/; \
	done

	dh_autoreconf

	dh_auto_configure -- \
	    --without-included-ltdl \
	    --with-modules='intl:gettext' \
	    --with-tidy=/usr/bin/tidy

override_dh_auto_build:
	# Fake consistent user and enforce all three modification times
	faketime "$(BUILD_DATE)" fakeroot sh -c \
	  'cp doc/mp4h.mp4h doc/mp4h.mp4h.tmp && rm doc/mp4h.mp4h && mv doc/mp4h.mp4h.tmp doc/mp4h.mp4h && dh_auto_build'

override_dh_auto_clean:
	-tar cvf debian/needed-after-clean.tar ABOUT-NLS po/Makefile.in.in
	dh_autoreconf_clean
	# PO stuff
	cd po && rm -f ${GETTEXT_PO_FILES} stamp-po POTFILES Makefile.in
	rm -f ${GETTEXT_TOP_FILES}
	# Restore as much as possible for easy VCS usage
	[ ! -f debian/needed-after-clean.tar ] || tar xvf debian/needed-after-clean.tar
	[ ! -f debian/not-needed-for-build.tar ] || tar xvf debian/not-needed-for-build.tar
	find . -name .deps -o -name .libs | xargs -r rm -rv
	find . -not -path './debian/*' \
	       '(' -name '*.[ao13]' \
	        -o -name '*.l[ao]' \
	        -o -name Makefile ')' \
	       -delete

override_dh_auto_install:
	dh_auto_install
	sed -e "/dependency_libs/ s/'.*'/''/" \
	    -i "`find $(CURDIR)/debian/mp4h/usr/lib -name gettext.la`"
	-find $(CURDIR)/debian/mp4h -type d -empty -delete
