#!/usr/bin/make -f
# Made with the aid of dh_make, by Craig Small
# Sample debian/rules that uses debhelper. GNU copyright 1997 by Joey Hess.
# This version is for a hypothetical package that builds an
# architecture-dependant package, as well as an architecture-independant
# package.

SO=4
MIN=0
VER=$(SO).$(MIN)

CC=cc
package=lam

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=2

TDIR:=$(shell pwd)

MEXT=tmp
MDIR:=debian/$(MEXT)
LAMV:=usr/lib/lam
LAMD:=$(MDIR)/$(LAMV)
OLDIR=$(LAMD)/lib
OBDIR=$(LAMD)/bin
SHLDIR=$(subst $(MEXT),shared,$(OLDIR))
SHBDIR=$(subst $(MEXT),shared,$(OBDIR))
SHIDIR:=$(SHBDIR)/../include
STLDIR=$(subst $(MEXT),static,$(OLDIR))
STBDIR=$(subst $(MEXT),static,$(OBDIR))
LDIR:=$(MDIR)/usr/lib
BDIR:=$(MDIR)/usr/bin
WDIR:=tools/wrappers
MPROX:=$(MDIR)/usr/share/man/man3/MPI_Wtime.3
MSRC:=debian/static/$(LAMV)/man/man3/MPI_Wtime.3
mans:=lamhalt lamnodes
MMDIR:=debian/$(MEXT)/usr/share/man/man1
MMAN:=$(addprefix $(MMDIR)/,$(addsuffix .1,$(mans)))
SUB:=$(shell find * -name config.sub |grep -v debian)
GUESS:=$(shell find * -name config.guess |grep -v debian)
#CONFIGURE:=$(shell find * -name configure)
DSUB:=$(addprefix debian/conf/,$(SUB))
DGUESS:=$(addprefix debian/conf/,$(GUESS))

export DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
export DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
export DEB_HOST_GNU_SYSTEM ?= $(shell dpkg-architecture -qDEB_HOST_GNU_SYSTEM)
export DEB_HOST_MULTIARCH  ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

ARCHT:=$(shell echo $(DEB_HOST_GNU_TYPE) | cut -f1 -d-)
OPTFLAGS:=
ifeq ($(ARCHT),m68k)
OPTFLAGS:=-O2
endif

dlibs:= lam lamf77mpi lammpi++ mpi lammpio 
stlibs:=$(addprefix $(STLDIR)/lib,$(addsuffix .a,$(dlibs)))
shlibs:=$(addprefix $(SHLDIR)/lib,$(addsuffix .a,$(dlibs)))

tlibs:=lam  lamio lam++
libs: $(addprefix $(LDIR)/lib,$(addsuffix .so,$(tlibs)) $(addsuffix .a,$(tlibs))) 

dbins:=lamclean lamexec lamgrow lamshrink lamtrace mpimsg mpirun mpiexec mpitask lamd tping \
	hboot lamboot laminfo lamhalt lamnodes recon tkill lamwipe   mpicc mpif77 mpic++
nbins:=lamclean lamexec lamgrow lamshrink lamtrace mpimsg mpirun.lam mpiexec.lam mpitask lamd tping \
	hboot lamboot laminfo lamhalt lamnodes recon tkill lamwipe  mpicc.lam\
	 mpif77.lam mpic++.lam

tbins:=$(addprefix $(BDIR)/,$(nbins))
shbins:=$(addprefix $(SHBDIR)/,$(dbins)))
bins: $(tbins)

$(DSUB):
	mkdir -p $(@D)
	cp $(subst debian/conf/,,$@) $@

$(SUB): $(DSUB)
	chmod +w $@
	cp /usr/share/misc/config.sub $@

$(DGUESS):
	mkdir -p $(@D)
	cp $(subst debian/conf/,,$@) $@

$(GUESS): $(DGUESS)
	chmod +w $@
	cp /usr/share/misc/config.guess $@

