#!/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_EXAMPLES=OFF \
	  -DBUILD_PYTHON_WRAPPERS=OFF -DBUILD_JAVASCRIPT_WRAPPERS=OFF

override_dh_auto_build-nopy:
	dh_auto_build
	mv build build-nopy

# Reconfigure base build for specific Python version and build wrappers
# WARNING: Python2 and Python3 MUST be built sequentially since they use the 
# same build directory
override_dh_auto_build-py: override_dh_auto_build-nopy
	for Python in $(ALLPY); do \
	  PythonNoDot=$$(echo $${Python} | sed 's|\.||'); \
	  cp -a build-nopy build; \
	  dh_auto_configure -- \
	    -DBUILD_EXAMPLES=OFF \
	    -DBUILD_PYTHON_WRAPPERS=ON -DBUILD_JAVASCRIPT_WRAPPERS=OFF \
	    -DPython_ADDITIONAL_VERSIONS=$${Python} \
	    -DPYTHON_EXECUTABLE=/usr/bin/python$${Python} \
	    -DBoost_PYTHON_LIBRARY_DEBUG=/usr/lib/$(TRIPLET)/libboost_python-py$${PythonNoDot}.so \
	    -DBoost_PYTHON_LIBRARY_RELEASE=/usr/lib/$(TRIPLET)/libboost_python-py$${PythonNoDot}.so; \
	  dh_auto_build; \
	  mv build build-py$${Python}; \
	done

override_dh_auto_build-arch: override_dh_auto_build-nopy override_dh_auto_build-py

override_dh_auto_build-indep:
	dh_auto_configure --builddirectory=build-indep -- \
	  -DBUILD_EXAMPLES=OFF \
	  -DBUILD_PYTHON_WRAPPERS=ON -DBUILD_JAVASCRIPT_WRAPPERS=OFF \
	  -DPYTHON_EXECUTABLE=/usr/bin/python$(PYTHON3)
	mkdir -p documentation/_build
	cd documentation && doxygen
	find documentation/_build -name "*.md5" -delete

override_dh_makeshlibs-arch:
	dh_makeshlibs -V'libodil0 (>= 0.9.2-1)'

# 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
# WARNING: Python2 and Python3 MUST be tested sequentially since they use the 
# same build directory
override_dh_auto_test-arch-py: override_dh_auto_test-arch-nopy
	for Python in $(ALLPY); do \
	  ln -s build-py$${Python} build; \
	  ln -sr ./build/wrappers/python ./build/odil; \
	  ln -sr ./wrappers/python/*.py ./build/odil; \
	  cd build && \
	    ../tests/run --no-network \
	      --nose nosetests$$(dpkg --compare-versions $${Python} ge 3 && echo "3") \
	      -E ".*"; \
	  cd ..; \
	  rm ./build/odil/*py; \
	  rm ./build/odil; \
	  rm build; \
	done

override_dh_auto_test-arch: override_dh_auto_test-arch-nopy override_dh_auto_test-arch-py

override_dh_auto_install-arch-nopy:
	ln -s build-nopy build
	$(MAKE) -C build/src install DESTDIR=$(CURDIR)/debian/tmp
	rm build

# WARNING: Python2 and Python3 MUST be installed sequentially since they use the 
# same build directory
override_dh_auto_install-arch-py: override_dh_auto_install-arch-nopy
	for Python in $(ALLPY); do \
	  ln -s build-py$${Python} build; \
	  $(MAKE) -C build/wrappers/python install DESTDIR=$(CURDIR)/debian/tmp; \
	  rm build; \
	done

override_dh_auto_install-arch: override_dh_auto_install-arch-nopy override_dh_auto_install-arch-py

override_dh_auto_install-indep:
	ln -s build-indep build
	$(MAKE) -C build/applications install DESTDIR=$(CURDIR)/debian/tmp
	rm build

override_dh_install-arch:
	dh_install
	d-shlibmove \
		--devunversioned \
		--commit --multiarch --exclude-a --exclude-la \
		--movedev debian/tmp/usr/include/* usr/include \
		debian/tmp/usr/lib/*.so


override_dh_clean:
	dh_clean
	find tests -name "*pyc" -delete
	rm -rf build-nopy $(ALLPY:%=build-py%) build-indep documentation/_build

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