#!/usr/bin/make -f

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

PYVERS=$(shell pyversions -vr)

DEBIAN_DIR := $(shell echo ${MAKEFILE_LIST} | awk '{print $$1}' | xargs dirname )

get-orig-source:
	@@dh_testdir
	@@cd ${DEBIAN_DIR}/.. && \
	version=$$(uscan --force-download --dehs | \
		sed -n 's/.*<upstream-version>\(.*\)<\/upstream-version>.*/\1/p') && \
		bzcat ../concordance-$${version}.tar.bz2 | gzip -9fn -c - > \
		${CURDIR}/${DEB_SOURCE_PACKAGE}_$${version}.orig.tar.gz

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp build-ext-* configure-stamp
	( cd libconcord/ && [ ! -f Makefile ] || $(MAKE) clean )
	( cd concordance/ && [ ! -f Makefile ] || $(MAKE) clean )
	find . -name *\.py[co] -exec rm {} \;
	rm -f patch-stamp
	dh_quilt_unpatch
	dh_clean

patch: patch-stamp
patch-stamp:
	dh_testdir
	dh_quilt_patch
	touch $@

configure: configure-stamp
configure-stamp: patch
	dh_testdir
	( cd libconcord/ && ./configure --prefix=/usr --sysconfdir=/etc )
	( cd concordance/ && CFLAGS=" -L../libconcord " CPPFLAGS=" -I../libconcord " ./configure --prefix=/usr )
	touch $@

build: build-stamp $(PYVERS:%=build-ext-%)
build-stamp: configure
	dh_testdir
	( cd libconcord/ && $(MAKE) )
	( cd libconcord/ && $(MAKE) udev )
	( cd concordance/ && $(MAKE) )
	touch $@
build-ext-%:
	( cd libconcord/bindings/python && python$* ./setup.py build )
	touch $@

install: install-stamp $(PYVERS:%=install-ext-%)
install-stamp: build
	dh_testdir
	dh_testroot
	dh_clean -k
	( cd libconcord/ && $(MAKE) DESTDIR=$(CURDIR)/debian/tmp install )
	( cd libconcord/ && $(MAKE) DESTDIR=$(CURDIR)/debian/tmp install_udev )
	mv $(CURDIR)/debian/tmp/etc/udev/rules.d/99-libconcord.rules $(CURDIR)/debian/libconcord1.udev
	( cd concordance/ && $(MAKE) DESTDIR=$(CURDIR)/debian/concordance install )
	touch $@
install-ext-%:
	( cd libconcord/bindings/python && python$* ./setup.py install --root=$(CURDIR)/debian/python-libconcord )

binary-indep: build install
	dh_testdir -i
	dh_testroot -i
	dh_installchangelogs -i Changelog
	dh_installdocs -i
	dh_pycentral -ppython-libconcord
	dh_compress -i -X.py
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

binary-arch: build install
	dh_testdir -a
	dh_testroot -a
	dh_installchangelogs -a Changelog
	dh_installdocs -a
	dh_install -a
	dh_installman -a
	dh_link -a
	dh_strip -a
	dh_compress -a -X.py
	dh_fixperms -a
	dh_makeshlibs -a
	dh_installudev -plibconcord1
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary: binary-indep binary-arch

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