#!/usr/bin/make -f

include /usr/share/dpkg/pkg-info.mk
include /usr/share/dpkg/architecture.mk

# Hardening flags.
export DEB_BUILD_MAINT_OPTIONS=hardening=+all

BUILDDIR = obj-$(DEB_HOST_MULTIARCH)

# Retrieve environment information.
DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_HOST_ARCH_OS ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)

# Linux-specific stuff (dc1394, v4l, ois only available on linux)
ifeq ($(DEB_HOST_ARCH_OS),linux)
  CMAKE_ARCH_FLAGS = -DUSE_DC1394=ON -DUSE_V4L2=ON -DUSE_OIS=ON
else
  CMAKE_ARCH_FLAGS = -DUSE_DC1394=OFF -DUSE_V4L2=OFF -DUSE_OIS=OFF
endif

# Comply with Debian architectures baseline
# https://wiki.debian.org/ArchitectureSpecificsMemo#Architecture_baselines
# Enable only SSE2 since SSE3 is not guaranteed
CMAKE_ARCH_FLAGS += -DENABLE_SSE2=ON
CMAKE_ARCH_FLAGS += -DENABLE_SSE3=OFF
CMAKE_ARCH_FLAGS += -DENABLE_SSSE3=OFF

# On armel, since 3.4.0-3 build returns the following error
# "ABORT: Received TERM signal"
# Disable parallel build (make -j1) to reduce memory usage
ifeq ($(DEB_HOST_ARCH_OS),armel)
  export DEB_BUILD_MAINT_OPTIONS="parallel=1"
endif

# Check whether nodoc is present in the build profiles
ifneq (,$(filter nodoc,$(DEB_BUILD_PROFILES)))
  export WITHOUT_DOC=1
endif

BUILDDIR_DOC = obj-doc

# CMake flags
CMAKE_FLAGS = \
	-DCMAKE_INSTALL_PREFIX=/usr \
	-DCMAKE_VERBOSE_MAKEFILE=ON \
	-DCMAKE_C_FLAGS_RELEASE="$(CFLAGS)" \
	-DCMAKE_CXX_FLAGS_RELEASE="$(CXXFLAGS)" \
	-DCMAKE_SHARED_LINKER_FLAGS_RELEASE="$(LDFLAGS)" \
	-DCMAKE_EXE_LINKER_FLAGS_RELEASE="$(LDFLAGS)" \
	-DCMAKE_INSTALL_LIBDIR="lib" \
	-DCMAKE_BUILD_TYPE=Release \
	-DCMAKE_SKIP_RPATH=ON \
	-DENABLE_MULTIARCH=ON \
	-DENABLE_VISP_NAMESPACE=ON \
	-DUSE_SOQT=OFF \
	-DBUILD_JAVA=OFF

%:
	dh $@

override_dh_clean:
	dh_clean -O--buildsystem=cmake

# For arch:any
override_dh_auto_configure-arch:
	dh_auto_configure --arch -- $(CMAKE_FLAGS) $(CMAKE_ARCH_FLAGS)

# For arch:all
override_dh_auto_configure-indep:
ifndef WITHOUT_DOC
	dh_auto_configure --indep --builddirectory=$(BUILDDIR_DOC) -- $(CMAKE_FLAGS) \
		-DBUILD_APPS=OFF \
		-DBUILD_DEMOS=OFF \
		-DBUILD_EXAMPLES=OFF \
		-DBUILD_TESTS=OFF \
		-DBUILD_TUTORIALS=OFF \
		-DUSE_COIN3D=OFF \
		-DUSE_COMEDI=OFF \
		-DUSE_DC1394=OFF \
		-DUSE_DMTX=OFF \
		-DUSE_EIGEN3=OFF \
		-DUSE_FREENECT=OFF \
		-DUSE_GSL=OFF \
		-DUSE_JPEG=OFF \
		-DUSE_NLOHMAN_JSON=OFF \
		-DUSE_OPENCV=OFF \
		-DUSE_PCL=OFF \
		-DUSE_PNG=OFF \
		-DUSE_V4L2=OFF \
		-DUSE_X11=OFF \
		-DUSE_XML2=OFF \
		-DUSE_ZBAR=OFF
else
	@echo "Nodoc profile enabled: skipping configure-indep"
endif

override_dh_auto_build-arch:
	dh_auto_build --arch

override_dh_auto_build-indep:
ifndef WITHOUT_DOC
	dh_auto_build --indep --builddirectory=$(BUILDDIR_DOC) -- visp_doc
else
	@echo "Nodoc profile enabled: skipping build-indep"
endif

override_dh_install-arch:
	# Install only packages arch: any (libvisp-*, visp-tools)
	dh_install -a
	
override_dh_install-indep:
ifndef WITHOUT_DOC
	# Install libvisp-doc
	dh_install -i
else
	# Do nothing or install the other all packages if they exist, 
    	# explicitly excluding the documentation.
	dh_install -i -Nlibvisp-doc
endif

override_dh_installdocs:
ifndef WITHOUT_DOC
	dh_installdocs -X.md5 
endif

override_dh_auto_install:
	if [ -d "$(BUILDDIR)" ]; then \
		dh_auto_install --arch --builddirectory=$(BUILDDIR); \
	fi
	if [ -z "$(WITHOUT_DOC)" ]; then \
		if [ -d "$(BUILDDIR_DOC)" ]; then \
			dh_auto_install --indep --builddirectory=$(BUILDDIR_DOC); \
		fi; \
	fi

# Due to numerical imprecision, some tests are failing on ia64.
# This is not a critical issue so we let the testing fail on this
# architecture for now.
# See http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=723803
ifeq ($(DEB_HOST_ARCH),ia64)
  DH_AUTOTEST_CAN_FAIL=true
else
  DH_AUTOTEST_CAN_FAIL=false
endif

override_dh_auto_test-indep:

override_dh_auto_test-arch:
	-LD_LIBRARY_PATH=$(shell realpath $(BUILDDIR))/lib dh_auto_test || ${DH_AUTOTEST_CAN_FAIL}

override_dh_auto_clean:
	dh_auto_clean
	rm -rf $(BUILDDIR_DOC)
