#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
include /usr/share/dpkg/buildflags.mk
CFLAGS+=$(CPPFLAGS)
CXXFLAGS+=$(CPPFLAGS)
DPKG_EXPORT_BUILDFLAGS = 1

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

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	test_makeflags = -j$(NUMJOBS)
endif

# Explicitly set builddir directory
builddir := obj-$(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)

PY3VER = $(shell py3versions -d)
PYALL = $(PY3VER)

%:
	dh $@ --buildsystem=cmake --with python3

override_dh_auto_clean: $(PYALL:python%=cmake-clean-%)

cmake-clean-%:
	# If we populated a persalys directory for Python modules during the tests,
	# we remove them.
	if [ -e build-python$*/lib/test/persalys ]; then rm -rf build-python$*/lib/test/persalys; fi
	if [ -e python/test/persalys ]; then rm -rf python/test/persalys; fi
	dh_auto_clean

override_dh_auto_configure: $(PYALL:python%=cmake-configure-%) \

cmake-configure-%:
	dh_auto_configure -Bbuild-python$* -- \
            -DUSE_SPHINX=OFF \
            -DUSE_SALOME=OFF \
            -DCMAKE_SKIP_INSTALL_RPATH:BOOL=ON \
            -DCMAKE_INSTALL_PREFIX:PATH=/usr \
            -DINSTALL_DESTDIR:PATH=$(CURDIR)/debian/persalys \
            -DLINK_PYTHON_LIBRARY:BOOL=OFF \
            -DPYTHON_EXECUTABLE:FILEPATH=/usr/bin/python$* \
            -DPYTHON_INCLUDE_DIR:PATH=$$(python$*-config --includes | sed -e 's/ .*//' -e 's/^-I//') \
            -DPYTHON_LIBRARY:PATH=$$(python$*-config --ldflags | sed -e 's/ .*//' -e 's/^-L//')/libpython$*.so

override_dh_auto_build: $(PYALL:python%=cmake-build-%)

cmake-build-%:
	$(MAKE) $(test_makeflags) -C build-python$*

override_dh_auto_install: $(PYALL:python%=cmake-install-%)

cmake-install-%:
	dh_auto_install -Bbuild-python$*
ifeq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
	# Building the folder of the persalys Python module in the Python test
	# directory so that it is found during the tests.
	if [ ! -e python/test/persalys ]; then \
	        mkdir python/test/persalys ; \
	        cp python/src/__init__.py python/test/persalys ; \
	        cp build-python$*/python/src/persalys.py python/test/persalys ; \
	        cp build-python$*/python/src/_persalys.so python/test/persalys ; \
	fi
	# Looking for the shared lib that is about to be installed.
	LD_LIBRARY_PATH=$${LD_LIBRARY_PATH:+$${LD_LIBRARY_PATH}:}$(CURDIR)/debian/libpersalys0/usr/lib/$(DEB_HOST_MULTIARCH) \
	  CTEST_OUTPUT_ON_FAILURE=1 \
	  $(MAKE) $(test_makeflags) -C build-python$* test ARGS="$(test_makeflags) -R pyinstallcheck"
	find $(CURDIR)/debian/persalys -name "*.pyc" -o -name "__pycache__" | xargs rm -rf
endif

override_dh_install:
	dh_install
	# Installing and renaming icon
	mkdir -p debian/persalys/usr/share/icons/hicolor/64x64/apps
	cp images/Ps-icon-64.png debian/persalys/usr/share/icons/hicolor/64x64/apps/persalys-icon.png

override_dh_makeshlibs:
	dh_makeshlibs -Xpyshared

override_dh_compress:
	dh_compress -X.pdf -X.svn -X.py

override_dh_auto_test: $(PYALL:python%=cmake-test-%)

cmake-test-%:
	# Building the folder of the persalys Python module in the C++ test directory
	# so that it is found during the tests.
ifeq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
	if [ ! -e build-python$*/lib/test/persalys ]; then \
	        mkdir build-python$*/lib/test/persalys ; \
	        cp python/src/__init__.py build-python$*/lib/test/persalys ; \
	        cp build-python$*/python/src/persalys.py build-python$*/lib/test/persalys ; \
	        cp build-python$*/python/src/_persalys.so build-python$*/lib/test/persalys ; \
	fi
	# 'make test' does not build binary tests
	$(MAKE) $(test_makeflags) -C build-python$* tests
	CTEST_OUTPUT_ON_FAILURE=1 \
	  $(MAKE) -C build-python$* test ARGS="$(test_makeflags) -R cppcheck"
endif
