#! /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.
# Handmodified by P. Frauenfelder for debhelper support, 5 Sept 1998


package=scalapack

version:=$(shell dpkg-parsechangelog | grep Version: | cut -d ' ' -f 2 | cut -d '-' -f 1)
version_major:=$(shell echo $(version) | cut -d '.' -f 1)

topdir=$(shell pwd)

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

include /usr/share/mpi-default-dev/debian_defaults
SCALAPACK_MPI=$(ARCH_DEFAULT_MPI_IMPL)

# This list of archs is maintained separately from that of the mpi-defaults
# package. If there is a mismatch between the two, the package will FTBFS. This
# is on purpose, to avoid silent breakage. See #740620 for more details.
OPENMPI_ARCHS=alpha amd64 arm64 armel armhf hppa hurd-i386 i386 kfreebsd-amd64 kfreebsd-i386 mips mips64el mipsel powerpc powerpcspe ppc64 ppc64el s390x sparc64 x32
MPICH_ARCHS=m68k sh4

build-arch: build

build-indep: build

build: build-$(SCALAPACK_MPI) build-pvm

build-openmpi: build-stamp-openmpi

build-lam: build-stamp-lam

build-mpich: build-stamp-mpich

build-mpich2: build-stamp-mpich2

build-pvm: build-stamp-pvm

build-stamp-openmpi:
	dh_testdir

# next is a clean
	echo *** cleaning object files ***
	BASEDIR=$(topdir) make clean

