#! /usr/bin/make -f
#export DH_VERBOSE=1

include /usr/share/dpkg/architecture.mk
include /usr/share/mpi-default-dev/debian_defaults

DESTDIR = $(CURDIR)/debian/tmp
UPSTREAM_VERSION = $(shell dpkg-parsechangelog | awk '/^Version:/ { sub("-[^-]*$$", "", $$2); print $$2}')
UPSTREAM_VERSION_ABI = $(shell echo $(UPSTREAM_VERSION) | sed -e 's/[\.\+]dfsg.*$$//' | awk 'BEGIN { FS="."}; {print $$1"."$$2 }')

%:
	dh $@ --buildsystem=cmake 

PY3DEF=$(shell py3versions -dv)
# many libvtk*.so files link to the specific python version
# so we can only support the default python
PY3VERS=$(PY3DEF)
PY3RANGE=$(shell echo $(PY3DEF) | awk 'BEGIN { FS="." }; { print $$1 "." $$2 "-" $$1 "." $$2 + 1 }')

# The Qt Svg component library is not detected automatically
# but is needed for labels and icons in the paraview GUI.
# QT_SVG_DEB identifies the debian binary package for the library.
QT_SVG_DEB = libqt6svg6
QT_SVG_VER = $(shell dpkg-query  -f '$${Version}'  -W $(QT_SVG_DEB))
QT_SVG_DEPENDS = "$(QT_SVG_DEB) (>= $(QT_SVG_VER)~)"

export LD_LIBRARY_PATH+=:$(CURDIR)/debian/paraview/usr/lib/paraview

CPPFLAGS:=$(shell dpkg-buildflags --get CPPFLAGS)
CFLAGS:=$(shell dpkg-buildflags --get CFLAGS) $(CPPFLAGS) -Wall -pedantic 
CXXFLAGS:=$(shell dpkg-buildflags --get CXXFLAGS) $(CPPFLAGS) -O0
LDFLAGS:=$(shell dpkg-buildflags --get LDFLAGS) -Wl,--as-needed

# libatomic is not automatically linked by the tool chain even when needed
ifneq (,$(filter $(DEB_HOST_ARCH), armel m68k mips mipsel powerpc sh4))
  export DEB_CXXFLAGS_MAINT_APPEND += -Wl,--no-as-needed -latomic -Wl,--as-needed
  export DEB_LDFLAGS_MAINT_APPEND += -Wl,--no-as-needed -latomic -Wl,--as-needed
endif

# Investigate: Pixie Needs ADIOS1
#	-DPARAVIEW_PLUGIN_ENABLE_AdiosReaderPixie=ON 
#	-DPARAVIEW_PLUGIN_ENABLE_AdiosReaderStaging=ON 

extra_flags += -Wno-dev \
	-DPARAVIEW_USE_EXTERNAL_VTK=ON \
	-DVTK_OPENGL_HAS_EGL=YES \
	-DBUILD_TESTING=OFF \
	-DCMAKE_C_FLAGS="${CFLAGS}" -DCMAKE_CXX_FLAGS="${CXXFLAGS}" \
	-DCMAKE_BUILD_TYPE=DEBUG \
	-DPARAVIEW_BUILD_DEVELOPER_DOCUMENTATION=ON \
	-DCMAKE_INSTALL_RPATH=/usr/lib/paraview \
	-DCMAKE_INSTALL_RPATH_USE_LINK_PATH=OFF  \
	-DCMAKE_SKIP_RPATH=OFF \
	-DPARAVIEW_ENABLE_FFMPEG=ON \
	-DPARAVIEW_ENABLE_WEB=OFF \
	-DPARAVIEW_BUILD_SHARED_LIBS=ON \
	-DHDF5_ROOT=/usr/lib/$(DEB_HOST_MULTIARCH)/hdf5/$(ARCH_DEFAULT_MPI_IMPL) \
	-DPARAVIEW_PYTHON_SITE_PACKAGES_SUFFIX="lib/python3/dist-packages" \
	-DPARAVIEW_USE_MPI=ON \
	-DPARAVIEW_USE_PYTHON=ON \
	-DPARAVIEW_ENABLE_VISITBRIDGE=OFF \
	-DPARAVIEW_ENABLE_GDAL=ON \
	-DPARAVIEW_ENABLE_MOTIONFX=ON \
	-DPARAVIEW_PLUGIN_ENABLE_GmshIO=ON \
	-DPARAVIEW_PLUGIN_CONF_EXTRA_PATHS="/usr/share/paraview/plugins" \
	-DPARAVIEW_CONF_IN_LIBDIR=ON \
	-DDEB_HOST_MULTIARCH=$(DEB_HOST_MULTIARCH)

# 32-bit architectures don't support all features
ifeq ($(DEB_HOST_ARCH_BITS),32)
  # VTK::vtkviskores exhausts virtual memory on 32-bit architectures
  extra_flags += -DPARAVIEW_USE_VISKORES=OFF
else
  # vtk's copy of xdmf3 fails to build on 32-bit arches, see Bug#1126484
  extra_flags += -DPARAVIEW_ENABLE_XDMF3=ON

  # Adios2 is only available on 64-bit systems
  extra_flags += -DPARAVIEW_ENABLE_ADIOS2=ON
endif

override_dh_auto_clean:
	dh_auto_clean
	rm -fr build.*

my_configure_python%: pyver=$(patsubst my_configure_python%,%,$@)
my_configure_python%:
	dh_auto_configure -Bbuild.python$(pyver) -- $(extra_flags) -DPython3_EXECUTABLE=/usr/bin/python$(pyver) -DDEBIAN_PYTHON_VERSION=$(pyver)

override_dh_auto_configure: $(foreach pyver,$(PY3VERS), my_configure_python$(pyver))

my_build_python%: pyver=$(patsubst my_build_python%,%,$@)
my_build_python%:
	dh_auto_build -Bbuild.python$(pyver)

override_dh_auto_build: $(foreach pyver,$(PY3VERS), my_build_python$(pyver))

my_install_python%: pyver=$(patsubst my_install_python%,%,$@)
my_install_python%:
	dh_auto_install -Bbuild.python$(pyver)
	cd build.python$(pyver)/bin $(foreach exe,pvbatch pvpython,&& cp $(exe) $(DESTDIR)/usr/bin/$(exe)$(pyver))

override_dh_auto_install: $(foreach pyver,$(PY3VERS), my_install_python$(pyver))
	# Use the defaut python3 version for the tools
	cd debian/tmp/usr/bin $(foreach exe,pvbatch pvpython,&& ln -fs $(exe)$(PY3DEF) $(exe))

my_test_python%: pyver=$(patsubst my_test_python%,%,$@)
my_test_python%:
	dh_auto_test -Bbuild.python$(pyver)

override_dh_python3:
	dh_python3 -V $(PY3RANGE)

override_dh_auto_test: $(foreach pyver,$(PY3VERS), my_test_python$(pyver))

override_dh_compress:
	dh_compress -X.qch

override_dh_makeshlibs:
	dh_makeshlibs -V'paraview (>= $(UPSTREAM_VERSION)), paraview (<< $(UPSTREAM_VERSION).)'

override_dh_gencontrol:
	dh_gencontrol -- -VQtsvg:Depends=$(QT_SVG_DEPENDS)
