#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1


# 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


PYVERS=$(shell pyversions -vr)

configure_flags= $(CROSS) --prefix=/usr \
		--enable-boost --enable-shared --with-pic \
		--enable-python --enable-muparser \
		--disable-matlab --disable-superlu \
		--with-mumps="-lsmumps_seq -ldmumps_seq -lcmumps_seq -lzmumps_seq -lpord_seq" \
		CFLAGS="$(CFLAGS)"

disable_scilab_archs   = mips kfreebsd-amd64 kfreebsd-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

# 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)}'`

configure: configure-stamp
configure-stamp:
	dh_testdir
	# Add here commands to configure the package.
	aclocal -I ./m4
	libtoolize -f
	autoheader
	autoreconf
	autoconf
	automake -a --gnu `find . -name Makefile.am | sed -e 's@\./\(.*\)\.am@\1@g'`
	./configure $(configure_flags)

	touch configure-stamp


build: build-stamp
build-stamp: configure-stamp  
	dh_testdir

	# Add here commands to compile the package.
	$(MAKE)
	# Workaround for the other archs without scilab getfem++
	mkdir -p $(CURDIR)/debian/tmp/usr/lib/scilab-getfem++/
	touch $@

clean: 
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp

	# Add here commands to clean up after the build process.
	[ ! -f Makefile ] || $(MAKE) clean

	dh_clean 

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

	# Add here commands to install the package into debian/tmp
	$(MAKE) DESTDIR=$(CURDIR)/debian/tmp install

	# Run the tests, but ignore the test results
	if ! make -C tests check; then \
	  echo "=================== TEST FAILURES, IGNORED FOR THE BUILD ===================="; \
	fi

	# 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++


# Build architecture-independent files here.
binary-indep: install
	dh_testdir
	dh_testroot
	dh_installchangelogs -i ChangeLog
	dh_installdocs -i
	dh_installexamples -i #not really needed
	dh_install -i --list-missing --sourcedir=debian/tmp
#	dh_installman -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: install
	dh_testdir
	dh_testroot
	dh_installchangelogs -a ChangeLog
	dh_installdocs -a
	dh_installexamples -a #not really needed
	dh_install -a --list-missing --sourcedir=debian/tmp
#	dh_installman -a
	dh_link -a
	dh_strip -a --dbg-package=libgetfem++-dbg
	dh_compress -a
	dh_fixperms -a
	dh_pysupport --package=python-getfem++
	dh_makeshlibs -a
	dh_installdeb -a
	LD_LIBRARY_PATH=$(LD_LIBRARY_PATH):$(CURDIR)/debian/tmp/usr/lib/ dh_shlibdeps -a -- --ignore-missing-info
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary: binary-indep binary-arch

.PHONY: build clean binary-indep binary-arch binary install configure
