#!/bin/sh
#
#	$Id: aimk,v 1.3 1998/10/01 21:33:32 pvmsrc Exp $
#
#	aimk.sh
#
#	Make wrapper for multiple arch. builds.
#
#	Sets PVM_ARCH to LINUX64
#	and PVM_ROOT to /usr/lib64/pvm3 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/conf/$PVM_ARCH.def
#		and -f $AIMK_SRC/Makefile.aimk PVM_ARCH=$PVM_ARCH
#		$AIMK_SRC is the original working directory before the chdir.
#
#	3.  Else will simply exec $MAKE in cwd.
#
#	usage:
#	    aimk [-here] [ make args ... ]
#
#	09 Apr 1993  Manchek
#	26 Dec 2000  modified - to make $PVM_ROOT not needed

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

if [ -z "$PVM_ARCH" ] ; then PVM_ARCH="LINUX64" ; fi
if [ -z "$PVM_ROOT" ] ; then PVM_ROOT="/usr/lib64/pvm3" ; fi

export PVM_ARCH
export PVM_ROOT

#
# use *.def file to get $MAKE define.
#

MAKEDEF=`grep MAKE $PVM_ROOT/conf/$PVM_ARCH.def \
	| sed -e "s/ //g" -e "s/	//g"`

eval $MAKEDEF

#
# run $MAKE in cwd or subdir if exists.
#

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
	if [ "$*" = "" ]; then
		exec $MAKE PVM_ARCH=$PVM_ARCH
	else
		exec $MAKE PVM_ARCH=$PVM_ARCH "$@"
	fi

else
	if [ $makeincwd = 0 -a -f Makefile.aimk ]; then
		if [ ! -d $PVM_ARCH ]; then
			mkdir $PVM_ARCH
		fi
		echo making in $PVM_ARCH/ for $PVM_ARCH
		AIMK_SRC=`pwd`
		export AIMK_SRC
		cd $PVM_ARCH
		if [ "$*" = "" ]; then
			exec $MAKE -f $PVM_ROOT/conf/$PVM_ARCH.def -f $AIMK_SRC/Makefile.aimk PVM_ARCH=$PVM_ARCH
		else
			exec $MAKE -f $PVM_ROOT/conf/$PVM_ARCH.def -f $AIMK_SRC/Makefile.aimk PVM_ARCH=$PVM_ARCH "$@"
		fi

	else
		echo making in . for $PVM_ARCH
		if [ "$*" = "" ]; then
			exec $MAKE PVM_ARCH=$PVM_ARCH
		else
			exec $MAKE PVM_ARCH=$PVM_ARCH "$@"
		fi
	fi
fi

exit 1

