#!/usr/bin/make -f

.SILENT:

# Set $(DESTDIR) (on command line preferably) if you want to see the
# .deb packages in another directory than `..'.
DESTDIR := ..

# DEB_BUILD_OPTIONS parsing
# XXX: The latest Debian policy says that DBO should be whitespace-separated,
#      but our automated builders might give a comma-separated list.
#      Therefore we replace all commas with spaces (which needs a bit of
#      make trickery).  NOTE: that this will break when they add options
#      with multiple, comma-separated values...

comma    := ,
empty    :=
space    := $(empty) $(empty)
DBO      := $(subst $(comma),$(space),$(DEB_BUILD_OPTIONS))

CFLAGS   :=
CFG_OPTS := --disable-dependency-tracking --prefix=/usr --enable-static
CFLAGS   += $(if $(filter noopt,$(DBO)),-O0,-O2)
CFG_OPTS += $(if $(filter debug,$(DBO)),--enable-debug,--disable-debug)
CFLAGS   += $(if $(filter thumb,$(DBO)),-mthumb)
CFG_OPTS += $(if $(filter nocheck,$(DBO)),--disable-tests,--enable-tests)
CFG_OPTS += $(if $(filter nodoc,$(DBO)),--disable-gtk-doc,--enable-gtk-doc)
CFG_OPTS += $(if $(filter lcov,$(DBO)),--enable-coverage)
# `nostrip' is handled by dh_strip.

# Do nothing by default.
all:;

configure: debian/stamp-autogen
debian/stamp-autogen:
	NOCONFIGURE=1 ./autogen.sh
	./configure $(CFG_OPTS) CFLAGS="$(CFLAGS)"
	touch $@

config.status: configure

build: debian/stamp-build
debian/stamp-build: config.status
	dh_testdir
	$(MAKE) all
	$(MAKE) check
	touch $@

clean:
	dh_testdir
	dh_testroot
	-rm -f debian/stamp-*
	[ ! -f Makefile ] || $(MAKE) distclean
	dh_clean

install: debian/stamp-install
debian/stamp-install: debian/stamp-build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs
	$(MAKE) install DESTDIR="$(CURDIR)/debian/tmp"
	touch $@

binary-indep: build install
	dh_testdir
	dh_testroot
	dh_install -i --sourcedir=debian/tmp
	dh_installchangelogs -i
	dh_installdocs -i
	dh_compress -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i --destdir="$(DESTDIR)"

binary-arch: build install
	dh_testdir
	dh_testroot
	dh_install -a --sourcedir=debian/tmp
	dh_installchangelogs -a
	dh_installdocs -a
	dh_installmime -a
	dh_strip --dbg-package=libmafw0-dbg
	dh_link -a
	dh_compress -a
	dh_fixperms -a
	dh_makeshlibs -a -V
	dh_shlibdeps -a
	dh_installdeb -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a --destdir="$(DESTDIR)"

binary: binary-indep binary-arch
	
distcheck: build
	dh_testdir
	$(MAKE) distcheck
	
vg:
	dh_testdir
	$(MAKE) -C "$(CURDIR)/tests" vg

.PHONY: all build clean binary-indep binary-arch binary-common binary install distcheck vg
