#!/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.

package=hypre

# This tells us which MPI implementation is in use
HYPRE_MPI=$(shell readlink /etc/alternatives/mpi | sed s/usr//g | sed s/include//g | sed s/lib//g | sed s/\\///g)

# Files removed by the clean target
KEEP_FILES= \
  src/FEI_mv/DSuperLU/SRC/dmemory.c.orig \
  src/FEI_mv/DSuperLU/SRC/memory.c.orig \
  src/babel-runtime/Makefile.in \
  src/babel-runtime/aclocal.m4 \
  src/babel-runtime/bin/Makefile.in \
  src/babel-runtime/config/Makefile.in \
  src/babel-runtime/config/config.guess \
  src/babel-runtime/config/config.sub \
  src/babel-runtime/config/ltmain.sh \
  src/babel-runtime/java/Makefile.in \
  src/babel-runtime/m4/Makefile.in \
  src/babel-runtime/m4/libtool.m4 \
  src/babel-runtime/m4/ltdl.m4 \
  src/babel-runtime/sidl/Makefile.in \
  src/babel-runtime/sidl/ltdl.c \
  src/babel-runtime/sidl/ltdl.h \
  src/babel-runtime/sidlx/Makefile.in \
  src/config/Makefile.config \
  src/config/config.guess \
  src/config/config.sub \
  src/configure

build: build-arch
build-arch: stamp-build-arch
stamp-build-arch: patch
	dh_testdir
	if [ ! -e src/parcsr_ls/flexgmres.c ]; then \
	  ln -s ../krylov/HYPRE_flexgmres.c ../krylov/flexgmres.c src/parcsr_ls/; \
	fi
	if [ ! -e src/configure.keep ]; then \
	  for keepfile in $(KEEP_FILES); do \
	    mv $$keepfile $$keepfile.keep; \
	  done; \
	fi
	rm -f src/configure
	(cd src/config && aclocal -I .)
	(cd src && autoconf -I config config/configure.in > configure)
	chmod +x src/configure
	cp -f /usr/share/automake*/config.* src/config/
	(cd src/babel-runtime && libtoolize)
	cp -f src/babel-runtime/libltdl/ltdl.* src/babel-runtime/sidl/
	rm -f src/babel-runtime/config/config.*
	(cd src/babel-runtime && aclocal -I m4)
	(cd src/babel-runtime && automake -a)
	(cd src && ./configure --enable-shared --without-superlu \
	  --with-blas-libs="blas" --with-blas-lib-dirs="/usr/lib" \
	  --with-lapack-libs="lapack" --with-lapack-lib-dirs="/usr/lib")
	$(MAKE) -C src
#	Remove PIC object files
#	rm -f `find . -name \*.o -print`
#	(cd src && ./configure --without-superlu \
#	  --with-blas-libs="blas-3" --with-blas-lib-dirs="/usr/lib" \
#	  --with-lapack-libs="lapack-3" --with-lapack-lib-dirs="/usr/lib")
#	$(MAKE) -C src
	touch $@

build-indep: build-arch

clean:
	dh_testdir
# Move these aside to avoid stomping on them during the clean process
	if [ ! -e src/configure.keep ]; then \
	  for keepfile in $(KEEP_FILES); do \
	    mv $$keepfile $$keepfile.keep; \
	  done; \
	fi
	if [ -e src/Makefile ]; then \
	  touch src/config/Makefile.config; \
	  $(MAKE) -C src clean; fi
	rm -f stamp-configure stamp-build-arch stamp-install	
	rm -f src/config/aclocal.m4 src/configure src/config/config.guess \
	  src/config/config.sub src/config/Makefile.config
	rm -f src/babel-runtime/Makefile.in src/babel-runtime/aclocal.m4 \
	  src/babel-runtime/bin/Makefile.in src/babel-runtime/config/Makefile.in \
	  src/babel-runtime/config/config.guess src/babel-runtime/config/config.sub \
	  src/babel-runtime/config/ltmain.sh src/babel-runtime/java/Makefile.in \
	  src/babel-runtime/m4/Makefile.in src/babel-runtime/m4/argz.m4 \
	  src/babel-runtime/m4/ltoptions.m4 src/babel-runtime/m4/libtool.m4 \
	  src/babel-runtime/m4/ltdl.m4 src/babel-runtime/m4/ltsugar.m4 \
	  src/babel-runtime/m4/ltversion.m4 src/babel-runtime/m4/lt~obsolete.m4 \
	  src/babel-runtime/sidl/Makefile.in src/babel-runtime/sidl/ltdl.c \
	  src/babel-runtime/sidl/ltdl.h src/babel-runtime/sidlx/Makefile.in
	rm -f src/config.log src/config.status src/HYPRE_config.h
	rm -f src/parcsr_ls/flexgmres.c src/parcsr_ls/HYPRE_flexgmres.c
	rm -rf src/babel-runtime/libltdl
	dh_clean
	if [ -e src/configure.keep ]; then \
	  for keepfile in $(KEEP_FILES); do \
	    mv -f $$keepfile.keep $$keepfile; \
	  done; \
	fi

install: stamp-install
stamp-install: stamp-build-arch
	install -d `pwd`/debian/tmp/usr/include
	install -d `pwd`/debian/tmp/usr/lib
	$(MAKE) -C src install HYPRE_INSTALL_DIR=`pwd`/debian/tmp/usr \
	  prefix=`pwd`/debian/tmp/usr exec_prefix=`pwd`/debian/tmp/usr
	touch $@

binary-indep: install
	dh_testdir -i
	dh_testroot -i
	dh_installdirs -i
# -a has to come before -i here because -i has ambiguous names
	dh_movefiles -a
	dh_movefiles -i
	dh_installdocs -i
	dh_installchangelogs -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

binary-arch: install
	dh_testdir -a
	dh_testroot -a
	dh_installdirs -a
	dh_movefiles -a
	dh_installdocs -a
	dh_installchangelogs -a
	dh_strip -a
	dh_makeshlibs -a
	dh_compress -a
	dh_fixperms -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary: binary-arch binary-indep

.PHONY: patch unpatch binary binary-arch binary-indep install build clean
