#!/usr/bin/make -f

#export DH_VERBOSE=1

include /usr/share/dpkg/architecture.mk

export CPPFLAGS := $(shell dpkg-buildflags --get CPPFLAGS) -fPIC
export CFLAGS   := $(CPPFLAGS) $(shell dpkg-buildflags --get CFLAGS)
export CXXFLAGS := $(CPPFLAGS) $(shell dpkg-buildflags --get CXXFLAGS)
export LDFLAGS  := -fPIC $(shell dpkg-buildflags --get LDFLAGS) -Wl,-z,defs -Wl,--as-needed

PYTHON3_VERSION     := $(shell py3versions -dv)

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

ifneq ($(wildcard /usr/lib/$(DEB_HOST_MULTIARCH)/libpython$(PYTHON3_VERSION).so),)
PYTHON3_LIBRARY=/usr/lib/$(DEB_HOST_MULTIARCH)/libpython$(PYTHON3_VERSION).so
else
PYTHON3_LIBRARY=/usr/lib/libpython$(PYTHON3_VERSION).so
endif

empty :=
space := $(empty)$(empty)

# Qt/GL support is broken on ARM, see Bug#798408
# The problem is triggered by the overlayaxes plugin
# so skip this plugin on arm.
# arm64 currently builds but that could change in the future
ARCH_NO_GL_LIST := armel armhf
ifneq (,$(findstring $(space)$(DEB_HOST_ARCH)$(space), $(space)$(ARCH_NO_GL_LIST)$(space)))
  OVERLAYAXES_CONFIG := -DUSE_QT_OPENGL=OFF
else
  OVERLAYAXES_CONFIG := -DUSE_QT_OPENGL=ON
endif

# avogadro treats warnings as fatal errors
# so don't warn about the new GCS feature on arm64
ifeq ($(DEB_TARGET_ARCH),arm64)
export DEB_LDFLAGS_MAINT_APPEND=-Wl,-z,gcs-report-dynamic=none
endif

CMAKE_OPTS=-DUSE_HDF5=ON \
	    -DUSE_LIBMSYM=OFF \
	    -DUSE_MMTF=OFF \
	    -DOpenGL_GL_PREFERENCE=GLVND \
	    -DCMAKE_SKIP_RPATH=TRUE \
	    -DUSE_PYTHON=ON \
	    -DUSE_VTK=OFF \
	    -DUSE_QT=ON -DQT_VERSION=6 \
	    -DENABLE_TRANSLATIONS=ON \
	    $(OVERLAYAXES_CONFIG)

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

override_dh_auto_configure-arch:
	dh_auto_configure -- $(CMAKE_OPTS)

override_dh_auto_configure-indep:
	dh_auto_configure -- -DBUILD_DOCUMENTATION=ON $(CMAKE_OPTS)

execute_after_dh_auto_build-indep:
	cd obj-$(DEB_HOST_GNU_TYPE); make documentation

execute_after_dh_auto_install-arch:
	cp -r obj-$(DEB_HOST_GNU_TYPE)/bin debian/tmp/usr

execute_after_dh_auto_install-indep:
	cp -r obj-$(DEB_HOST_GNU_TYPE)/docs/html debian/tmp
	rm -f debian/tmp/usr/share/avogadro2/fragments/.gitignore
