#!/usr/bin/make -f

# Allow to find debhelper7 on Maemo
# Install from Extras repository
PATH := /usr/bin/dh7:$(PATH)
export PATH

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

LDFLAGS="-Wl,--reduce-memory-overheads"

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

#### CONFIGURE ####

override_dh_auto_configure: $(PYVERSIONS:%=override_dh_auto_configure-%)
#$(PYVERSIONS:%=override_dh_auto_configure_dbg-%) 

#override_dh_auto_configure_dbg-%:
#	mkdir -p build-$*-dbg
#	dh_auto_configure --builddirectory=build-$*-dbg -- \
#	-DCMAKE_VERBOSE_MAKEFILE=OFF \
#	-DENABLE_MAEMO=1 \
#	-DCMAKE_BUILD_TYPE:STRING="Debug" \
#	-DCMAKE_SKIP_RPATH=true \
#	-DCMAKE_USE_PYTHON_VERSION="$*-dbg;$*_d;$*" \
#	-DCMAKE_DEBUG_POSTFIX=_d \
#	-DLIBPYSIDE_POSTFIX=$(shell echo -py$* | sed -e 's/\.//g' -) \
#	-DSHIBOKEN_POSTFIX=$(shell echo -py$*_d | sed -e 's/\.//g' -)

override_dh_auto_configure-%:
	mkdir -p build-$*
	dh_auto_configure --builddirectory=build-$* -- \
	-DCMAKE_VERBOSE_MAKEFILE=OFF \
	-DENABLE_MAEMO=1 \
	-DCMAKE_BUILD_TYPE:STRING="Release" \
	-DCMAKE_SKIP_RPATH=true \
	-DCMAKE_USE_PYTHON_VERSION="$*" \
	-DLIBPYSIDE_POSTFIX=$(shell echo -py$* | sed -e 's/\.//g' -) \
	-DSHIBOKEN_POSTFIX=$(shell echo -py$* | sed -e 's/\.//g' -)

#### BUILD ####

override_dh_auto_build: $(PYVERSIONS:%=override_dh_auto_build-%)
# $(PYVERSIONS:%=override_dh_auto_build_dbg-%)

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

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

#### INSTALL ####

override_dh_auto_install: $(PYVERSIONS:%=override_dh_auto_install-%)
# $(PYVERSIONS:%=override_dh_auto_install_dbg-%)

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

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

#### STRIP ####
#override_dh_strip:
#	dh_strip --dbg-package=python-pyside-dbg --exclude=_d.so

#### TEST ####
#override_dh_auto_test: $(PYVERSIONS:%=override_dh_auto_test_dbg-%) $(PYVERSIONS:%=override_dh_auto_test-%)

#override_dh_auto_test_dbg-%:
#	# Run tests in fake X Server in a non-blocking way
#	- xvfb-run -a -s -noreset dh_auto_test --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 --builddirectory=build-$*

#### 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_1

override_dh_install_1:
	# Move the debug .so's right in place
	#$(shell rename 's/(debian\/tmp-dbg\/)(.*)\.so(.*)$$/debian\/tmp\/$$2.so$$3/'\
	#             debian/tmp-dbg/usr/lib/python*/*-packages/PySide/*.so)
	#$(shell rename 's/(debian\/tmp-dbg\/)(.*)\.so(.*)$$/debian\/tmp\/$$2.so$$3/'\
	#             debian/tmp-dbg/usr/lib/*.so*)
	# Setup the default version symbolic links
	$(shell ln -s libpyside`echo -py${DEFAULT_PY} | sed -e 's/\.//g' -`.so.0.4 \
	             debian/tmp/usr/lib/libpyside0.4.so)
	$(shell ln -s libpyside0.4.so debian/tmp/usr/lib/libpyside.so)

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

#override_dh_install_3: override_dh_install_2
	# Move the debug library to the correct package.
#	$(shell rename 's/(debian\/libpyside0.4\/)(.*)_d\.so(.*)$$/debian\/python-pyside-dbg\/$$2_d.so$$3/' \
#	              debian/libpyside0.4/usr/lib/*_d.so*)

override_dh_gencontrol:
	debian/dh_pycmakedeps
	# this option doesn't work into Maemo5
	#--modules-root=PySide/
	dh_gencontrol
