#!/usr/bin/make -f


PY3VER := $(shell py3versions -d -v | tr -d .)
export PYTHON = $(shell py3versions -d)

# less debug info to avoid running out of address space
ifneq (,$(filter $(DEB_HOST_ARCH), mips mipsel))
  export DEB_CXXFLAGS_MAINT_APPEND += -g1
endif

include /usr/share/dpkg/default.mk

%:
	dh $@ --with python3

export OMPI_MCA_plm_rsh_agent=/bin/false                #workaround to start MPI-applications in chroot
disable_auto_test_archs_mpi = hurd-i386 mips mipsel s390 s390x ia64 armhf i386

export LD_LIBRARY_PATH :=  $(CURDIR)/debian/esys-particle/usr/lib/${DEB_HOST_MULTIARCH}/:$(CURDIR)/debian/esys-particle/usr/lib/python3/dist-packages/esys/lsm/util/:$(CURDIR)/debian/esys-particle/usr/lib/python3/dist-packages/esys/lsm/:$(LD_LIBRARY_PATH);

export PYTHONPATH := $(CURDIR)/debian/esys-particle/usr/lib/python3/dist-packages/:$(PYTHONPATH);

export NO_PNG_PKG_MANGLE := 1
export DEB_BUILD_MAINT_OPTIONS = hardening=+all

CFLAGS := -Wall $(shell dpkg-buildflags --get CFLAGS)
CXXFLAGS := -Wall $(shell dpkg-buildflags --get CXXFLAGS) -std=c++14
CPPFLAGS := $(shell dpkg-buildflags --get CPPFLAGS)
LDFLAGS := -lboost_system $(shell pkg-config --libs python3) $(shell dpkg-buildflags --get LDFLAGS)

extra_flags += CC=mpicc CXX=mpicxx CFLAGS='$(CFLAGS)' CXXFLAGS='$(CXXFLAGS)' CPPFLAGS='$(CPPFLAGS)'\
 LDFLAGS='$(LDFLAGS)' --with-vtk --with-latex --enable-vtk

override_dh_auto_configure:
	dh_auto_configure -- $(extra_flags) --with-boost-libdir=/usr/lib/$(DEB_HOST_MULTIARCH) --with-boost-python='boost_python$(PY3VER)'

execute_after_dh_auto_build:
	doxygen

execute_after_dh_auto_install:
ifeq (,$(filter $(DEB_HOST_ARCH),$(disable_auto_test_archs_mpi)))
	cd $(CURDIR)/debian/test; python3 box.py 15.0
endif
	find debian -name '*.pyc' -delete
	find debian -name '*.pyo' -delete
	find debian -name '*.la' -delete
	find debian -type d -name __pycache__ -exec rm -rf {} +

execute_after_dh_installdocs:
	find debian -name '*.md5' -delete
	find debian -name '_form0.ps' -delete