#!/usr/bin/make -f
# -*- makefile -*-

# Needed by octave-common.links
include /usr/share/dpkg/pkg-info.mk
DEB_VERSION_UPSTREAM := $(subst ~rc,-rc,$(DEB_VERSION_UPSTREAM))
export DEB_VERSION_UPSTREAM

# Configure default Java paths using javahelper except on some archs.
# hurd-i386 and hppa have gcj as default, which cause either
# a compilation failure (hurd-i386) or a test failure (other archs).
# On mips and mipsel java unit tests calling openjdk-7 cause a bus error at
# runtime.
# This arch list must be kept in sync with the arch list qualifying
# default-jre in debian/control (note: we always want default-jdk in
# build deps because it is needed for octave-common, see
# debian/patches/always-build-octave-jar.patch).
include /usr/share/dpkg/architecture.mk
NO_JDK_ARCHS := armel hppa hurd-i386 m68k mips mipsel powerpc sparc64
ifneq (,$(filter $(DEB_HOST_ARCH),$(NO_JDK_ARCHS)))
WITH_JAVA_FLAGS := --disable-java
else
include /usr/share/javahelper/java-vars.mk
JAVA_LIBDIR := $(firstword $(JVM_CLIENT_DIR) $(JVM_SERVER_DIR))
WITH_JAVA_FLAGS := --with-java-homedir=$(JAVA_HOME) --with-java-libdir=$(JAVA_LIBDIR)
endif

# Disable the experimental JIT unconditionally for now. The LLVM library
# interface is unstable. Linking with LLVM may also cause conflicts with other
# libraries, for example the mesa graphics stack which also uses LLVM.
JIT_FLAG := --disable-jit

# Needed to force the serial version of HDF5 even if other flavours are
# installed.
ifneq ($(wildcard /usr/lib/$(DEB_HOST_MULTIARCH)/hdf5/serial/libhdf5.so),)
  HDF5_FLAGS := --with-hdf5-includedir=/usr/include/hdf5/serial \
	--with-hdf5-libdir=/usr/lib/$(DEB_HOST_MULTIARCH)/hdf5/serial
endif

# Support the nodoc flag of DEB_BUILD_OPTIONS
ifneq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
override_dh_auto_configure:
DOC_FLAG := --disable-docs
endif

%:
	dh $@

override_dh_auto_configure:
	# Enforce generic BLAS (in order to avoid tying the binary to OpenBLAS or ATLAS)
	# Also pass OpenMP flag (#631831)
	# Pass the install directory of fonts-freefont-otf package
	dh_auto_configure -- \
		--with-blas=blas --enable-openmp \
		--with-system-freefont=/usr/share/fonts/opentype/freefont \
		$(WITH_JAVA_FLAGS) $(JIT_FLAG) $(HDF5_FLAGS) $(DOC_FLAG)

# dh_auto_test tries to run "make test", so override it
override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	make check
endif

# override normal dh_compress call to avoid compressing .pdf files
override_dh_compress:
	dh_compress --exclude=.pdf

override_dh_auto_install:
	dh_auto_install
	# Delete .la files (for liboctgui)
	find debian/tmp -name '*.la' -delete

# The info files have references to .png images, so also ship those.
# Note that we do not install all doc/interpreter/*.png, since some are not referenced
# when compiling the output info (using @ifnotinfo macro).
# We used to rename them by prefixing their name with "octave-", but this was breaking some
# info files (see #816534).
override_dh_installinfo-indep:
	dh_installinfo
	for f in `grep -a src=\".*\" doc/interpreter/*.info* | sed 's/.*src="\([^"]*\)".*/\1/'`; do \
		cp doc/interpreter/$$f debian/octave-doc/usr/share/info/; \
	done

# Strip the executable bit from the .oct files
# Cannot be done in dh_fixperms, as then dh_strip and dh_shlibdeps don't take the
# .oct files into account
override_dh_shlibdeps:
	dh_shlibdeps
	find debian/octave -name '*.oct' -print0 2>/dev/null | xargs -0r chmod 644

# Avoid useless call of ldconfig in the triggers script of the octave
# package
override_dh_makeshlibs:
	dh_makeshlibs --package=octave --noscripts
	dh_makeshlibs --no-package=octave
