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

topdir=$(shell pwd)

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

build-stamp: build-stamp-shared build-stamp-static build-stamp-test $(QUILT_STAMPFN)
	touch build-stamp

build-stamp-shared:
	dh_testdir
	[ -d TESTING/EXE ] || mkdir TESTING/EXE
# clean
	BASEDIR=$(topdir) make cleanall
	cd TESTING && make clean
# build the shared libraries
	BASEDIR=$(topdir) FPIC=-fPIC make pvm
	mkdir -p tmp
	set -e ;\
	cd tmp ;\
	ar x ../LIB/blacs_PVM-LINUX-0.a ;\
	mkdir tmp ;\
	for j in $$(find -name "*.C") ;\
	  do mv $$j tmp/$$(echo $$j | sed 's,C$$,o,g') ;\
	done;\
	cd .. ;\
	gcc -shared -Wl,-soname=libblacs-pvm.so.1 -o libblacs-pvm.so.1.1 \
	  $$(find tmp -name "*.o");\
	ln -sf libblacs-pvm.so.1.1 libblacs-pvm.so.1 ;\
	ln -sf libblacs-pvm.so.1 libblacs-pvm.so ;\
	rm -f tmp/tmp/* ; rmdir tmp/tmp ; rm tmp/* ;\
	rmdir tmp

	touch build-stamp-shared

build-stamp-static:
	dh_testdir
	[ -d TESTING/EXE ] || mkdir TESTING/EXE
# clean
	BASEDIR=$(topdir) make cleanall
	cd TESTING && make clean
# static libaries
	BASEDIR=$(topdir) make pvm

	touch build-stamp-static

build-stamp-test:
	dh_testdir
	[ -d TESTING/EXE ] || mkdir TESTING/EXE
# clean
	BASEDIR=$(topdir) make cleanall
	cd TESTING && make clean
# testing binaries
#	cd TESTING && BASEDIR=$(topdir) BTLIBS='$$(BLACSLIB) $$(PVMLIB)' make
	cd TESTING && BASEDIR=$(topdir) BTLIBS='-L.. -lblacs-pvm $$(PVMLIB)' make

	touch build-stamp-test

clean: unpatch
	dh_testdir
	dh_testroot
	rm -f build-stamp-*
	BASEDIR=$(topdir) make cleanall
	cd LIB && rm -f *.a
	cd TESTING && make clean
	rm -f TESTING/EXE/*
	rm -f SRC/PVM/INTERNAL/Bconfig.h
	rm -f SRC/PVM/INTERNAL/Bdef.h
	rm -f libblacs*so*
	dh_clean

binary-arch: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs -a

	install LIB/blacs_PVM-LINUX-0.a \
		`pwd`/debian/blacs-pvm-dev/usr/lib/libblacs-pvm.a
	install libblacs-pvm.so.1.1 \
		`pwd`/debian/blacs1-pvm/usr/lib/libblacs-pvm.so.1.1
	install TESTING/EXE/xFbtest_PVM-0 \
		`pwd`/debian/blacs-pvm-test/usr/lib/blacs/fblacs_test-pvm
	install TESTING/EXE/xCbtest_PVM-0 \
		`pwd`/debian/blacs-pvm-test/usr/lib/blacs/cblacs_test-pvm

	dh_installdocs -a README
	dh_installman -a
	dh_installchangelogs -a
	dh_link -a
	dh_compress -a 
	dh_fixperms -a
	dh_strip -a
	dh_makeshlibs -V 
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary-indep: build

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

binary: binary-indep binary-arch

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