#!/usr/bin/make -f
# Made with the aid of debmake, by Christoph Lameter,
# based on the sample debian/rules file for GNU hello by Ian Jackson.

DEB_VERSION := $(shell dpkg-parsechangelog -S Version)
DEB_NOEPOCH_VERSION := $(shell echo $(DEB_VERSION) | cut -d: -f2-)
DEB_UPSTREAM_VERSION := $(shell echo $(DEB_NOEPOCH_VERSION) | sed 's/-[^-]*$$//')

HYPRE_UPSTREAM_VERSION=$(DEB_UPSTREAM_VERSION)
HYPRE_X_Y_VERSION=$(shell echo $(HYPRE_UPSTREAM_VERSION) | sed "s/^\(.*\)\.\([^.]*\)$$/\1/")
HYPRE_Z_VERSION=$(shell echo $(HYPRE_UPSTREAM_VERSION) | sed "s/^\(.*\)\.\([^.]*\)$$/\2/")
HYPRE_NEXT_VERSION=$(HYPRE_X_Y_VERSION).$(shell echo $$(( $(HYPRE_Z_VERSION) + 1 )) )

export HYPRE_SOVERSION=$(HYPRE_UPSTREAM_VERSION)

export DEB_CFLAGS_MAINT_APPEND  = -Wall -pedantic -O3
export DEB_CXXFLAGS_MAINT_APPEND  = -Wall -pedantic -O3
export DEB_LDFLAGS_MAINT_APPEND = -Wl,--no-as-needed
export DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
export DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)

%:
	dh $@ --sourcedirectory=src --with autoreconf

# to avoid lintian warnings
CPPFLAGS:=$(shell dpkg-buildflags --get CPPFLAGS)
CFLAGS:=$(shell dpkg-buildflags --get CFLAGS)
CXXFLAGS:=$(shell dpkg-buildflags --get CXXFLAGS)
LDFLAGS:=$(shell dpkg-buildflags --get LDFLAGS)

# extra flags set to be similar to what petsc requires
extra_flags += \
  --with-fei --with-mli \
  --with-superlu --with-superlu-include=/usr/include/superlu --with-superlu-lib="-lsuperlu" \
  --with-dsuperlu --with-dsuperlu-include=/usr/include/superlu-dist --with-dsuperlu-lib="-lsuperlu_dist" \
  --with-blas=yes --with-blas-lib="$(shell pkg-config --libs blas)" \
  --with-lapack=yes \
	--with-lapack-lib="$(shell pkg-config --libs lapack)" \
	--with-fmangle-lapack="one-underscore"   \
  --with-MPI-include=$(shell pkg-config --cflags-only-I mpi | awk '{print $$1}' | sed "s/^-I//") \
  --with-MPI-libs="$(shell pkg-config --libs-only-l mpi | sed 's/-l//g')" \
  --with-MPI-lib-dirs="$(shell pkg-config --libs-only-L mpi | sed 's/-L//g')" \
  --with-MPI-flags="$(shell pkg-config --libs-only-other mpi)" \
  --with-extra-flags="$(shell pkg-config --cflags-only-other mpi)" \
  --prefix=$(CURDIR)/debian/tmp/usr

# To get petsc support when indexTypes.integerSize == 64
#ifeq ($(DEB_HOST_ARCH),amd64)
#   extra_flags +=  --enable-bigint
#endif

# from pets:
# # explicitly tell hypre BLAS/LAPACK mangling since it may not match Fortran mangling
#    if self.blasLapack.mangling == 'underscore':
#      mang = 'one-underscore'
#    elif self.blasLapack.mangling == 'caps':
#      mang = 'caps-no-underscores'
#    else:
#      mang = 'no-underscores'
#    args.append('--with-fmangle-blas='+mang)
#    args.append('--with-fmangle-lapack='+mang)


# Other options from src/configure
  # --enable-debug          Set compiler flags for debugging.
  # --enable-shared         Build shared libraries (default is NO).
  # --enable-bigint         Use long long int for HYPRE_Int (default is NO).
  # --enable-complex        Use complex values (default is NO).
  # --enable-maxdim=MAXDIM  Change max dimension size to MAXDIM (default is 3).
  #                         Currently must be at least 3.
  # --enable-persistent     Uses persistent communication (default is NO).
  # --enable-hopscotch      Uses hopscotch hashing if configured with OpenMP and
  #                         atomic capability available(default is NO).
  # --enable-global-partition
  #                         Use global partitioning (default is NO).
  # --enable-fortran        Require a working Fortran compiler (default is YES).

export OMPI_MCA_orte_rsh_agent=/bin/false

override_dh_clean:
	dh_clean
	rm -f debian/libhypre.shlibs

