#!/usr/bin/make -f

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

DEB_DH_BUILDDEB_ARGS += -- -Z$(shell dpkg-deb --help | grep -q ":.* xz[,.]" \
                               && echo xz || echo bzip2)

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

CFLAGS = -Wall -g
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif

configure: configure-stamp
configure-stamp:
	dh_testdir
	
	CFLAGS="$(CFLAGS)" ./configure --host=$(DEB_HOST_GNU_TYPE) \
		--build=$(DEB_BUILD_GNU_TYPE) --prefix=/usr --mandir=\$${prefix}/share/man \
		--enable-gtk-doc

	touch configure-stamp

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

clean: clean-patched
clean-patched:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp

	[ ! -f Makefile ] || $(MAKE) distclean
	
	dh_clean 

install: build-stamp install-arch install-indep

install-arch:
	dh_testdir
	dh_testroot
	dh_clean -k 
	dh_installdirs -a

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

	# we don't those binaries
	cd $(CURDIR)/examples && make clean
	
	dh_install -a --sourcedir=debian/tmp
	
	find debian/python-beagle -name '*.a' -delete
	find debian/python-beagle debian/libbeagle-dev -name '*.la' -delete

# Install link from gtk-doc to /usr/share/doc
	mkdir -p debian/libbeagle-dev/usr/share/doc/libbeagle-dev/
	ln -s ../../gtk-doc/html/beagle/ debian/libbeagle-dev/usr/share/doc/libbeagle-dev/html

install-indep:

binary-indep: build install
	
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs -a ChangeLog
	dh_installdocs -a -X .deps/
	dh_installexamples -a
	dh_installmenu -a
	dh_installman -a
	dh_makeshlibs -p libbeagle1 -V
	dh_pysupport -p python-beagle
	dh_link -a
	dh_strip -a --dbg-package=libbeagle-dbg
	dh_compress -a -X examples/
	dh_fixperms -a
	dh_installdeb -a
	dh_shlibdeps -a -L libbeagle1 -l debian/libbeagle1/usr/lib
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a $(DEB_DH_BUILDDEB_ARGS)

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