#!/usr/bin/make -f

DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)

confflags = --prefix=/usr

ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
    confflags += --build $(DEB_HOST_GNU_TYPE)
else
    confflags += --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
endif

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
    makeflags = -j$(NUMJOBS)
endif

# build* targets
# ==============

.PHONY: build build-arch build-indep
build build-arch build-indep: build-stamp

config.status:
	./configure $(confflags)

build-stamp: config.status
	$(MAKE) $(makeflags)
	touch $(@)

# binary* targets
# ===============

install-stamp: build-stamp
	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp/
	touch $(@)

.PHONY: binary
binary: binary-arch binary-indep

.PHONY: binary-indep
binary-indep: install-stamp
	dh_testroot -i
	dh_prep -i -X debian/tmp
	dh_install -p python-poliqarp python/poliqarp/ usr/lib/$(shell pyversions -d)/site-packages/
	dh_install -i
	cat debian/poliqarp-launcher.sh > debian/poliqarp-gui/usr/bin/poliqarp
	dh_installdocs -i
	dh_installchangelogs -i
	dh_installexamples -i
	dh_installman -i
	dh_pysupport -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

.PHONY: binary-arch
binary-arch: install-stamp
	dh_testroot -a
	dh_prep -a -X debian/tmp
	dh_install -a
	dh_installdocs -a
	dh_installchangelogs -a
	dh_installexamples -a
	dh_installman -a
	dh_pysupport -a
	dh_compress -a
	dh_fixperms -a
	dh_strip -a
	dh_shlibdeps -a
	dh_installdeb -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

# clean target
# ============

.PHONY: clean
clean:
	dh_testdir
	! [ -f Makefile ] || $(MAKE) clean
	dh_clean

# vim:ts=4 sw=4 noet
