#!/usr/bin/make -f

%:
	dh $@ --with python2 --buildsystem=cmake --parallel

# Python versions
PYVERSIONS=$(shell pyversions -v -r)
DEFAULT_PY=$(shell pyversions -v -d)

ifneq (,$(filter debug,$(DEB_BUILD_OPTIONS)))
	BD_DEBUG           = python-all-dbg (>= 2.6.6-3~), shiboken-dbg (>= 1.0.10), python-pyside-dbg (>= 1.0.9),
endif

ifneq (,$(filter python-pyside-dbg,$(shell dh_listpackages)))
	BUILD_DEBUG = true
	CONFIGURE2_TARGETS = $(PYVERSIONS:%=override_dh_auto_configure_dbg-%)  $(PYVERSIONS:%=override_dh_auto_configure-%)
	BUILD2_TARGETS     = $(PYVERSIONS:%=override_dh_auto_build_dbg-%)  $(PYVERSIONS:%=override_dh_auto_build-%)
	INSTALL2_TARGETS   = $(PYVERSIONS:%=override_dh_auto_install_dbg-%)    $(PYVERSIONS:%=override_dh_auto_install-%)
	TEST2_TARGETS      = $(PYVERSIONS:%=override_dh_auto_test_dbg-%)  $(PYVERSIONS:%=override_dh_auto_test-%)
	
	STRIP2_PARAMETERS  = --dbg-package=python-qtmobility-dbg --exclude=-dbg.so --exclude=_d.so
else
	CONFIGURE2_TARGETS = $(PYVERSIONS:%=override_dh_auto_configure-%)
	BUILD2_TARGETS     = $(PYVERSIONS:%=override_dh_auto_build-%)
	INSTALL2_TARGETS   = $(PYVERSIONS:%=override_dh_auto_install-%)
	TEST2_TARGETS      = $(PYVERSIONS:%=override_dh_auto_test-%)
endif

update-control:
	sed -e 's/@BD-DEBUG@/$(BD_DEBUG)/g' debian/control.in > debian/control
ifdef BD_DEBUG
	cat debian/control.dbg-packages >> debian/control
endif

#### CONFIGURE ####

override_dh_auto_configure: $(CONFIGURE2_TARGETS)

override_dh_auto_configure_dbg-%:
	mkdir -p build-$*-dbg
	dh_auto_configure -a --builddirectory=build-$*-dbg -- \
	-DCMAKE_VERBOSE_MAKEFILE=ON \
	-DCMAKE_BUILD_TYPE:STRING="Debug" \
	-DCMAKE_SKIP_RPATH=true \
	-DCMAKE_USE_PYTHON_VERSION="$*-dbg;$*_d;$*" \
	-DPython_ADDITIONAL_VERSIONS="$*-dbg;$*_d;$*" \
	-DPYTHON_BASENAME=-python$*-dbg \
	-DPYTHON_SUFFIX=-python$*-dbg

override_dh_auto_configure-%:
	mkdir -p build-$*
	dh_auto_configure -a --builddirectory=build-$* -- \
	-DCMAKE_VERBOSE_MAKEFILE=ON \
	-DCMAKE_BUILD_TYPE:STRING="Release" \
	-DCMAKE_SKIP_RPATH=true \
	-DCMAKE_USE_PYTHON_VERSION=$* \
	-DPython_ADDITIONAL_VERSIONS=$* \
	-DPYTHON_BASENAME=-python$* \
	-DPYTHON_SUFFIX=-python$*

#### BUILD ####

override_dh_auto_build: $(BUILD2_TARGETS)

override_dh_auto_build_dbg-%:
	dh_auto_build -a --builddirectory=build-$*-dbg

override_dh_auto_build-%:
	dh_auto_build -a --builddirectory=build-$*

#### INSTALL ####

override_dh_auto_install: $(INSTALL2_TARGETS)

override_dh_auto_install_dbg-%:
	dh_auto_install -a --builddirectory=build-$*-dbg --destdir=debian/tmp-dbg

override_dh_auto_install-%:
	dh_auto_install -a --builddirectory=build-$*

#### STRIP ####
override_dh_strip:
	dh_strip -a $(STRIP2_PARAMETERS)

#### TEST ####
override_dh_auto_test: $(TEST2_TARGETS)

override_dh_auto_test_dbg-%:
	# Run tests in fake X Server in a non-blocking way
	- xvfb-run -a -s -noreset dh_auto_test -a --builddirectory=build-$*-dbg

override_dh_auto_test-%:
	# Run tests in fake X Server in a non-blocking way
	- xvfb-run -a -s -noreset dh_auto_test -a --builddirectory=build-$*

#### usr/share/doc/* symlinks ####
override_dh_link:
	# Replace doc directories by symlinks
	for p in `dh_listpackages -a -Npython-qtmobility-common`; do \
		rm -Rf debian/$$p/usr/share/doc/$$p; \
		ln -sf python-qtmobility-common debian/$$p/usr/share/doc/$$p ; \
	done
	dh_link

#### CLEAN ####
override_dh_auto_clean:
	rm -rf build-*
	cd tests; rm -Rf *.pyc */*.pyc */*/*.pyc */*/*/*.pyc
	- rm -rf debian/tmp-dbg

#### DEBIAN INSTALL ####

override_dh_install: override_dh_install_2

override_dh_install_1:
ifneq (,$(filter python-qtmobility-dbg, $(shell dh_listpackages)))
	# Move the debug .so's right in place
	$(shell rename 's/(debian\/tmp-dbg\/)(.*)\.so(.*)$$/debian\/tmp\/$$2_d.so$$3/'\
	             debian/tmp-dbg/usr/lib/python*/*-packages/QtMobility/*.so)
endif

override_dh_install_2: override_dh_install_1
	# Do the legacy install for the rest
	dh_install -a --list-missing

override_dh_gencontrol:
	debian/dh_pycmakedeps --modules-root=QtMobility/
	dh_gencontrol

override_dh_builddeb:
ifdef BUILD_DEBUG
	# XZ-compress the huge python-qtmobility-dbg
	dh_builddeb -ppython-qtmobility-dbg -- -Zxz
endif
	dh_builddeb --remaining-packages