#$(CONFIGURE): FORCE
#	! [ -r $(@D)/configure.in ] || touch $(@D)/configure.in
#	sleep 2
#	! [ -r $(@D)/aclocal.m4 ] || touch $(@D)/aclocal.m4
#	sleep 2
#	touch $@

FORCE:

$(stlibs): $(SUB) $(GUESS) #$(CONFIGURE)

	[ ! -f Makefile ] || $(MAKE) distclean
#	-$(MAKE) clean

	CC="gcc" CXX="g++" F77="gfortran" FC="gfortran" \
	CFLAGS="$(OPTFLAGS) -std=gnu17" CXXFLAGS="$(OPTFLAGS) -std=gnu17" CPPFLAGS="-std=gnu17" FFLAGS="$(OPTFLAGS)" \
	./configure --prefix=/$(LAMV) \
		     --build=$(DEB_BUILD_GNU_TYPE) \
		     --host=$(DEB_HOST_GNU_TYPE) \
		     --with-rsh=/usr/bin/rsh \
		     --with-romio-flags=-ar_nolocal \
		     --with-rpi=sysv 

	awk '/LAM_HAVE_OPENPTY/ {gsub("1","0")} /LAM_HAVE_SYSV_PTYS/ {gsub("1","0")}  {print}' \
		share/include/lam_config.h >tmp && mv tmp share/include/lam_config.h

	$(MAKE) 

	$(MAKE) install DESTDIR=$(subst $(LAMV)/lib,,$(TDIR)/$(@D))


$(MSRC) $(SHIDIR)/mpi.h $(shbins) $(shlibs): $(SUB) $(GUESS) #$(CONFIGURE)

	[ ! -f Makefile ] || $(MAKE) distclean
#	-$(MAKE) clean

	CC="gcc" CXX="g++" F77="gfortran" FC="gfortran" \
	CFLAGS="$(OPTFLAGS) -fPIC -std=gnu17" CXXFLAGS="$(OPTFLAGS) -fPIC -std=gnu17" CPPFLAGS=" -std=gnu17 " FFLAGS="$(OPTFLAGS) -fPIC" \
	./configure --prefix=/$(LAMV) \
		     --build=$(DEB_BUILD_GNU_TYPE) \
		     --host=$(DEB_HOST_GNU_TYPE) \
		     --with-rsh=/usr/bin/rsh \
		     --with-trillium \
		     --with-romio-flags=-ar_nolocal \
		     --with-rpi=sysv 

	awk '/LAM_HAVE_OPENPTY/ {gsub("1","0")} /LAM_HAVE_SYSV_PTYS/ {gsub("1","0")}  {print}' \
		share/include/lam_config.h >tmp && mv tmp share/include/lam_config.h

	$(MAKE) 

	$(MAKE) install DESTDIR=$(subst $(LAMV)/bin,,$(subst $(LAMV)/lib,,$(TDIR)/$(@D)))


ERM:=pmpil_trace_f.o pdupfn_f.o mpietc.o



