#!/usr/bin/make -f
# -*- gmake -*-
# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

# Paths for Octave
OCTDIR = $(shell octave-config --print LOCALAPIOCTFILEDIR)

# Note cmake ignores CPPFLAGS so add them to CFLAGS and CXXFLAGS as a
# work around
CPPFLAGS += $(shell mkoctfile -p INCFLAGS)
CFLAGS += $(CPPFLAGS) -fvisibility=hidden -O2
# Don't add -fvisibility=hidden to CXXFLAGS for now as this breaks the
# octave bindings.
CXXFLAGS += $(CPPFLAGS) -O2
FFLAGS += -fvisibility=hidden
LDFLAGS += -Wl,--as-needed

export verbose_test = on

export QT_SELECT=5

CONFIGURE_OPTIONS = \
	-DUSE_RPATH=OFF -DPLD_psttf=OFF -DNON_TRANSITIVE=ON \
	-DENABLE_tk=ON -DENABLE_python=ON -DENABLE_ada=ON \
	-DOCTAVE_OCT_DIR=$(OCTDIR) -DTRY_OCTAVE4=ON \
	-DBUILD_DOC=ON \
	-DCMAKE_INSTALL_LIBDIR=/usr/lib/$(DEB_HOST_MULTIARCH) 

ifeq (,$(findstring nocheck, $(DEB_BUILD_OPTIONS)))
  CONFIGURE_OPTIONS += -DBUILD_TEST=ON
endif

%:
	dh $@ -Scmake --with python2,python3,ocaml

override_dh_auto_configure:
	xvfb-run dh_auto_configure -- $(CONFIGURE_OPTIONS)

override_dh_auto_test:
ifeq (,$(findstring nocheck, $(DEB_BUILD_OPTIONS)))
	ctest  --extra-verbose
	$(MAKE) -C obj-* VERBOSE=1 test_diff_psc
endif

override_dh_auto_install:
	dh_auto_install
	# Dirty hack: Make shared lib executable to force its detection
	# by dh_strip and dh_shlibdeps.
	# This will be reverted later, in override_dh_shlibdeps.
	# See https://bugs.debian.org/35733#35
	chmod ugo+x debian/tmp/usr/lib/*/octave/site/oct/api-*/*/plplot_octave.oct

	# Remove multiarch path in pkgconfig files.
	sed -i 's,-L"/usr/lib/$(DEB_HOST_MULTIARCH)",,g;s,  *, ,g' \
		debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig/*.pc

	dh_numpy
	dh_numpy3

override_dh_shlibdeps:
	dh_shlibdeps
	# Dirty hack, part 2: Remove the executable bit from the shared library
	chmod ugo-x debian/octave-plplot/usr/lib/*/octave/site/oct/api-*/*/plplot_octave.oct

override_dh_installchangelogs:
	dh_installchangelogs ChangeLog.release
