#!/usr/bin/make -f
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)



CONFIGURE_OPTIONS=	--host=$(DEB_HOST_GNU_TYPE) \
			--build=$(DEB_BUILD_GNU_TYPE) \
			--prefix=/usr \
			--mandir=\$${prefix}/share/man \
			--infodir=\$${prefix}/share/info \
			--enable-omp

ifneq ($(wildcard /usr/lib/$(DEB_HOST_MULTIARCH)/hdf5/serial/libhdf5.so),)
  export CPPFLAGS += -I/usr/include/hdf5/serial
endif

config-serial.status: 
	dh_testdir
ifneq "$(wildcard /usr/share/misc/config.sub)" ""
	cp -f /usr/share/misc/config.sub config.sub
endif
ifneq "$(wildcard /usr/share/misc/config.guess)" ""
	cp -f /usr/share/misc/config.guess config.guess
endif
	# Add here commands to configure the package.
	-mkdir debian/build-serial
	cd debian/build-serial && \
	../../configure $(CONFIGURE_OPTIONS)


config-mpi.status: 
	dh_testdir
ifneq "$(wildcard /usr/share/misc/config.sub)" ""
	cp -f /usr/share/misc/config.sub config.sub
endif
ifneq "$(wildcard /usr/share/misc/config.guess)" ""
	cp -f /usr/share/misc/config.guess config.guess
endif
	# Add here commands to configure the package.
	-mkdir debian/build-mpi
	cd debian/build-mpi && \
	../../configure $(CONFIGURE_OPTIONS) \
			--enable-mpi

build: build-arch build-indep
build-arch: build-stamp
build-indep: build-stamp
build-stamp: build-stamp-serial build-stamp-mpi
build-stamp-serial: config-serial.status
	dh_testdir

	# Add here commands to compile the package.
	$(MAKE) -C debian/build-serial/
	#/usr/bin/docbook-to-man debian/gpivtools.sgml > gpivtools.1

	touch  $@


build-stamp-mpi: config-mpi.status
	dh_testdir

	# Add here commands to compile the package.
	$(MAKE) -C debian/build-mpi/
	#/usr/bin/docbook-to-man debian/gpivtools.sgml > gpivtools.1

	touch  $@


clean:
	dh_testdir
	dh_testroot
	dh_clean build-stamp* install-stamp*

	# Add here commands to clean up after the build process.
	[ ! -f Makefile ] || $(MAKE) distclean
	-test -r /usr/share/misc/config.sub && \
	 rm -f config.sub
	-test -r /usr/share/misc/config.guess && \
	 rm -f config.guess
	-rm -rf debian/build-serial
	-rm -rf debian/build-mpi
	-rm -rf debian/tmp debian/tmp-mpi
	-rm -rf debian/gpivtools debian/gpivtools-mpi
	dh_clean


install: build-stamp-serial build-stamp-mpi
install-serial: install-stamp-serial
install-stamp-serial: build-stamp-serial
	dh_testdir
	dh_testroot
#	dh_clean -k
#	dh_installdirs

	# Add here commands to install the package into debian/gpivtools.
	install -d $(CURDIR)/debian/tmp
	$(MAKE) -C $(CURDIR)/debian/build-serial/ install \
	prefix=$(CURDIR)/debian/tmp/usr

	dh_install --sourcedir=debian/tmp  -pgpivtools
	touch $@


install-mpi: install-stamp-mpi
install-stamp-mpi: build-stamp-mpi
	dh_testdir
	dh_testroot
#	dh_clean -k
#	dh_installdirs

	# Add here commands to install the package into debian/gpivtools.
	install -d $(CURDIR)/debian/tmp-mpi
	$(MAKE) -C $(CURDIR)/debian/build-mpi/ install \
	prefix=$(CURDIR)/debian/tmp-mpi/usr

	dh_install --sourcedir=debian/tmp-mpi  -pgpivtools-mpi
	touch $@


# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install-serial install-mpi
	$(MAKE) -f debian/rules DH_OPTIONS=-a binary-common

binary-common:
	dh_testdir
	dh_testroot
	dh_installdocs
	dh_installexamples
	dh_installmenu
	dh_installcron
	dh_installman
	dh_installinfo
	dh_installchangelogs ChangeLog
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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

