#!/usr/bin/make -f

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

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

# G++ versions
g++-version := $(shell readlink /usr/bin/g++ | sed -e 's/g++-\(.*\)\.\(.*\)/\1\2/')
g++-version-ge-4.5 := $(shell if test $(g++-version) -ge 45; then echo "yes"; else echo "no"; fi)

#### 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;$*" \

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=$* \
	-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-$*

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=-dbg.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-python$*-dbg.so \
	   build-$*-dbg/tests/libother/libother.so )
	$(shell ln -s libsample-python$*-dbg.so \
	   build-$*-dbg/tests/libsample/libsample.so )
	$(shell ln -s other-python$*-dbg.so \
	   build-$*-dbg/tests/otherbinding/other.so )
	$(shell ln -s sample-python$*-dbg.so \
	   build-$*-dbg/tests/samplebinding/sample.so )

prepare_test-%:
	# Prepare the convenience symlinks for the tests
	$(shell ln -s libother-python$*.so \
	   build-$*/tests/libother/libother.so )
	$(shell ln -s libsample-python$*.so \
	   build-$*/tests/libsample/libsample.so )
	$(shell ln -s other-python$*.so \
	   build-$*/tests/otherbinding/other.so )
	$(shell ln -s sample-python$*.so \
	   build-$*/tests/samplebinding/sample.so )

#### CLEAN ####
override_dh_auto_clean:
	rm -rf build-*
	rm -rf debian/tmp-dbg
	rm -f debian/libshiboken1.0.symbols
	rm -f debian/libshiboken1.0.symbols-py*

#### 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 libshiboken-python${DEFAULT_PY}.so \
	               debian/libshiboken-dev/usr/lib/libshiboken.so )
	$(shell ln -sf libshiboken-python${DEFAULT_PY}-dbg.so \
	               debian/shiboken-dbg/usr/lib/libshiboken-dbg.so )

#### Shared libs ####
override_dh_makeshlibs: $(PYVERSIONS:%=override_dh_makeshlibs-%)
	cat debian/libshiboken1.0.symbols-py* > debian/libshiboken1.0.symbols
	# Package shiboken ships a usr/lib/generatorrunner/*.so plugin. No shared libs in there.
	dh_makeshlibs -Nshiboken

override_dh_makeshlibs-%:
	echo "libshiboken-python$*.so.1.0 libshiboken1.0 #MINVER#" > debian/libshiboken1.0.symbols-py$*
	cat debian/libshiboken1.0.symbols.pristine >> debian/libshiboken1.0.symbols-py$*
ifeq ($(g++-version-ge-4.5),yes)
	# g++ version is >= 4.5, add related symbols
	sed -e 's|^ _| (ge-g++4.5)_|g' debian/libshiboken1.0.symbols.g++-ge-4.5 >> debian/libshiboken1.0.symbols-py$*
else
	# g++ version is < 4.5, add related symbols
	sed -e 's|^ _| (lt-g++4.5)_|g' debian/libshiboken1.0.symbols.g++-lt-4.5 >> debian/libshiboken1.0.symbols-py$*
endif


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