#!/usr/bin/make -f

export DEB_CXXFLAGS_MAINT_APPEND  = -std=c++0x
export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# Find all Python versions
PYTHON2=$(shell pyversions -vrd)
PYTHON3=$(shell py3versions -vrd)
ALLPY=$(PYTHON2) $(PYTHON3)

# Required to get the full path to the boost.python library
TRIPLET=$(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

%:
	dh $@ --builddirectory=build --with python2,python3

# Base build: no wrappers
override_dh_auto_configure-arch:
	dh_auto_configure -- -DBUILD_WRAPPERS=OFF

override_dh_auto_build-nopy:
	dh_auto_build
	mv build build-nopy

# Reconfigure base build for specific Python version and build wrappers
override_dh_auto_build-py%: override_dh_auto_build-nopy
	cp -a build-nopy build
	dh_auto_configure -- \
	  -DBUILD_WRAPPERS=ON -DPython_ADDITIONAL_VERSIONS=$* \
	  -DPYTHON_EXECUTABLE=/usr/bin/python$* \
	  -DBoost_PYTHON_LIBRARY_DEBUG=/usr/lib/$(TRIPLET)/libboost_python-py$(subst .,,$*).so \
	  -DBoost_PYTHON_LIBRARY_RELEASE=/usr/lib/$(TRIPLET)/libboost_python-py$(subst .,,$*).so
	dh_auto_build
	mv build build-py$*

override_dh_auto_build-arch: override_dh_auto_build-nopy $(ALLPY:%=override_dh_auto_build-py%)

override_dh_auto_build-indep:
	doxygen

override_dh_makeshlibs-arch:
	dh_makeshlibs -V'libodil0 (>= 0.8.0-3)'

# Run C++ tests only on base build
override_dh_auto_test-arch-nopy:
	ln -s build-nopy build
	cd build && ../tests/run --no-network -e ".*"
	rm build

# Run Python tests only on python build
override_dh_auto_test-arch-py%:
	ln -s build-py$* build
	cd build && \
	  ../tests/run --no-network \
	    --nose nosetests$(shell dpkg --compare-versions $* ge 3 && echo "3") \
	    -E ".*"
	rm build

override_dh_auto_test-arch: override_dh_auto_test-arch-nopy $(ALLPY:%=override_dh_auto_test-arch-py%)

# Only auto_install base build: wrappers are installed with *.install files
override_dh_auto_install-arch:
	ln -s build-nopy build
	dh_auto_install
	rm build

override_dh_install-arch-py%:
	sed \
	  's/VERSION/$*/g' \
	  debian/python$(shell dpkg --compare-versions $* ge 3 && echo "3")-odil.install.in > \
	  debian/python$(shell dpkg --compare-versions $* ge 3 && echo "3")-odil.install

override_dh_install-arch: $(ALLPY:%=override_dh_install-arch-py%)
	dh_install
	chrpath -d debian/python*-odil/usr/lib/python*/dist-packages/odil*.so
	d-shlibmove \
		--devunversioned \
		--commit --multiarch --exclude-a --exclude-la \
		--movedev debian/tmp/usr/include/* usr/include \
		--override s/libjsoncpp.-dev/libjsoncpp-dev/ \
		--override s/libdcm.*-dev/libdcmtk-dev/ \
		--override s/libof.*-dev/libdcmtk-dev/ \
		--override s/libicuuc.*-dev/libicu-dev/ \
		debian/tmp/usr/lib/*.so

override_dh_fixperms-indep:
	dh_fixperms
	chmod a-x $(CURDIR)/debian/odil/usr/share/odil/store.py

override_dh_clean:
	dh_clean
	find tests -name "*pyc" -delete
	rm -rf build-nopy $(ALLPY:%=build-py%) doc
	rm -f debian/python*-odil.install

# These steps are not needed for arch-independent packages
override_dh_auto_configure-indep:
override_dh_auto_test-indep:
override_dh_auto_install-indep:
override_dh_makeshlibs-indep:
