#!/usr/bin/make -f

BUILDDIR = $(CURDIR)/debian/build

PYVERS := $(shell pyversions -vr debian/control)

# The directories below are versioned.  We only support the packages for the
# stable version of Octave
mpath = $(shell octave-config -p LOCALFCNFILEDIR)/nlopt
bpath = $(shell octave-config -p LOCALOCTFILEDIR)/nlopt

export DEB_BUILD_MAINT_OPTIONS=hardening=+all

%:
	dh $@  --buildsystem=autoconf --builddirectory=$(BUILDDIR) --with python2

override_dh_auto_configure:
	for v in $(PYVERS); do \
	   dh_auto_configure --builddirectory=$(BUILDDIR)$$v \
	        -- $(extra_flags) \
		--prefix=/usr \
		--srcdir=$(CURDIR) \
		--enable-shared \
		PYTHON=/usr/bin/python$$v \
		OCT_INSTALL_DIR=$(bpath) M_INSTALL_DIR=$(mpath) \
		GUILE=guile GUILE_CONFIG=guile-config; \
	done

override_dh_auto_build:
	for v in $(PYVERS); do \
	   dh_auto_build --builddirectory=$(BUILDDIR)$$v; \
	done
	# Now configure and build again for C++
	dh_auto_configure --builddirectory=$(BUILDDIR)$$v \
	        -- $(extra_flags) \
		--prefix=/usr \
		--srcdir=$(CURDIR) \
		--enable-shared \
		--with-cxx
	dh_auto_build --builddirectory=$(BUILDDIR)$$v; \

override_dh_clean:
	dh_clean --exclude='*.orig'

override_dh_auto_clean:
	for v in $(PYVERS); do \
	   rm -rf $(BUILDDIR)$$v; \
	done

override_dh_strip:
	dh_strip
	find debian/octave-nlopt/usr/lib -name *.oct | \
		xargs strip --strip-unneeded --remove-section=.comment

override_dh_makeshlibs:
	dh_makeshlibs $(shell for v in $(PYVERS); do printf -- "-X/usr/lib/python$$v/dist-packages "; done)

override_dh_shlibdeps:
	dh_shlibdeps
	dh_octave_substvar

override_dh_auto_install:
	for v in $(PYVERS); do \
	  dh_auto_install --builddirectory=$(BUILDDIR)$$v; \
	done

	dh_numpy
	find debian/tmp/usr/lib/python* \
		-name '*.pyc' -o -name '*.pyo' \
		-o -name '*.la' -o -name '*.a' | xargs rm

override_dh_install:
	dh_install
	# Remove C++ header from C interface
	find debian/libnlopt-dev -name nlopt.hpp -delete

override_dh_link:
	for package in libnlopt-dev libnlopt-guile0 octave-nlopt python-nlopt; do \
		rm -rf debian/$$package/usr/share/doc/$$package; \
	done
	dh_link


override_dh_auto_test:
	for v in $(PYVERS); do \
	   dh_auto_test --builddirectory=$(BUILDDIR)$$v \
	        -- $(extra_flags); \
	done
