#!/usr/bin/make -f

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

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

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

# 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 -a --builddirectory=build-$*-dbg -- \
	-DCMAKE_BUILD_TYPE:STRING="Debug" \
	-DCMAKE_SKIP_RPATH=true \
	-DLIB_SUFFIX=/$(DEB_HOST_MULTIARCH) \
	-DCMAKE_USE_PYTHON_VERSION="$*-dbg;$*_d;$*" \
	-DPython_ADDITIONAL_VERSIONS="$*-dbg;$*_d;$*"

override_dh_auto_configure-%:
	mkdir -p build-$*
	dh_auto_configure --builddirectory=build-$* -- \
	-DCMAKE_BUILD_TYPE:STRING="Release" \
	-DCMAKE_SKIP_RPATH=true \
	-DLIB_SUFFIX=/$(DEB_HOST_MULTIARCH) \
	-DCMAKE_USE_PYTHON_VERSION=$* \
	-DPython_ADDITIONAL_VERSIONS=$* \
	-DDEFAULT_PYTHON_BASENAME=python${DEFAULT_PY}

#### BUILD ####

override_dh_auto_build: $(PYVERSIONS:%=override_dh_auto_build_dbg-%) $(PYVERSIONS:%=override_dh_auto_build-%)
	dh_auto_build -i --builddirectory=build-$(DEFAULT_PY) -- doc

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

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

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

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

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

override_dh_install_done: override_dh_install_real
ifneq (,$(filter libshiboken-dev, $(shell dh_listpackages)))
	# Setup the default version symbolic links
	ln -sf libshiboken-python${DEFAULT_PY}.so \
	               debian/libshiboken-dev/usr/lib/$(DEB_HOST_MULTIARCH)/libshiboken.so
endif
ifneq (,$(filter shiboken-dbg, $(shell dh_listpackages)))
	ln -sf libshiboken-python${DEFAULT_PY}-dbg.so \
	               debian/shiboken-dbg/usr/lib/$(DEB_HOST_MULTIARCH)/libshiboken-dbg.so
endif

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

override_dh_installdocs:
	dh_installdocs
ifneq (,$(filter shiboken-doc, $(shell dh_listpackages)))
	dh_sphinxdoc -pshiboken-doc
endif
