#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

CPPFLAGS:=$(shell dpkg-buildflags --get CPPFLAGS)
CFLAGS   := $(shell dpkg-buildflags --get CPPFLAGS) $(shell dpkg-buildflags --get CFLAGS)
CXXFLAGS := $(shell dpkg-buildflags --get CPPFLAGS) $(shell dpkg-buildflags --get CXXFLAGS)
LDFLAGS  := $(shell dpkg-buildflags --get LDFLAGS)

include /usr/share/dpkg/architecture.mk

DEB_VERSION := $(shell dpkg-parsechangelog -S Version)
DEB_NOEPOCH_VERSION := $(shell echo $(DEB_VERSION) | cut -d: -f2-)
DEB_UPSTREAM_VERSION := $(shell echo $(DEB_NOEPOCH_VERSION) | sed 's/-[^-]*$$//')
DEB_STRIPPED_UPSTREAM_VERSION = $(shell echo $(DEB_UPSTREAM_VERSION) | sed -e 's/[\.\+]dfsg.*$$//' )

# upstream script generate_version_file.cmake uses leading v
# use with tweak version 0 to trigger a short upstream version
NETGEN_VERSION = v$(DEB_STRIPPED_UPSTREAM_VERSION)-0

PY3DEF := $(shell py3versions -dv | sed "s/\.//")

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

export MACHINE=LINUX

extra_flags += \
	-DNETGEN_VERSION_GIT=$(NETGEN_VERSION) \
	-DNG_INSTALL_DIR_INCLUDE:PATH=include/netgen \
	-DPREFER_SYSTEM_PYBIND11=ON \
	-DBUILD_STUB_FILES=OFF \
	-DUSE_OCC:BOOL=ON \
	-DUSE_JPEG:BOOL=ON \
	-DUSE_MPEG:BOOL=ON \
	-DUSE_SUPERBUILD:BOOL=OFF \
	-DUSE_NATIVE_ARCH:BOOL=OFF \
	-DUSE_MPI:BOOL=ON \
	-DUSE_MPI4PY:BOOL=ON \
	-DENABLE_UNIT_TESTS=ON \
	-DCMAKE_BUILD_TYPE=RelWithDebInfo \
	-DCMAKE_SKIP_RPATH=ON

execute_after_dh_clean:
	rm -rf tests/pytest/__pycache__ tests/pytest/.pytest_cache tests/pytest/*vol.gz

override_dh_auto_configure:
	dh_auto_configure -- $(extra_flags)
	sed -i s/DEB_HOST_MULTIARCH/${DEB_HOST_MULTIARCH}/ debian/netgen.desktop

ifneq ($(DEB_HOST_ARCH),amd64)
# this subset of demo tests (test_tutorials.py::test_geoFiles)
# only passes on amd64
    SKIP_TEST_LIST += boxcyl.geo-mp11-5 \
	cylsphere.geo-mp58-4 cylsphere.geo-mp61-1 cylsphere.geo-mp64-4 \
	hinge.stl-mp93-5 hinge.stl-mp99-5 part1.stl-mp129-1 \
	part1.stl-mp123-1 part1.stl-mp126-4 part1.stl-mp132-4 \
	sculpture.geo-mp143-4 sculpture.geo-mp149-4 \
	sphereincube.geo-mp162-5 sphereincube.geo-mp168-5 \
	twobricks.geo-mp195-2 twobricks.geo-mp199-0 twobricks.geo-mp201-2 twobricks.geo-mp202-3 \
	twocubes.geo-mp201-2 twocubes.geo-mp205-0 twocubes.geo-mp207-2 twocubes.geo-mp208-3
endif

ifeq ($(DEB_HOST_ARCH),i386)
    CXXFLAGS += -ffloat-store
endif

override_dh_auto_test:
	dh_auto_install
	list_initialised=0; \
        for t in $(SKIP_TEST_LIST); do \
            if [ $${list_initialised} = 0 ]; then \
                SKIP_TESTS=$$t; \
                list_initialised=1; \
            else \
                SKIP_TESTS="$${SKIP_TESTS} or $$t"; \
            fi; \
        done; \
        if [ "x$${SKIP_TESTS}" != "x" ]; then \
            SKIP_TESTS="not ( $${SKIP_TESTS} )"; \
        fi; \
        echo "skipping tests: $${SKIP_TESTS}"; \
	cd tests/pytest && \
          PYTHONPATH=$(CURDIR)/debian/tmp/usr/lib/python3/dist-packages \
          LD_LIBRARY_PATH=$(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH) \
          python3 -m pytest -v -k "$${SKIP_TESTS}"

execute_after_dh_python3-arch:
	for pylib in libngpy libngguipy; do \
	  pylib_dh=`basename debian/python3-netgen/usr/lib/python3/dist-packages/netgen/$${pylib}*$(PY3DEF)*.so`; \
	  sed "s|netgen/$${pylib}.so|netgen/$${pylib_dh}|" -i debian/libnglib-dev/usr/lib/$(DEB_HOST_MULTIARCH)/cmake/netgen/netgen-targets-relwithdebinfo.cmake; \
	done

override_dh_shlibdeps:
	dh_shlibdeps -l/usr/lib/${DEB_HOST_MULTIARCH}/netgen