$(LDIR)/liblam.so.$(VER): $(addprefix $(SHLDIR)/,liblamf77mpi.a libmpi.a liblam.a)
	mkdir tmp 
	chmod +x debian/mar
	cd tmp && mkdir $(^F) && echo $(addprefix ../../,$^) | xargs -t -n 1 ../debian/mar
	rm -f $(addprefix tmp/libmpi.a/,$(ERM)) $(addprefix tmp/liblam.a/,ltdl.o)
	mkdir -p $(@D)
	$(CC) -shared -Wl,-soname=$(@F:.$(VER)=.$(SO)) -o $@ tmp/*/*.o -lpthread -lltdl # -ldl -lutil -lcr
	rm -rf tmp

$(LDIR)/libplam.so.$(VER): $(addprefix $(SHLDIR)/,liblamf77mpi.a liblam.a  )
	mkdir tmp
	cd tmp && echo $(addprefix ../,$^) | xargs -t -n 1 ar x && rm -f $(ERM)
	mkdir -p $(@D)
	$(CC) -shared -Wl,-soname=$(@F:.$(VER)=.$(SO)) -o $@ tmp/*.o # -lutil
	rm -rf tmp

$(LDIR)/liblamio.so.$(VER): $(SHLDIR)/liblammpio.a $(LDIR)/liblam.so
	mkdir tmp
	cd tmp && echo $(addprefix ../,$<) | xargs -t -n 1 ar x && rm -f $(ERM)
	mkdir -p $(@D)
ifeq ($(DEB_HOST_GNU_SYSTEM),kfreebsd-gnu)
# aio is enable by default on GNU/kFreeBSD, because of UFS support
	$(CC) -shared -Wl,-soname=$(@F:.$(VER)=.$(SO)) -o $@ tmp/*.o -L$(LDIR) -llam -lrt # -lutil
else
	$(CC) -shared -Wl,-soname=$(@F:.$(VER)=.$(SO)) -o $@ tmp/*.o -L$(LDIR) -llam # -lutil
endif
	rm -rf tmp

$(LDIR)/liblam++.so.$(VER): $(SHLDIR)/liblammpi++.a $(LDIR)/liblam.so
	mkdir tmp
	cd tmp && echo $(addprefix ../,$<) | xargs -t -n 1 ar x && rm -f $(ERM)
	mkdir -p $(@D)
	g++ -shared -Wl,-soname=$(@F:.$(VER)=.$(SO)) -o $@ tmp/*.o -L$(LDIR) -llam # -lutil
	rm -rf tmp

lib%.so.$(SO): lib%.so.$(VER)
	ln -snf $(<F) $@

lib%.so: lib%.so.$(SO)
	ln -snf $(<F) $@




$(LDIR)/liblam.a: $(addprefix $(STLDIR)/,liblamf77mpi.a libmpi.a liblam.a )
	mkdir tmp
	cd tmp && echo $(addprefix ../,$^) | xargs -t -n 1 ar x 
	rm -f $@
	mkdir -p $(@D)
	ar r $@ tmp/*.o
	rm -rf tmp

$(LDIR)/libplam.a: $(addprefix $(STLDIR)/,liblamf77mpi.a liblam.a  )
	mkdir tmp
	cd tmp && echo $(addprefix ../,$^) | xargs -t -n 1 ar x 
	rm -f $@
	mkdir -p $(@D)
	ar r $@ tmp/*.o
	rm -rf tmp

$(LDIR)/liblamio.a: $(STLDIR)/liblammpio.a
	mkdir tmp
	cd tmp && echo $(addprefix ../,$^) | xargs -t -n 1 ar x 
	rm -f $@
	mkdir -p $(@D)
	ar r $@ tmp/*.o
	rm -rf tmp

$(LDIR)/liblam++.a: $(STLDIR)/liblammpi++.a
	mkdir tmp
	cd tmp && echo $(addprefix ../,$^) | xargs -t -n 1 ar x 
	rm -f $@
	mkdir -p $(@D)
	ar r $@ tmp/*.o
	rm -rf tmp

define relink
	mkdir -p $(@D)
	cd $$(dirname $$(find -name $(<F) -perm -u+x -type f |grep -v debian)) && \
		mv -f $(<F) $(<F).old && \
		( $(MAKE) -n $(<F) | awk '/mkdir/ {next} /libtool/ { \
				gsub("-lutil",""); \
				gsub("[^ ]*libltdlc\\.la",""); \
				gsub("[^ ]*liblam\\.la","-L'$(TDIR)/$(LDIR)' -llam"); \
				gsub("[^ ]*libmpi\\.la","-L'$(TDIR)/$(LDIR)' -llam"); \
				gsub(" $(subst +,\\+,$(<F)) "," '$(TDIR)/$(@)' "); \
				print}' | bash -x -e || (mv $(<F).old $(<F) && false)) && \
		mv $(<F).old $(<F)
endef

$(BDIR)/%: $(SHBDIR)/% $(LDIR)/liblam.so
	$(relink)
$(addprefix $(BDIR)/, lamsweep): $(BDIR)/lam%: $(SHBDIR)/% $(LDIR)/liblam.so
	$(relink)
$(BDIR)/%.lam: $(SHBDIR)/% $(LDIR)/liblam.so
	$(relink)
$(BDIR)/mpiexec.lam: $(SHBDIR)/mpiexec
	cp $< $@

#$(addprefix $(BDIR)/,lamwipe lamsweep): $(BDIR)/lam% : $(SHBDIR)/% $(LDIR)/liblam.so
#	mkdir -p $(@D)
#	cd $$(find -name $(<F) -type d |grep -v share) && \
#		rm -f $(<F) && \
#		$(MAKE) -n $(<F) | awk '/libtool/ { \
#				gsub("[^ ]*liblam\.la","-L'$(TDIR)/$(LDIR)' -llam"); \
#				gsub(" '$(<F)' "," '$(TDIR)/$(@)' "); \
#				print}' | bash -x -e

#$(BDIR)/mpirun.lam: $(SHBDIR)/mpirun $(LDIR)/liblam.so
#	mkdir -p $(@D)
#	cd $$(find -name $(<F) -type d |grep -v share) && \
#		rm -f $(<F) && \
#		$(MAKE) -n $(<F) | awk '/libtool/ { \
#				gsub("[^ ]*liblam\.la","-L'$(TDIR)/$(LDIR)' -llam"); \
#				gsub(" '$(<F)' "," '$(TDIR)/$(@)' "); \
#				print}' | bash -x -e

#$(addprefix $(BDIR)/,$(addsuffix .lam,mpicc mpic++ mpif77)): \
#	$(BDIR)/%.lam: \
#	$(WDIR)/% $(LDIR)/liblam.so
#	mkdir -p $(@D)
#	cd $(WDIR) && \
#		rm -f $(<F) && \
#		$(MAKE) -n $(<F) | awk '/libtool/ { \
#				gsub("[^ ]*liblam\.la","-L'$(TDIR)/$(LDIR)' -llam"); \
#				gsub(" '$(<F)' "," '$(TDIR)/$(@)' "); \
#				print}' | bash -x -e

$(BDIR)/balky: $(SHBDIR)/balky
	mkdir -p $(@D)
	awk '{gsub("'$(TDIR)/tools/hcc'","/usr/bin");print}' $< >$@
	chmod 0755 $@

$(BDIR)/hcp: $(SHBDIR)/hcp
	mkdir -p $(@D)
	install -m 755 $< $@ 

$(MPROX): $(MSRC)
	mkdir -p $$(dirname $$(dirname $(@D)))
	rm -rf $$(dirname $(@D))
	cp -af $$(dirname $(<D)) $$(dirname $(@D))
#	mv $(@D)/../man1/wipe.1 $(@D)/../man1/lamwipe.1
	rm $(@D)/../man1/wipe.1
	mv $(@D)/../man1/mpicc.1 $(@D)/../man1/mpicc.lam.1
	mv $(@D)/../man1/mpiCC.1 $(@D)/../man1/mpiCC.lam.1
	mv $(@D)/../man1/mpic++.1 $(@D)/../man1/mpic++.lam.1
	mv $(@D)/../man1/mpif77.1 $(@D)/../man1/mpif77.lam.1
	mv $(@D)/../man1/mpirun.1 $(@D)/../man1/mpirun.lam.1
	mv $(@D)/../man1/mpiexec.1 $(@D)/../man1/mpiexec.lam.1
	! [ -e $(@D)/../man3/libmpi.3 ] || mv $(@D)/../man3/libmpi.3 $(@D)/../man7/libmpi.7
	cat $(@D)/../man1/lamd.1 | sed "s,^.TH  *LAM  *7 \(.*\),.TH LAM 1 \1,1" >tmp && \
		mv tmp $(@D)/../man1/lamd.1
	for i in $$(find $$(dirname $(@D)) -type f) ; do \
		( cat $$i | sed -e 's,\([^A-Z_a-z]\)wipe\([^A-Z_a-z]\),\1lamwipe\2,1' \
			      -e 's,^wipe\([^A-Z_a-z]\),lamwipe\1,1' \
			      -e 's,\([^A-Z_a-z]\)wipe$$,\1lamwipe,1' \
			      -e 's,^wipe$$,lamwipe,1' > tmp && \
		( ( diff -u $$i tmp && rm -f tmp ) || ( echo Modifiying $$i && mv tmp $$i ))) || exit -1;\
	done

llibs:=lam mpi lamf77mpi
sollinks:=$(addprefix $(LAMD)/lib/lib,$(addsuffix .so,$(llibs)))
allinks:=$(addprefix $(LAMD)/lib/lib,$(addsuffix .a,$(llibs)))
lamlinks: $(addprefix $(LAMD)/lib/lib,$(addsuffix .so,$(dlibs)) $(addsuffix .a,$(dlibs)))

$(sollinks): $(MDIR)/usr/lib/liblam.so
	mkdir -p $(@D)
	rm -rf $(@)
	ln -snf ../../$(<F) $@

$(allinks): $(MDIR)/usr/lib/liblam.a
	mkdir -p $(@D)
	rm -rf $(@)
	ln -snf ../../$(<F) $@

$(LAMD)/lib/libpmpi.a : $(MDIR)/usr/lib/libplam.a
	mkdir -p $(@D)
	rm -rf $(@)
	ln -snf ../../$(<F) $@

$(LAMD)/lib/libpmpi.so : $(MDIR)/usr/lib/libplam.so
	mkdir -p $(@D)
	rm -rf $(@)
	ln -snf ../../$(<F) $@

$(LAMD)/lib/liblammpi++.a: $(MDIR)/usr/lib/liblam++.a
	mkdir -p $(@D)
	rm -rf $(@)
	ln -snf ../../$(<F) $@

$(LAMD)/lib/liblammpi++.so: $(MDIR)/usr/lib/liblam++.so
	mkdir -p $(@D)
	rm -rf $(@)
	ln -snf ../../$(<F) $@

$(LAMD)/lib/liblammpio.a: $(MDIR)/usr/lib/liblamio.a
	mkdir -p $(@D)
	rm -rf $(@)
	ln -snf ../../$(<F) $@

$(LAMD)/lib/liblammpio.so: $(MDIR)/usr/lib/liblamio.so
	mkdir -p $(@D)
	rm -rf $(@)
	ln -snf ../../$(<F) $@

$(LAMD)/include:
	mkdir -p $(@D)
	rm -rf $(@)
	ln -snf ../../include/lam $@

$(LAMD)/man:
	mkdir -p $(@D)
	rm -rf $(@)
	ln -snf ../../share/man $@

$(LAMD)/bin:
	mkdir -p $(@D)
	rm -rf $(@)
	ln -snf ../../bin $@

CF:=conf.lamd bhost.def #bhost.lam conf.lam

$(addprefix $(MDIR)/etc/lam/,$(CF)): $(MDIR)/etc/lam/%: $(SHLDIR)/../etc/lam-%
	mkdir -p $(@D)
	install -m 0644 $< $@ 
	ln -snf $(@F) $(@D)/$(<F)

$(MDIR)/etc/lam/lam-helpfile: $(SHLDIR)/../etc/lam-helpfile
	mkdir -p $(@D)
	install -m 0644 $< $@

$(LAMD)/etc: $(addprefix $(MDIR)/etc/lam/,$(CF)) $(MDIR)/etc/lam/lam-helpfile
	mkdir -p $(@D)
	rm -rf $(@)
	ln -snf /etc/lam $@

$(MDIR)/usr/include/lam/mpi.h: $(SHIDIR)/mpi.h
	mkdir -p $(@D)
	cp -af $(<D)/* $(@D)

$(MDIR)/usr/include/lam/mpi++.h: $(MDIR)/usr/include/lam/mpi.h 
	rm -rf $(@)
	ln -snf mpi2c++/$(@F) $@

links: $(addprefix $(LAMD)/,include bin man etc)

debian/examples/config.status: config.status
	mkdir -p $(@D)
	cp -a config $(@D)/
	cp configure.in $(@D)
	cp -a examples $(@D)/
	awk '/LINK/ {$$0=$$0 " -llammpio"} {print}' \
		$(@D)/examples/romio/Makefile.am >tmp && \
		mv tmp $(@D)/examples/romio/Makefile.am
	mv $(@D)/examples $(@D)/main
#	cp -a mpi2c++/contrib $(@D)
#	for i in $(@D)/contrib/test_suite/Makefile \
#		$(@D)/contrib/examples/Makefile ; do \
#		touch $$i.am ; touch $$i.in ; touch $$i ; done
#	mv $(@D)/contrib $(@D)/c++
	cp -a romio/test $(@D)/romio.test
	rm -f debian/examples/romio.test/mpif.h
	awk '{gsub("'$(TDIR)'","");gsub("/tools/hcc/","");gsub("/tools/hf77/","");\
	      gsub("/share/include","/usr/lib/lam/include");\
	      gsub("[^ ]*liblammpio.a","-llammpio");\
	      gsub("[^ ]*libmpi.la","-lmpi");\
	      gsub("-lam-building","");print}' $(@D)/romio.test/Makefile >tmp && \
		mv tmp $(@D)/romio.test/Makefile
	( awk '/^chmod/ {$$0="# " $$0} {print}' $< > $@ && \
		touch -r $< $@ ) || ( rm -f $@ && false )


$(MDIR)/usr/lib/update-cluster/lam.updatelist: debian/lam.updatelist
	mkdir -p $(@D)
	install -m 755 $< $@

foo:
	head -1l debian/changelog | cut -f2 -d\  | tr -d '()' | cut -f1 -d-


$(MMAN): $(MMDIR)/%.1 : $(SHBDIR)/% $(LDIR)/liblam.so
	mkdir -p $(@D)
	chmod +x debian/mm
	LD_LIBRARY_PATH=$(LDIR) \
		VERSION=$$(head -1l debian/changelog | cut -f2 -d\  | tr -d '()' | cut -f1 -d-)\
		PNAME=$* MPATH=$(<D) help2man -m LAM -s 1 -N debian/mm >$@

#$(MDIR)/usr/include/lam/mpi++.h 
install: libs bins $(MPROX) links $(MDIR)/usr/include/lam/mpi.h \
	debian/examples/config.status lamlinks \
	$(MDIR)/usr/lib/update-cluster/lam.updatelist #$(MMAN)

	rm -rf $(MDIR)/usr/lib/lam/bin/.libs
	dh_movefiles
	mv ./debian/lam4-dev/usr/share/man/man3/MPI_Comm_set_name.3 ./debian/lam4-dev/usr/share/man/man3/MPI_Comm_set_name_lam4-dev.3
	mkdir -p debian/liblam$(SO)t64/usr/lib/$(DEB_HOST_MULTIARCH)
	mv debian/liblam$(SO)t64/usr/lib/lib* debian/liblam$(SO)t64/usr/lib/$(DEB_HOST_MULTIARCH)
	mkdir -p debian/lam$(SO)-dev/usr/lib/$(DEB_HOST_MULTIARCH)
	mv debian/lam$(SO)-dev/usr/lib/lib* debian/lam$(SO)-dev/usr/lib/$(DEB_HOST_MULTIARCH)
	for i in debian/lam$(SO)-dev/usr/lib/lam/lib/*; do \
		j=$$(readlink $$i);\
		ln -snf $$(dirname $$j)/$(DEB_HOST_MULTIARCH)/$$(basename $$j) $$i;\
	done

build: build-arch build-indep
build-arch: build-stamp
build-indep: build-stamp
build-stamp: $(stlibs) $(shlibs)
build-stamp: debian/lam4-dev.prerm debian/lam4-dev.postinst

debian/lam4-dev.prerm: debian/lam4-dev.prerm.in
	sed 's/@DEB_HOST_MULTIARCH@/$(DEB_HOST_MULTIARCH)/g' $< > $@

debian/lam4-dev.postinst: debian/lam4-dev.postinst.in
	sed 's/@DEB_HOST_MULTIARCH@/$(DEB_HOST_MULTIARCH)/g' $< > $@

clean:
	dh_testdir
	dh_testroot
	rm -rf debian/examples
	rm -f  install-stamp

	-rm -rf debian/static debian/shared
	rm -f romio/lib/LINUX/libmpio.a romio/lib/liblammpio.a \
		romio/test/error romio/test/status romio/ltest.c
	-cd romio && $(MAKE) clean
	find . -name "*.lo" -exec rm -f {} \;
	rm -f romio/util/lam-configure-values share/rpi/usysv/Makefile
	[ ! -f Makefile ] || $(MAKE) clean
#	-$(MAKE) clean
	rm -f share/rpi/myri/Makefile
	rm -f share/rpi/sysv/Makefile
	rm -f share/rpi/shmem/Makefile
	rm -f share/rpi/via/Makefile
	-cd debian/conf/ && find * -type f -exec mv {} ../../{} \; 
	rm -rf debian/conf
	rm -f config/Makefile
	for i in config.status config.cache config.log Makefile myapp patchlevel.h lam_config.h stamp-h1 stamp-h liblamrpishmem.la liblamrpisysv.la mpi2c++_config.h balky mini-balky hcp ; do \
		find -name $$i -exec rm -f {} \; ; \
	done
	rm -f ./romio/test/mpif.h romio/util/romioinstall romio/include/mpio.h  romio/include/mpiof.h  romio/test/misc.c  romio/test/large_file.c  romio/test/runtests  romio/test/fmisc.f  romio/test/fcoll_test.f  romio/test/pfcoll_test.f  romio/test/fperf.f 
	for i in $$(find -name .deps -type d); do  rm -rf $$i; done
	rm -f config/lam-shell-setup.sh config/lam-shell-setup.csh config/lam_module.tcl
	find -name libtool -exec rm -f {} \;
	find -name "*-config.h" -exec rm -f {} \;
	find -name "*-modules.h" -exec rm -f {} \;
	rm -f romio/adio/include/romioconf.h \
		romio/config.command \
		share/ssi/rpi/base/lam-ssi-rpi-modules.h.struct \
		share/ssi/rpi/base/lam-ssi-rpi-modules.h.extern conftest_ssi_list.29858 \
		confdefs.h share/include/lam_build_info.h ./share/include/mpif.h

	find -type l -exec rm {} \;
	find -name .lam_configure_results_cache -exec rm {} \;
	rm -f share/libltdl/config.h
	rm -f share/ssi/crlam/blcr/ssi_crlam_blcr_config.sh debian/lam$(SO).substvars

	rm -f debian/lam4-dev.prerm debian/lam4-dev.postinst

	dh_clean

binary-arch: build install
	dh_testdir -a
	dh_testroot -a
	dh_installdocs -a
	dh_installexamples -a
	dh_installchangelogs -a
	dh_link -a
	dh_strip -a
	dh_compress -a
	dh_fixperms -a
	dh_makeshlibs -a
	dh_installdeb -a
	dh_shlibdeps -a -l `pwd`/debian/liblam$(SO)t64/usr/lib/
	cat debian/liblam$(SO)t64.substvars | sed "s, *lam$(SO)[^,]*,,1" > tmp && mv tmp debian/lam$(SO)t64.substvars
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary-indep: build install
	dh_testdir -i
	dh_testroot -i
	dh_installdocs -i
	dh_installchangelogs -i
	dh_link -i
	dh_strip -i
	dh_compress -i
	for i in $$(find debian/lam-mpidoc -name "MPI*.gz"); do\
		zcat $$i | sed "s,^'ierr',\\\\\&'ierr',g" | gzip -9 >tmp.gz && mv tmp.gz $$i; done
	dh_fixperms -i
	dh_makeshlibs -i
	dh_installdeb -i
	dh_shlibdeps -i -l `pwd`/debian/liblam$(SO)t64/usr/lib/
	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: build clean binary-indep binary-arch binary
.PRECIOUS: $(addprefix $(LDIR)/lib,$(addsuffix .so.$(SO),$(tlibs)))
