#!/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)

DEB_BUILD_MAINT_OPTIONS       := hardening=+all
DPKG_EXPORT_BUILDFLAGS        := 1
DEB_CFLAGS_MAINT_APPEND       := -fvisibility=hidden
DEB_FFLAGS_MAINT_APPEND       := -fvisibility=hidden
# Don't add -fvisibility=hidden to CXXFLAGS for now as this breaks the
# octave bindings.
DEB_CPPFLAGS_MAINT_APPEND     := $(shell mkoctfile -p INCFLAGS)
DEB_LDFLAGS_MAINT_APPEND      := -Wl,--as-needed
include /usr/share/dpkg/default.mk

# Note cmake ignores CPPFLAGS so add them to CFLAGS and CXXFLAGS as a
# work around, see #653916.
CFLAGS   += $(CPPFLAGS)
CXXFLAGS += $(CPPFLAGS)

HOME=$(shell mktemp -d)

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) \
	-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 python3,ocaml

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

override_dh_auto_configure: debian/plplotada.gpr
debian/plplotada.gpr: debian/plplotada.gpr.in
	sed s/@DEB_HOST_MULTIARCH@/$(DEB_HOST_MULTIARCH)/ $< > $@

override_dh_auto_test:
ifeq (,$(findstring nocheck, $(DEB_BUILD_OPTIONS)))
	#xvfb-run $(MAKE) -C obj-* VERBOSE=1 test_noninteractive
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 and -fPIC in pkgconfig files.
	sed -i -e 's,-L"/usr/lib/$(DEB_HOST_MULTIARCH)",,g;s,  *, ,g' \
	    -e 's,-fPIC,,g' \
	    debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig/*.pc

	# Remove tests for shared libs on Python and ADA from export_plplot:
	# These libs were renamed during build for multiarch
	sed '/\/_Pltk_init.so/d;/\/_plplotc.so/d;/\/plplot_pyqt5_/d;/\/libplplotada.so/d' \
	    -i debian/tmp/usr/lib/*/cmake/plplot/export_plplot-none.cmake

	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_compress:
	dh_compress -Xexamples

override_dh_installchangelogs:
	dh_installchangelogs ChangeLog.release
