#!/usr/bin/make -f


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)
bpath = $(shell octave-config -p LOCALARCHLIBDIR)

BUILDDIR = $(CURDIR)/debian/build

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

override_dh_auto_reconf:
	[ -e bin/scripts ] || mkdir -p bin/scripts
	cp -f /usr/share/misc/config.guess /usr/share/misc/config.sub bin/scripts
	libtoolize --automake
	aclocal -Im4
	autoreconf -i
	automake
 
override_dh_auto_clean:
	for v in $(PYVERS); do \
	   rm -rf $(BUILDDIR)$$v; \
	done
	-rm bin/scripts/config.guess bin/scripts/config.sub
	-rm ltmain.sh octave/nlopt_optimize-oct.o m4/ltversion.m4 m4/libtool.m4 m4/ltoptions.m4 m4/lt~obsolete.m4
	-rm aclocal.m4
	-rm -rf autom4te.cache
	-rm configure
	find . -name Makefile.in | xargs -r rm 

override_dh_auto_configure: override_dh_auto_reconf
	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:
	# to prevent bug in install due to build-%/config.status being deleted after build target is done
	mkdir -p $(CURDIR)/debian/tmp

	for v in $(PYVERS); do \
	   dh_auto_build --builddirectory=$(BUILDDIR)$$v; \
	done

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

	#
	# to prevent bug in install due to build-%/config.status being deleted after build target is done
	dh_prep --exclude=debian/tmp
	find . -name "*.pyc" -exec rm {} \;
	#
	# Fix lintian errors
	dh_numpy
	dh_shlibdeps
	#
	# Fix unstripped-binary-or-object
	strip  --strip-unneeded $(CURDIR)/debian/tmp/usr/lib/libnlopt.so.0*
	strip  --strip-unneeded $(CURDIR)/debian/tmp/usr/lib/libnlopt_guile.so.0.*
	find $(CURDIR)/debian/tmp/usr/lib -name _\*.so | xargs strip --strip-unneeded 
	strip  --strip-unneeded $(CURDIR)/debian/tmp/$(bpath)/nlopt_optimize.oct
	#
	#mkdir -p  $(CURDIR)/debian/tmp/usr/share/lintian/overrides/
	#cp debian/octave-nlopt.lintian-overrides $(CURDIR)/debian/tmp/usr/share/lintian/overrides/octave-nlopt
	#
	find $(CURDIR)/debian/tmp/usr/lib -name \*.la | xargs rm

# Grab the version before +dfsg
DEB_UPSTREAM_VERSION=$(shell dpkg-parsechangelog | sed -rne 's,^Version: ([^+]+).*,\1,p')
get-orig-source:
	uscan --noconf --force-download --rename --download-current-version --destdir=.
	rm -rf nlopt-$(DEB_UPSTREAM_VERSION)
	tar -xf nlopt_$(DEB_UPSTREAM_VERSION).orig.tar.gz
	rm nlopt_$(DEB_UPSTREAM_VERSION).orig.tar.gz
	find nlopt-$(DEB_UPSTREAM_VERSION) -name \*.pdf | xargs rm
	mv nlopt-$(DEB_UPSTREAM_VERSION) nlopt-$(DEB_UPSTREAM_VERSION).orig
	GZIP=--best tar -cz --owner root --group root --mode a+rX \
			-f nlopt_$(DEB_UPSTREAM_VERSION)+dfsg.orig.tar.gz \
			nlopt-$(DEB_UPSTREAM_VERSION).orig
	rm -r nlopt-$(DEB_UPSTREAM_VERSION).orig
