#!/usr/bin/make -f

# workaround to avoid calling Scratchbox python2.3
PATH := /usr/bin:$(PATH)
export PATH
SBOX_REDIRECT_IGNORE = /usr/bin/python
export SBOX_REDIRECT_IGNORE

# 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)
LIBPYSIDE_SOVERSION=1.0

#### CONFIGURE ####

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

override_dh_auto_configure_dbg-%:
	mkdir -p build-$*-dbg
	dh_auto_configure --builddirectory=build-$*-dbg -- \
	-DENABLE_MAEMO=1 \
	-DCMAKE_VERBOSE_MAKEFILE=OFF \
	-DCMAKE_BUILD_TYPE:STRING="Debug" \
	-DCMAKE_SKIP_RPATH=true \
	-DCMAKE_USE_PYTHON_VERSION="$*-dbg;$*_d;$*" \
	-DUSE_XVFB=true \
	-DPYTHON_BASENAME=python$*-dbg

override_dh_auto_configure-%:
	mkdir -p build-$*
	dh_auto_configure --builddirectory=build-$* -- \
	-DENABLE_MAEMO=1 \
	-DCMAKE_VERBOSE_MAKEFILE=OFF \
	-DCMAKE_BUILD_TYPE:STRING="Release" \
	-DCMAKE_SKIP_RPATH=true \
	-DCMAKE_USE_PYTHON_VERSION="$*" \
	-DUSE_XVFB=true \
	-DPYTHON_BASENAME=python$* \
	-DDEFAULT_PYTHON_BASENAME=python${DEFAULT_PY}

#### BUILD ####

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

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_dbg-%) $(PYVERSIONS:%=override_dh_auto_install-%)

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=-dbg.so --exclude=_d.so

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

override_dh_auto_test_dbg-%:
	# Don't be blocking.
	- dh_auto_test --builddirectory=build-$*-dbg

override_dh_auto_test-%:
	# Don't be blocking.
	- 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
	rm -f debian/libpyside${LIBPYSIDE_SOVERSION}.symbols
	rm -f debian/libpyside${LIBPYSIDE_SOVERSION}.symbols-py*

#### DEBIAN INSTALL ####

override_dh_install: override_dh_install_3

override_dh_install_1:
	# 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/PySide/*.so)
	$(shell for i in debian/tmp-dbg/usr/lib/python*/*-packages/PySide/*.so; do mv $$i $${i/.so/_d.so}; done)
	$(shell mv debian/tmp-dbg/usr/lib/python*/*-packages/PySide/*_d.so \
			debian/tmp/usr/lib/python*/*-packages/PySide/)


#	$(shell rename 's/(debian\/tmp-dbg\/)(.*)\.so(.*)$$/debian\/tmp\/$$2.so$$3/'\
#	             debian/tmp-dbg/usr/lib/*.so*)
	$(shell mv debian/tmp-dbg/usr/lib/*.so* debian/tmp/usr/lib)

	# Setup the default version symbolic links
	$(shell ln -s libpyside-python${DEFAULT_PY}.so.${LIBPYSIDE_SOVERSION} \
	             debian/tmp/usr/lib/libpyside${LIBPYSIDE_SOVERSION}.so)
	$(shell ln -s libpyside${LIBPYSIDE_SOVERSION}.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\/libpyside${LIBPYSIDE_SOVERSION}\/)(.*)-dbg\.so(.*)$$/debian\/python-pyside-dbg\/$$2-dbg.so$$3/' \
#	              debian/libpyside${LIBPYSIDE_SOVERSION}/usr/lib/*-dbg.so*)
	$(shell mv debian/libpyside${LIBPYSIDE_SOVERSION}/usr/lib/*-dbg.so* debian/python-pyside-dbg/usr/lib)

	# Move the debug cmake files to the correct package
	#
#	$(shell rename 's/(debian\/libpyside-dev\/)(.*)-dbg\.cmake/debian\/python-pyside-dbg\/$$2-dbg.cmake/'\
#	              debian/tmp-dbg/usr/lib/cmake/PySide-*/*-dbg.cmake)
	$(shell mkdir -p debian/python-pyside-dbg/usr/lib/cmake/PySide-1.0.0)
	$(shell mv debian/tmp-dbg/usr/lib/cmake/PySide-1.0.0/*-dbg.cmake debian/python-pyside-dbg/usr/lib/cmake/PySide-1.0.0)

override_dh_gencontrol:
	debian/dh_pycmakedeps --modules-root=PySide/
	# Force versioning for libpyside1.0 and libshiboken1.0 dependencies in PySide modules
	# The command below uses info from debian/shlibs.local, and will force dpkg to pull
	# updated versions of the libs above. This is not a ideal solution, but for the
	# time being it will do
	dh_shlibdeps debian/tmp/usr/lib/python${DEFAULT_PY}/site-packages/PySide/*.so
	dh_gencontrol
