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

SO=4
VER=4.0

package=xmpi

version=$(shell expr `pwd` : '.*-\([0-9.]*\)')
version_major=$(shell expr `pwd` : '.*-\([0-9]*\).[0-9.]*')

# Uncomment this to turn on verbose mode. 
#export DH_VERBOSE=1
#export DH_COMPAT=3

ARCH:=$(shell dpkg --print-architecture)

export DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
export DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

OPTFLAGS=-g
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	OPTFLAGS += -O0
else
	OPTFLAGS += -O2
endif

# FOR AUTOCONF 2.52 AND NEWER ONLY
CONFFLAGS =
ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
  CONFFLAGS += --build $(DEB_HOST_GNU_TYPE)
else
  CONFFLAGS += --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
endif

# Other oddities
ifneq (,$(findstring strict,$(DEB_BUILD_OPTIONS)))
	CONFFLAGS += --enable-warnings-are-errors
endif


build: build-arch build-indep
build-arch: build-stamp
build-indep: build-stamp
build-stamp:
	dh_testdir

	dh_autoreconf
	touch configure
	touch -r configure $$(find -name "*.am") $$(find -name "*.in") $$(find -name "*.m4")

	CFLAGS="$(OPTFLAGS)"
	./configure \
                $(CONFFLAGS) \
		--with-cc=mpicc.lam \
		--with-cxx=mpic++.lam \
		--prefix=/usr \
		--enable-shared=yes

	$(MAKE)
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp install-stamp
	! [ -f Makefile ] || $(MAKE) clean
	-rm -f Config/config
	-rm -f 	app-defaults/XMPI.LINUX.$(ARCH) config.status confdefs.h config.log config.mk
	-rm -f src/libxmpi/Makefile src/xmpi/xmpi_config.h src/xmpi/Makefile \
		src/xmpi/stamp-h src/Bitmaps/Makefile src/Pixmaps/Makefile \
		src/Makefile man/man1/Makefile man/Makefile app-defaults/Makefile \
		config/Makefile libtool Makefile 
	-cd debian/conf/ && find * -type f -exec mv {} ../../{} \; 
	rm -rf debian/conf
	rm -rf debian/tmp src/libxmpi/nodeslist.loT src/xmpi/stamp-h1

	dh_autoreconf_clean
	dh_clean

install: install-stamp
install-stamp: build-stamp
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	$(MAKE) install DESTDIR=`pwd`/debian/tmp
	rm -f debian/tmp/usr/lib/libxmpi.so.0.0.0 
	rm -f debian/tmp/usr/lib/libxmpi.so.0
	g++ -shared -Wl,-soname=libxmpi.so.$(SO) src/libxmpi/.libs/*.o \
		-o debian/tmp/usr/lib/libxmpi.so.$(VER) -llam++ -llam
	ln -s libxmpi.so.$(VER) debian/tmp/usr/lib/libxmpi.so.$(SO)
	ln -snf libxmpi.so.$(SO) debian/tmp/usr/lib/libxmpi.so
	rm src/xmpi/xmpi
	cd src/xmpi && make -n | awk '/libtool/ {gsub("[^ ]*libxmpi.la","-L../../debian/tmp/usr/lib -lxmpi"); sub("^.* mpic","mpic");gsub("-o xmpi","-o ../../debian/tmp/usr/bin/xmpi"); print}' | bash -x -e
	mkdir -p debian/tmp/etc/X11/app-defaults
	install -m 0644 app-defaults/XMPI debian/tmp/etc/X11/app-defaults/XMPI
	mkdir -p debian/tmp/usr/share
	dh_movefiles
	touch install-stamp


binary-indep: build install
#	dh_testversion
	dh_testdir -i
	dh_testroot -i
	dh_installdocs -i
	dh_installexamples -i
	dh_installmenu -i
#	dh_installemacsen -i
#	dh_installinit -i
	dh_installcron -i
#	dh_installmanpages -i
#	dh_undocumented
	dh_installchangelogs -i 
	dh_compress -i
	dh_fixperms -i
#	dh_suidregister -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

binary-arch: build install
	dh_testdir -a
	dh_testroot -a
	dh_installdirs -a
	dh_installdocs -a README NEWS
	dh_installexamples -a
	dh_installchangelogs -a
	dh_installmenu -a
	dh_installcron -a
#	dh_installmanpages -pxmpi1-runtime
	dh_strip -a
	dh_compress -a
	dh_fixperms -a
#	dh_suidregister -a
	dh_makeshlibs -a
	dh_shlibdeps -a -l `pwd`/debian/libxmpi4/usr/lib
	dh_gencontrol -a
	dh_installdeb -a
	dh_md5sums -a
	dh_builddeb -a

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

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