#!/usr/bin/make -f
%:
	dh $@ --with python2

#=====================================================================
# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

ifneq ($(DEB_HOST_GNU_TYPE),$(DEB_BUILD_GNU_TYPE))
CROSS= --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
else
CROSS= --build $(DEB_BUILD_GNU_TYPE)
endif

export CFLAGS := -Wall $(shell dpkg-buildflags --get CFLAGS)
export CXXFLAGS := -Wall $(shell dpkg-buildflags --get CXXFLAGS)
export CPPFLAGS := $(shell dpkg-buildflags --get CPPFLAGS)
export LDFLAGS := $(shell dpkg-buildflags --get LDFLAGS)

configure_flags= $(CROSS) --prefix=/usr \
		--enable-boost --enable-shared --with-pic \
		--enable-python \
		--disable-matlab --disable-superlu \
		CFLAGS="$(CFLAGS)" \
		CXXFLAGS="$(CXXFLAGS)" \
		CPPFLAGS="$(CPPFLAGS)" \
		LDFLAGS="$(LDFLAGS)" 

disable_scilab_archs   = mips kfreebsd-amd64 kfreebsd-i386 s390x hurd-i386
ifneq (,$(filter $(DEB_HOST_ARCH), $(disable_scilab_archs)))
	configure_flags += --disable-scilab
else
	configure_flags += --enable-scilab --with-scilab-toolbox-dir=$(CURDIR)/debian/tmp/usr/lib/scilab-getfem++/
endif

disable_muparser_archs = hurd-i386
ifneq (,$(filter $(DEB_HOST_ARCH), $(disable_muparser_archs)))
	configure_flags += --disable-muparser
else
	configure_flags += --enable-muparser 
endif

disable_mumps_archs = s390x
ifneq (,$(filter $(DEB_HOST_ARCH), $(disable_mumps_archs)))
	configure_flags += --disable-mumps
else
	configure_flags += --enable-mumps --with-mumps="-lsmumps_seq -ldmumps_seq -lcmumps_seq -lzmumps_seq -lpord_seq"  
endif




# shared library versions, option 1
version=4.1.1
major=5
# option 2, assuming the library is created as src/.libs/libfoo.so.2.0.5 or so
#version=`ls src/.libs/lib*.so.* | \
# awk '{if (match($$0,/[0-9]+\.[0-9]+\.[0-9]+$$/)) print substr($$0,RSTART)}'`
#major=`ls src/.libs/lib*.so.* | \
# awk '{if (match($$0,/\.so\.[0-9]+$$/)) print substr($$0,RSTART+4)}'`

#=====================================================================

override_dh_auto_configure:
	aclocal -I ./m4
	libtoolize -f
	autoheader
	autoreconf
	autoconf
	automake -a --gnu `find . -name Makefile.am | sed -e 's@\./\(.*\)\.am@\1@g'`
	./configure $(configure_flags)


override_dh_auto_build:
	dh_auto_build
	mkdir -p $(CURDIR)/debian/tmp/usr/lib/scilab-getfem++/

override_dh_auto_install:
	dh_auto_install -- DESTDIR=$(CURDIR)/debian/tmp 
	# Workaround for the other archs without scilab getfem++
	mkdir -p $(CURDIR)/debian/tmp/usr/lib/scilab-getfem++/
	mkdir -p $(CURDIR)/debian/scilab-getfem++/usr/share/scilab/contrib/
	cd $(CURDIR)/debian/scilab-getfem++/usr/share/scilab/contrib/; \
	rm -f scilab-getfem++; \
	ln -s ../../../lib/scilab-getfem++/ getfem++

override_dh_install:
	dh_install --list-missing
	dh_numpy

override_dh_installchangelogs:
	dh_installchangelogs ChangeLog

override_dh_strip-arch:
	dh_strip -a --dbg-package=libgetfem++-dbg

override_dh_pysupport-arch:
	dh_pysupport --package=python-getfem++

override_dh_auto_test:
	dh_auto_test || true

override_dh_python2:
	dh_python2 --no-guessing-versions
