#!/usr/bin/make -f
# common debian/rules for autotool based packages

BUILD_INDEP := $(shell dh_listpackages -i 2> /dev/null)
DH_COMPAT := 8

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)

configure.ac:
	dh_testdir
	/usr/share/build-rules-autotool/configure.ac.gen > $@

configure: configure.ac
	dh_testdir
	[ -e AUTHORS ] || touch AUTHORS
	[ -e ChangeLog ] || ln -s debian/changelog ChangeLog
	[ -e COPYING ] || ln -s debian/copyright COPYING
	[ -e NEWS ] || touch NEWS
	[ -e README ] || touch README
	echo $(DH_COMPAT) > debian/compat
	autoreconf -is

config.status: configure
	dh_testdir
	./configure --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) \
		--prefix=/usr

build: build-stamp
build-stamp: config.status
	dh_testdir
	$(MAKE) $(DEB_BUILD_FLAVOURS)
	touch $@

clean:
	dh_testdir
	[ ! -f Makefile ] || $(MAKE) distclean
	DH_COMPAT=$(DH_COMPAT) dh_clean
	rm -f aclocal.m4 compile config.guess config.h.in config.log \
		config.sub configure depcomp install-sh ltmain.sh missing NEWS \
		debian/compat
	[ -s AUTHORS ] || rm -f AUTHORS
	[ ! -L ChangeLog ] || rm -f ChangeLog
	[ ! -L COPYING ] || rm -f COPYING
	[ ! -L INSTALL ] || rm -f INSTALL
	[ -s README ] || rm -f README
	find -name Makefile.in -delete
	if [ "$$(head -n 1 configure.ac 2> /dev/null)" = \
		"# Generated by build-rules-autotool." ] ; then \
		rm configure.ac ; fi

binary-indep: build
ifneq (,$(BUILD_INDEP))
	dh_testdir
	dh_prep -i
	dh_install_needed -i
	dh_strip -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_shlibdeps -i
	dh_multiarchdeps
	dh_gencontrol_substvars -i
	dh_md5sums -i
	dh_builddeb -i
endif

ifeq (,$(DEB_BUILD_FLAVOURS))
BINARY_ARCH_DH_ARGS := -a
else
BINARY_ARCH_DH_ARGS := $(patsubst %,-p%,$(BINARY_ARCH_PKGS))
endif

binary-arch: build
	dh_testdir
	dh_prep $(BINARY_ARCH_DH_ARGS)
	dh_install_needed $(BINARY_ARCH_DH_ARGS)
	dh_strip $(BINARY_ARCH_DH_ARGS)
	dh_compress $(BINARY_ARCH_DH_ARGS)
	dh_fixperms $(BINARY_ARCH_DH_ARGS)
	dh_makeshlibs $(BINARY_ARCH_DH_ARGS)
	dh_installdeb $(BINARY_ARCH_DH_ARGS)
	dh_shlibdeps $(BINARY_ARCH_DH_ARGS)
	dh_gencontrol_substvars $(BINARY_ARCH_DH_ARGS)
	dh_md5sums $(BINARY_ARCH_DH_ARGS)
	dh_builddeb $(BINARY_ARCH_DH_ARGS)

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary
