#!/usr/bin/make -f
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

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

src_toplevel = $(shell pwd)

DEST_DIR = $(CURDIR)/debian/mpb/usr
DEST_DIR_MPI = $(CURDIR)/debian/mpb-mpi/usr
BUILD_DIR = $(CURDIR)/debian/build
BUILD_DIR_INV = $(CURDIR)/debian/buildi
BUILD_DIR_MPI = $(CURDIR)/debian/buildm
BUILD_DIR_MPI_INV = $(CURDIR)/debian/buildmi


CONFIG_OPTS = --build=$(DEB_BUILD_GNU_TYPE) \
              --cache-file=../config.cache \
              --prefix=/usr \
              --mandir=\$${prefix}/share/man \
	      --with-libctl=/usr/share/libctl3

plop = $(shell test -f /usr/lib/atlas/liblapack.so && echo plop)
ifeq ($(plop),plop)
  CONFIG_OPTS += --with-lapack=/usr/lib/atlas/liblapack.so
endif

clean:
	dh_testdir
	dh_testroot
	dh_clean debian/config.cache config.sub config.guess stamp-h \
		autotools-stamp *-normal-stamp *-inv-stamp \
		*-mpi-stamp *-mpi-inv-stamp
	rm -rf $(BUILD_DIR) $(BUILD_DIR_INV) \
		$(BUILD_DIR_MPI) $(BUILD_DIR_MPI_INV)

# Configuration

autotools-stamp:
	dh_testdir
	rm -f config.sub config.guess
	ln -s /usr/share/misc/config.sub config.sub
	ln -s /usr/share/misc/config.guess config.guess
	touch autotools-stamp

configure: configure-normal-stamp configure-inv-stamp configure-mpi-stamp configure-mpi-inv-stamp

configure-normal-stamp: autotools-stamp
	dh_testdir
	mkdir -p $(BUILD_DIR)
	( cd $(BUILD_DIR) && F77=gfortran ../../configure $(CONFIG_OPTS) )
	touch configure-normal-stamp
configure-inv-stamp: autotools-stamp
	dh_testdir
	mkdir -p $(BUILD_DIR_INV)
	( cd $(BUILD_DIR_INV) && F77=gfortran ../../configure --with-inv-symmetry $(CONFIG_OPTS) )
	touch configure-inv-stamp
configure-mpi-stamp: autotools-stamp
	dh_testdir
	mkdir -p $(BUILD_DIR_MPI)
	( cd $(BUILD_DIR_MPI) && F77=gfortran ../../configure --with-mpi $(CONFIG_OPTS) )
	touch configure-mpi-stamp
configure-mpi-inv-stamp: autotools-stamp
	dh_testdir
	mkdir -p $(BUILD_DIR_MPI_INV)
	( cd $(BUILD_DIR_MPI_INV) && F77=gfortran ../../configure --with-mpi --with-inv-symmetry $(CONFIG_OPTS) )
	touch configure-mpi-inv-stamp

# Build

build: build-arch build-indep
build-arch: build-normal-stamp build-inv-stamp build-mpi-stamp build-mpi-inv-stamp
build-indep: build-normal-stamp build-inv-stamp build-mpi-stamp build-mpi-inv-stamp

build-normal-stamp: configure-normal-stamp
	dh_testdir
	( cd $(BUILD_DIR) && $(MAKE) )
	touch build-normal-stamp
build-inv-stamp: configure-inv-stamp
	dh_testdir
	( cd $(BUILD_DIR_INV) && $(MAKE) )
	touch build-inv-stamp
build-mpi-stamp: configure-mpi-stamp
	dh_testdir
	( cd $(BUILD_DIR_MPI) && $(MAKE) )
	touch build-mpi-stamp
build-mpi-inv-stamp: configure-mpi-inv-stamp
	dh_testdir
	( cd $(BUILD_DIR_MPI_INV) && $(MAKE) )
	touch build-mpi-inv-stamp

# Installation

install: install-doc install-bin

install-doc:
	dh_testdir
	dh_testroot
	dh_installdirs -i
	dh_install -i

install-bin: build
	dh_testdir
	dh_testroot
	dh_installdirs -a
	( cd $(BUILD_DIR) && $(MAKE) install prefix=$(DEST_DIR) )
	( cd $(BUILD_DIR_INV) && $(MAKE) install prefix=$(DEST_DIR) )
	( cd $(BUILD_DIR_MPI) && $(MAKE) install prefix=$(DEST_DIR_MPI) )
	( cd $(BUILD_DIR_MPI_INV) && $(MAKE) install prefix=$(DEST_DIR_MPI) )
	rm -rf $(DEST_DIR_MPI)/share

# Build architecture-independent files here.
binary-indep: install-doc
	dh_testdir -i
	dh_testroot -i
	dh_installdocs -i
	dh_installexamples -i
	dh_installman -i
	dh_installchangelogs -i ChangeLog
	dh_link -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_perl -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: install-bin
	dh_testdir -a
	dh_testroot -a
	dh_installdocs -a
	dh_installexamples -a
	dh_installmenu -a
	dh_installcron -a
	dh_installman -a
	dh_installchangelogs -a ChangeLog
	dh_link -a
	dh_strip -a
	dh_compress -a
	dh_fixperms -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary: binary-indep binary-arch

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