override_dh_auto_clean:
	echo "Running clean"
	rm -rf $(CURDIR)/debian/tmp
	rm -f src/configure.in
	rm -f src/config.log
	rm -f src/config/config.guess
	rm -f src/config/config.sub
	cd src/test; \
	for TEST in TEST_*; do \
	  rm -f $${TEST}.stdout $${TEST}.stderr; \
	done
	-dh_auto_clean

override_dh_autoreconf:
	cp /usr/share/misc/config.* src/config/
	cd src && ./config/bootstrap

override_dh_auto_configure:
	dh_auto_configure -- $(extra_flags) --enable-shared
	#dh_auto_configure -p libhypre-complex -- $(extra_flags) --enable-shared --enable-complex


# disable build tests for these arches:
# ia64 consistently times out on tests
# armel occasionally times out
ARCH_NO_TEST_LIST = armel ia64

empty :=
space := $(empty)$(empty)

# run tests, or not
RUNTEST=yes
ifneq (,$(findstring $(space)$(DEB_HOST_ARCH)$(space), $(space)$(ARCH_NO_TEST_LIST)$(space)))
  RUNTEST=no
endif
# don't run tests if the nocheck build option has been specified
ifeq (nocheck,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
  RUNTEST=no
endif

override_dh_auto_test:
	set -e; \
	if [ "$(RUNTEST)" = "no" ]; then \
	  echo Tests have been disabled on $(DEB_HOST_ARCH); \
	else echo "Building tests"; \
	  $(MAKE) -C src test VERBOSE=1; \
	  echo "Running tests"; \
	  set -e; \
	  export LD_LIBRARY_PATH=$(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH):$(LD_LIBRARY_PATH); \
	  cd src/test; \
	  test_return="ok"; \
	  for TEST in TEST_*; do \
	    echo -n "running $${TEST} ... "; \
	    if ./runtest.sh -t $${TEST}/*.sh >$${TEST}.stdout 2>$${TEST}.stderr ; then \
	      echo ok; \
	    else \
	      test_return=$$?; \
	      echo "error found with error code $${test_return}"; \
	      echo "===== error logs for $${TEST} ====="; \
	      echo "$${TEST} stdout:"; \
	      cat $${TEST}.stdout; \
	      echo "$${TEST} stderr:"; \
	      cat $${TEST}.stderr; \
	      echo "===== end error logs for $${TEST} with error code $${test_return} ====="; \
	    fi; \
	  done; \
	  if [ $${test_return} != "ok" ]; then \
	      echo "Tests failed with last error code $${test_return}"; \
	      return $${test_return}; \
	  fi; \
	fi

# upstream places the soname in front of .so
# which makes it trickier than desirable to exclude
# the versioned so files in a libhypre-dev.install file
# Easier to just override instead
override_dh_install:
	dh_install -p libhypre usr/lib/*/*${HYPRE_SOVERSION}.so
	dh_install -p libhypre-dev --exclude=${HYPRE_SOVERSION} usr/lib/*/*
	dh_install -p libhypre-dev usr/include/hypre

override_dh_makeshlibs:
	sed "s/__HYPRE_VERSION__/$(HYPRE_SOVERSION)/g; s/__HYPRE_VERSION_NEXT__/$(HYPRE_NEXT_VERSION)/g" debian/libhypre.shlibs.in > debian/libhypre.shlibs
	dh_makeshlibs

# run examples (add -W, -no-as-needed on Unbuntu for ex5f)
#	echo "Building examples"
#	$(MAKE) -C src/examples \
#          HYPRE_LIB_INSTALL=$(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH) \
#          HYPRE_INC_INSTALL=$(CURDIR)/debian/tmp/usr/include/hypre
#	ALLPROGS="ex1 ex2 ex3 ex4 ex5 ex5f \
#          ex6 ex7 ex8 ex9 ex11 ex12 ex12f" ; \
#	for ex in $$ALLPROGS; do \
#	   echo "Running $$ex"; \
#	   ls -l src/examples/$$ex; \
#	   mpirun -np 2 src/examples/$$ex ; \
#	done
#	ALLPROGS="ex13 ex14" ; \
#	for ex in $$ALLPROGS; do \
#	   echo "Running $$ex"; \
#	   mpirun -np 3 src/examples//$$ex ; \
#	done
#	ALLPROGS="ex15" ; \
#	for ex in $$ALLPROGS; do \
#	   echo "Running $$ex"; \
#	   mpirun -np 8 src/examples//$$ex ; \
#	done
#	ALLPROGS="ex16" ; \
#	for ex in $$ALLPROGS; do \
#	   echo "Running $$ex"; \
#	   mpirun -np 4 src/examples//$$ex ; \
#	done
#

# run testsuite

#	cd src/test; \
#	for file in `find . -name \*.sh | egrep "^./TEST" | egrep -v "fei|babel|timing"`; do \
#	   echo "./runtest.sh $$file" \
#	   ./runtest.sh $$file; \
#	done; \
#	cd ../..
