#!/usr/bin/make -f

export LIBTOOLIZE = libtoolize --install
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

buildflags = --build=$(DEB_BUILD_GNU_TYPE)
ifneq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
  buildflags += --host=$(DEB_HOST_GNU_TYPE)
endif

configure: configure-stamp
configure-stamp:
	dh_testdir
	[ -f debian/autoreconf.before ] || dh_autoreconf
	./configure $(buildflags) \
		--prefix=/usr \
	        --without-zlib \
		--mandir=\$${prefix}/share/man \
		$(shell dpkg-buildflags --export=configure)
	touch configure-stamp

build-arch: build
build-indep:
build: build-stamp
build-stamp: configure-stamp 
	dh_testdir
	$(MAKE)
	touch build-stamp

ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	$(MAKE) check
endif

clean:
	dh_testdir
	dh_testroot
	# Stale build files
	[ ! -f Makefile ] || $(MAKE) distclean
	-rm -f build-stamp configure-stamp
	dh_autoreconf_clean
	dh_clean libtool configure

install: build-stamp
	dh_testdir
	dh_testroot
	dh_prep

	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp

binary-indep:

binary-arch: install
	dh_testdir
	dh_testroot
	dh_install --sourcedir=debian/tmp

	dh_installdocs
	dh_installexamples
	dh_installman 
	dh_installchangelogs ChangeLog
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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