#!/bin/sh
#
#	aimk_easy.sh (derived from aimk.sh, by Bob Manchek)
#
#	To build libeasy.a & libfeasy.a (EASY stands for EnhAnced
#       Subroutine librarY for PVM).
#	By Sami Saarinen (sbs@csc.fi), Center for Scientific Computing
#       (CSC), Finland. 8-Nov-1993.
#
#	Make wrapper for multiple arch. builds.
#
#	Automatically sets PVM_ARCH and PVM_ROOT for make to use.
#
#	Action depends on makefile locations:
#	1.  If $PVM_ARCH/Makefile or $PVM_ARCH/makefile exists,
#		chdir to $PVM_ARCH and exec make there.
#
#	2.  Else if ./Makefile.aimk exists,
#		chdir to $PVM_ARCH and exec make
#		with -f $PVM_ROOT/easypvm3/conf/$PVM_ARCH.def
#		and -f ../Makefile.aimk PVM_ARCH=$PVM_ARCH
#
#	3.  Else will simply exec make in cwd.
#
#	usage:
#	    aimk [-here] [-nomk] [ make args ... ]
#
#	09 Apr 1993  Manchek
#

makeincwd=0
nomk=0
found=1
while [ $# -ge 1 -a $found = 1 ]; do
	found=0
	case "$1" in
	-here ) makeincwd=1; shift; found=1 ;;
	-nomk ) nomk=1 shift; found=1 ;;
	esac
done

case "x$PVM_ROOT" in x )
	echo aimk: PVM_ROOT not defined >&2
	if [ "$1" != "clean" ] ; then
		CURDIR="`pwd`"
		cd ../..
		PVM_ROOT="`pwd`"
		export PVM_ROOT
		cd $CURDIR
		echo "aimk: PVM_ROOT automagically set to '$PVM_ROOT'"
	else
		exit 1
	fi
;; esac

case "x$PVM_ARCH" in x | xUNKNOWN )
#	PVM_ARCH="`$PVM_ROOT/lib/pvmgetarch`"
	PVM_ARCH="`$PVM_ROOT/easypvm3/lib/pvmgetarch`"
	case "x$PVM_ARCH" in x )
		echo aimk: can\'t set arch >&2
		exit 1
	;; esac
;; esac

export PVM_ARCH
export PVM_ROOT

#
# run make in cwd or subdir if exists.
#

if [ $nomk = 1 ]; then echo $PVM_ARCH; exit 0; fi

if [ $makeincwd = 0 -a \( -f $PVM_ARCH/Makefile -o -f $PVM_ARCH/makefile \) ]; then
	echo making in $PVM_ARCH/ for $PVM_ARCH
	cd $PVM_ARCH
	exec make PVM_ARCH=$PVM_ARCH $@

else
	if [ -f Makefile.aimk ]; then
		if [ ! -d $PVM_ARCH ]; then
			mkdir $PVM_ARCH
		fi
		echo making in $PVM_ARCH/ for $PVM_ARCH
		cd $PVM_ARCH
		exec make -f $PVM_ROOT/easypvm3/conf/$PVM_ARCH.def -f ../Makefile.aimk PVM_ARCH=$PVM_ARCH $@

	else
		echo making in . for $PVM_ARCH
		exec make PVM_ARCH=$PVM_ARCH $@
	fi
fi

exit 1

