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

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

# 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-indep: build-indep-stamp
build-indep-stamp: config.status
ifneq (,$(BUILD_INDEP))
	dh_testdir
	$(MAKE) $(DEB_MAKE_INDEP_TARGETS)
	touch $@
endif

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

build: build-indep build-arch

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-indep
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_gencontrol_substvars -i
	dh_md5sums -i
	dh_builddeb -i
endif

binary-arch: build-arch
	dh_testdir
	dh_prep -a
	dh_install_needed -a
	dh_strip -a
	dh_compress -a
	dh_fixperms -a
	dh_makeshlibs -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol_substvars -a
	dh_md5sums -a
	dh_builddeb -a

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