#!/usr/bin/make -f

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

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

#### 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 -- \
	-DCMAKE_BUILD_TYPE:STRING="Debug" \
	-DCMAKE_SKIP_RPATH=true \
	-DCMAKE_USE_PYTHON_VERSION="$*-dbg;$*_d;$*" \
	-DCMAKE_DEBUG_POSTFIX=$(shell echo -py$*_d | sed -e 's/\.//g' -)

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

#### 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-$*

override_dh_compress:
	dh_compress -X.js

#### 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=shiboken-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-%: prepare_debug_test-%
	- dh_auto_test --builddirectory=build-$*-dbg

override_dh_auto_test-%: prepare_test-%
	dh_auto_test --builddirectory=build-$*

prepare_debug_test-%:
	# Prepare the convenience symlinks for the tests
	$(shell ln -s libother`echo -py$* | sed -e 's/\.//g' -`_d.so \
	   build-$*-dbg/tests/libother/libother.so )
	$(shell ln -s libsample`echo -py$* | sed -e 's/\.//g' -`_d.so \
	   build-$*-dbg/tests/libsample/libsample.so )
	$(shell ln -s other`echo -py$* | sed -e 's/\.//g' -`_d.so \
	   build-$*-dbg/tests/otherbinding/other.so )
	$(shell ln -s sample`echo -py$* | sed -e 's/\.//g' -`_d.so \
	   build-$*-dbg/tests/samplebinding/sample.so )

prepare_test-%:
	# Prepare the convenience symlinks for the tests
	$(shell ln -s libother`echo -py$* | sed -e 's/\.//g' -`.so \
	   build-$*/tests/libother/libother.so )
	$(shell ln -s libsample`echo -py$* | sed -e 's/\.//g' -`.so \
	   build-$*/tests/libsample/libsample.so )
	$(shell ln -s other`echo -py$* | sed -e 's/\.//g' -`.so \
	   build-$*/tests/otherbinding/other.so )
	$(shell ln -s sample`echo -py$* | sed -e 's/\.//g' -`.so \
	   build-$*/tests/samplebinding/sample.so )

#### CLEAN ####
override_dh_auto_clean:
	rm -rf build-*
	rm -rf debian/tmp-dbg

#### DEBIAN INSTALL ####

override_dh_install: override_dh_install_done

override_dh_install_real:
	# Install the debug variant
	dh_install --sourcedir=debian/tmp-dbg -pshiboken-dbg
	
	# Install the other variants
	dh_install --list-missing --remaining-packages

override_dh_install_done: override_dh_install_real
	# Setup the default version symbolic links
	$(shell ln -sf shiboken_generator`echo -py${DEFAULT_PY} | sed -e 's/\.//g' -`.so \
	               debian/shiboken/usr/lib/generatorrunner/shiboken_generator.so )
	$(shell ln -sf libshiboken`echo -py${DEFAULT_PY} | sed -e 's/\.//g' -`.so \
	               debian/libshiboken-dev/usr/lib/libshiboken.so )
	# Setup the debug default version symbolic links
	$(shell ln -sf shiboken_generator`echo -py${DEFAULT_PY} | sed -e 's/\.//g' -`_d.so \
	               debian/shiboken-dbg/usr/lib/generatorrunner/shiboken_generator_d.so )
	$(shell ln -sf libshiboken`echo -py${DEFAULT_PY} | sed -e 's/\.//g' -`_d.so \
	               debian/shiboken-dbg/usr/lib/libshiboken_d.so )

#### Shared libs ####
override_dh_makeshlibs:
	# Package shiboken ships a usr/lib/generatorrunner/*.so plugin. No shared libs in there.
	dh_makeshlibs -Nshiboken

#### Docs ####
binary: binary-arch binary-indep;

binary-indep: binary-arch
	dh binary-indep --until dh_auto_build
	dh_auto_build -i --builddirectory=build-$(DEFAULT_PY) -- doc
	dh binary-indep --after dh_auto_build