# build the shared libraries
	echo *** building shared libraries for openmpi ***
	BASEDIR=$(topdir) MPI=openmpi FPIC=-fPIC make lib
	mkdir -p tmp
	set -e ;\
	for i in scalapack ; do \
	  cd tmp ;\
	  ar x ../$${i}_openmpi.a ;\
	  cd .. ;\
	  gcc -shared -Wl,-soname=lib$$i-openmpi.so.$(version_major) -o \
	    lib$$i-openmpi.so.$(version) tmp/*.o -lblas -llapack -lblacsCinit-openmpi -lblacs-openmpi $(shell pkg-config mpi-fort --libs) -lgfortran -lm;\
	  ln -snf lib$$i-openmpi.so.$(version) lib$$i-openmpi.so.$(version_major) ;\
	  ln -snf lib$$i-openmpi.so.$(version_major) lib$$i-openmpi.so ;\
	  rm tmp/* ;\
	done
	rmdir tmp
#	for i in $$(find -name "*.f"); do \
#		if grep '^[^\*].*TOTMEM *= *' $$i | grep -v 64000000 >/dev/null ; then \
#		cat $$i | sed 's,\(^[^\*].*TOTMEM *= *\)[0-9]*,\164000000,g' >tmp ;\
#		mv tmp $$i;\
#		fi;\
#	done

# the testing binaries
	echo *** building static testing binaries for openmpi ***
	BASEDIR=$(topdir) MPI=openmpi BUILD=shared make exe
	set -e ;\
	cd TESTING ;\
	for i in $$(find -name 'x*' -maxdepth 1 ! -name 'x*-lam' ! -name 'x*-mpich' ! -name 'x*-pvm'); do \
	  mv $$i $$i-openmpi ;\
	done

# next is a clean
	echo *** cleaning object files ***
	BASEDIR=$(topdir) make clean

# build the static libraries
	echo *** building static libraries for openmpi ***
	BASEDIR=$(topdir) MPI=openmpi make lib

	touch build-stamp-openmpi

build-stamp-lam:
	dh_testdir

# next is a clean
	echo *** cleaning object files ***
	BASEDIR=$(topdir) make clean

# build the shared libraries
	echo *** building shared libraries for lam ***
	BASEDIR=$(topdir) MPI=lam FPIC=-fPIC make lib
	mkdir -p tmp
	set -e ;\
	for i in scalapack ; do \
	  cd tmp ;\
	  ar x ../$${i}_lam.a ;\
	  cd .. ;\
	  gcc -shared -Wl,-soname=lib$$i-lam.so.$(version_major) -o \
	    lib$$i-lam.so.$(version) tmp/*.o -lblas -llapack -lblacsCinit-lam -lblacs-lam -llam -lgfortran -lm;\
	  ln -snf lib$$i-lam.so.$(version) lib$$i-lam.so.$(version_major) ;\
	  ln -snf lib$$i-lam.so.$(version_major) lib$$i-lam.so ;\
	  rm tmp/* ;\
	done
	rmdir tmp
#	for i in $$(find -name "*.f"); do \
#		if grep '^[^\*].*TOTMEM *= *' $$i | grep -v 64000000 >/dev/null ; then \
#		cat $$i | sed 's,\(^[^\*].*TOTMEM *= *\)[0-9]*,\164000000,g' >tmp ;\
#		mv tmp $$i;\
#		fi;\
#	done

# the testing binaries
	echo *** building static testing binaries for lam ***
	BASEDIR=$(topdir) MPI=lam BUILD=shared make exe
	set -e ;\
	cd TESTING ;\
	for i in $$(find -name 'x*' -maxdepth 1 ! -name 'x*-openmpi' ! -name 'x*-mpich' ! -name 'x*-pvm'); do \
	  mv $$i $$i-lam ;\
	done

# next is a clean
	echo *** cleaning object files ***
	BASEDIR=$(topdir) make clean

# build the static libraries
	echo *** building static libraries for lam ***
	BASEDIR=$(topdir) MPI=lam make lib

	touch build-stamp-lam

build-stamp-mpich:
	dh_testdir

# next is a clean
	echo *** cleaning object files ***
	BASEDIR=$(topdir) make clean

# build the shared libraries
	echo *** building shared libraries for mpich ***
	BASEDIR=$(topdir) MPI=mpich FPIC=-fPIC make lib
	mkdir -p tmp
	set -e ;\
	for i in scalapack ; do \
	  cd tmp ;\
	  ar x ../$${i}_mpich.a ;\
	  cd .. ;\
	  gcc -shared -Wl,-soname=lib$$i-mpich.so.$(version_major) -o \
	    lib$$i-mpich.so.$(version) tmp/*.o -lblas -llapack -lblacsCinit-mpich -lblacs-mpich -L /usr/lib/mpich/lib/shared -lmpich -lgfortran -lm;\
	  ln -snf lib$$i-mpich.so.$(version) lib$$i-mpich.so.$(version_major) ;\
	  ln -snf lib$$i-mpich.so.$(version_major) lib$$i-mpich.so ;\
	  rm tmp/* ;\
	done
	rmdir tmp

# the testing binaries
	echo *** building static testing binaries for mpich ***
	BASEDIR=$(topdir) MPI=mpich BUILD=shared make exe
	set -e ;\
	cd TESTING ;\
	for i in $$(find -name 'x*'  -maxdepth 1 ! -name 'x*-openmpi' ! -name 'x*-lam' ! -name 'x*-pvm'); do \
	  mv $$i $$i-mpich ;\
	done

# next is a clean
	echo *** cleaning object files ***
	BASEDIR=$(topdir) make clean

# build the static libraries
	echo *** building static libraries for mpich ***
	BASEDIR=$(topdir) MPI=mpich make lib

	touch build-stamp-mpich

build-stamp-mpich2:
	dh_testdir

# next is a clean
	echo *** cleaning object files ***
	BASEDIR=$(topdir) make clean

# build the shared libraries
	echo *** building shared libraries for mpich2 ***
	BASEDIR=$(topdir) MPI=mpich2 FPIC=-fPIC make lib
	mkdir -p tmp
	set -e ;\
	for i in scalapack ; do \
	  cd tmp ;\
	  ar x ../$${i}_mpich2.a ;\
	  cd .. ;\
	  gcc -shared -Wl,-soname=lib$$i-mpich2.so.$(version_major) -o \
	    lib$$i-mpich2.so.$(version) tmp/*.o -lblas -llapack -lblacsCinit-mpich2 -lblacs-mpich2 -lmpich -lmpl -lgfortran -lm;\
	  ln -snf lib$$i-mpich2.so.$(version) lib$$i-mpich2.so.$(version_major) ;\
	  ln -snf lib$$i-mpich2.so.$(version_major) lib$$i-mpich2.so ;\
	  rm tmp/* ;\
	done
	rmdir tmp
#	for i in $$(find -name "*.f"); do \
#		if grep '^[^\*].*TOTMEM *= *' $$i | grep -v 64000000 >/dev/null ; then \
#		cat $$i | sed 's,\(^[^\*].*TOTMEM *= *\)[0-9]*,\164000000,g' >tmp ;\
#		mv tmp $$i;\
#		fi;\
#	done

# the testing binaries
	echo *** building static testing binaries for mpich2 ***
	BASEDIR=$(topdir) MPI=mpich2 BUILD=shared make exe
	set -e ;\
	cd TESTING ;\
	for i in $$(find -name 'x*' -maxdepth 1 ! -name 'x*-openmpi' ! -name 'x*-mpich*' ! -name 'x*-pvm'); do \
	  mv $$i $$i-mpich2 ;\
	done

# next is a clean
	echo *** cleaning object files ***
	BASEDIR=$(topdir) make clean

# build the static libraries
	echo *** building static libraries for mpich2 ***
	BASEDIR=$(topdir) MPI=mpich2 make lib

	touch build-stamp-mpich2

build-stamp-pvm:
	dh_testdir

# next is a clean
	echo *** cleaning object files ***
	BASEDIR=$(topdir) make clean

# build the shared libraries
	echo *** building shared libraries for pvm ***
	BASEDIR=$(topdir) MPI=pvm FPIC=-fPIC make lib
	mkdir -p tmp
	set -e ;\
	for i in scalapack ; do \
	  cd tmp ;\
	  ar x ../$${i}_pvm.a ;\
	  cd .. ;\
	  gcc -shared -Wl,-soname=lib$$i-pvm.so.$(version_major) -o \
	    lib$$i-pvm.so.$(version) tmp/*.o -lblas -llapack -lblacs-pvm -lpvm3 -lgfortran -lm;\
	  ln -snf lib$$i-pvm.so.$(version) lib$$i-pvm.so.$(version_major) ;\
	  ln -snf lib$$i-pvm.so.$(version_major) lib$$i-pvm.so ;\
	  rm tmp/* ;\
	done
	rmdir tmp

# the testing binaries
	echo *** building static testing binaries for pvm ***
	BASEDIR=$(topdir) MPI=pvm BUILD=shared make exe
	set -e ;\
	cd TESTING ;\
	for i in $$(find -name 'x*'  -maxdepth 1 ! -name 'x*-openmpi' ! -name 'x*-lam' ! -name 'x*-mpich*'); do \
	  mv $$i $$i-pvm ;\
	done

# next is a clean
	echo *** cleaning object files ***
	BASEDIR=$(topdir) make clean

# build the static libraries
	echo *** building static libraries for pvm ***
	BASEDIR=$(topdir) MPI=pvm make lib

	touch build-stamp-pvm

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp-* install-stamp-* install-dirs-stamp
	BASEDIR=$(topdir) make clean
	rm -f *.a
	rm -f *so*
	rm -f TESTING/x*
	rm -fr tmp
	rm -f PBLAS/TIMING/PB_Cabort.c PBLAS/TIMING/PB_Cwarn.c PBLAS/TESTING/PB_Cabort.c PBLAS/TESTING/PB_Cwarn.c
#	for i in $$(find -name "*.f"); do \
#		if grep '^[^\*].*TOTMEM *= *' $$i | grep 64000000 >/dev/null ; then\
#		cat $$i | sed -e 's/\(^[^\*].*TOTMEM *= *\)[0-9]*,/\12000000,/g' -e 's/\(^[^\*].*TOTMEM *= *\)[0-9]*\( *)\)/\13000000\2/g'>tmp ;\
#		mv tmp $$i;\
#		fi;\
#	done
	dh_clean

	{ \
	  echo "# This file is autogenerated. DO NOT EDIT!" ; \
	  echo "#" ; \
	  echo "# Modifications should be made to debian/control.in instead." ; \
	  echo "# This file is regenerated automatically in the clean target." ; \
	  echo ; \
	  sed "s/@OPENMPI_ARCHS@/$(OPENMPI_ARCHS)/g;s/@MPICH_ARCHS@/$(MPICH_ARCHS)/g;" debian/control.in ; } \
	> debian/control

install: install-dirs install-$(SCALAPACK_MPI) install-pvm

install-dirs: install-dirs-stamp

install-dirs-stamp:
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs -v

	touch install-dirs-stamp

install-openmpi: install-stamp-openmpi

install-lam: install-stamp-lam

install-mpich: install-stamp-mpich

install-mpich2: install-stamp-mpich2

install-pvm: install-stamp-pvm

install-stamp-openmpi: build-stamp-openmpi
	dh_testdir
	dh_testroot

	# The shared library is installed via debian/*.install, to avoid
	# creating an empty package if default MPI implementation changes on
	# the arch
	dh_install

	set -e ;\
	for i in scalapack ; do \
	  cp -a lib$$i-openmpi.so \
	    `pwd`/debian/libscalapack-mpi-dev/usr/lib/$(DEB_HOST_MULTIARCH)/ ;\
	done

	install TESTING/x*-openmpi \
	  `pwd`/debian/scalapack-mpi-test/usr/lib/scalapack

	set -e ;\
	for i in scalapack ; do \
	  install $${i}_openmpi.a \
	    `pwd`/debian/libscalapack-mpi-dev/usr/lib/$(DEB_HOST_MULTIARCH)/lib$$i-openmpi.a ;\
	done

	touch install-stamp-openmpi

install-stamp-lam: build-stamp-lam
	dh_testdir
	dh_testroot

	set -e ;\
	for i in scalapack ; do \
	  cp -a lib$$i-lam.so.* \
	    `pwd`/debian/libscalapack-mpi1/usr/lib/$(DEB_HOST_MULTIARCH)/ ;\
	  cp -a lib$$i-lam.so \
	    `pwd`/debian/libscalapack-mpi-dev/usr/lib/$(DEB_HOST_MULTIARCH)/ ;\
	done

	install TESTING/x*-lam \
	  `pwd`/debian/scalapack-mpi-test/usr/lib/scalapack

	set -e ;\
	for i in scalapack ; do \
	  install $${i}_lam.a \
	    `pwd`/debian/libscalapack-mpi-dev/usr/lib/$(DEB_HOST_MULTIARCH)/lib$$i-lam.a ;\
	done

	touch install-stamp-lam

install-stamp-mpich: build-stamp-mpich
	dh_testdir
	dh_testroot

	# The shared library is installed via debian/*.install, to avoid
	# creating an empty package if default MPI implementation changes on
	# the arch
	dh_install

	set -e ;\
	for i in scalapack ; do \
	  cp -a lib$$i-mpich.so \
	    `pwd`/debian/libscalapack-mpi-dev/usr/lib/$(DEB_HOST_MULTIARCH)/ ;\
	done

	install TESTING/x*-mpich \
	  `pwd`/debian/scalapack-mpi-test/usr/lib/scalapack

	set -e ;\
	for i in scalapack ; do \
	  install $${i}_mpich.a \
	    `pwd`/debian/libscalapack-mpi-dev/usr/lib/$(DEB_HOST_MULTIARCH)/lib$$i-mpich.a ;\
	done

	touch install-stamp-mpich

install-stamp-mpich2: build-stamp-mpich2
	dh_testdir
	dh_testroot

	set -e ;\
	for i in scalapack ; do \
	  cp -a lib$$i-mpich2.so.* \
	    `pwd`/debian/libscalapack-mpi1/usr/lib/$(DEB_HOST_MULTIARCH)/ ;\
	  cp -a lib$$i-mpich2.so \
	    `pwd`/debian/libscalapack-mpi-dev/usr/lib/$(DEB_HOST_MULTIARCH)/ ;\
	done

	install TESTING/x*-mpich2 \
	  `pwd`/debian/scalapack-mpi-test/usr/lib/scalapack

	set -e ;\
	for i in scalapack ; do \
	  install $${i}_mpich2.a \
	    `pwd`/debian/libscalapack-mpi-dev/usr/lib/$(DEB_HOST_MULTIARCH)/lib$$i-mpich2.a ;\
	done

	touch install-stamp-mpich2

install-stamp-pvm: build-stamp-pvm
	dh_testdir
	dh_testroot

	set -e ;\
	for i in scalapack ; do \
	  cp -a lib$$i-pvm.so.* \
	    `pwd`/debian/libscalapack-pvm1/usr/lib/$(DEB_HOST_MULTIARCH)/ ;\
	  cp -a lib$$i-pvm.so \
	    `pwd`/debian/libscalapack-pvm-dev/usr/lib/$(DEB_HOST_MULTIARCH)/ ;\
	done

	install TESTING/x*-pvm \
	  `pwd`/debian/scalapack-pvm-test/usr/lib/scalapack

	set -e ;\
	for i in scalapack ; do \
	  install $${i}_pvm.a \
	    `pwd`/debian/libscalapack-pvm-dev/usr/lib/$(DEB_HOST_MULTIARCH)/lib$$i-pvm.a ;\
	done

	touch install-stamp-pvm

binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installdocs README
	dh_installexamples
	dh_installchangelogs
	dh_link
	dh_compress
	dh_fixperms
	dh_strip
	dh_makeshlibs -V
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

install-indep: install-stamp-indep

install-stamp-indep:
	dh_testdir -i
	dh_testroot -i
	dh_installdirs -i

	set -e ;\
	for i in $$(find TESTING -name '*.dat') ; do \
	  install -m 644 $$i \
	    `pwd`/debian/scalapack-test-common/usr/share/scalapack ;\
	done
	set -e ;\
	for i in $$(find REDIST/TESTING -name '*.dat') ; do \
	  install -m 644 $$i \
	    `pwd`/debian/scalapack-test-common/usr/share/scalapack ;\
	done
	set -e ;\
	for i in $$(find PBLAS -name '*.dat') ; do \
	  install -m 644 $$i \
	    `pwd`/debian/scalapack-test-common/usr/share/scalapack ;\
	done

	touch install-stamp-indep

binary-indep: install-indep
	dh_testdir -i
	dh_testroot -i
	dh_installdocs -i README
	dh_installchangelogs -i
	dh_link -i
	dh_compress -i
	dh_strip -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

source diff:
	@echo >&2 'source and diff are obsolete - use dpkg-source -b'; false

binary: binary-arch binary-indep

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