#! /bin/sh
#
#  Wrapper for pvm binaries on various OS systems
#  Install this as e.g. pvmfoo in /usr/bin/ and this
#  script tries to start
#
#     $PVM_ROOT/bin/$PVM_ARCH/pvmfoo
#     $PVM_ROOT/lib/$PVM_ARCH/pvmfoo
#
#  Adapted from the pvm script for SuSE Linux: <werner@suse.de>
#

wrapped="`basename $0`"

if test -z "$PVM_ROOT"; then
    case "$0" in
	/*) PVM_ROOT="$0"	;;
	*)  PVM_ROOT="`pwd`/$0" ;;
    esac
    PVM_ROOT="`echo \"$PVM_ROOT\" | sed -e \"s:/lib/${wrapped}::\"`"
    if [ -d $PVM_ROOT ]; then
	export PVM_ROOT
    else
	if   test -d /usr/lib/pvm3 ; then
	    PVM_ROOT=/usr/lib/pvm3
	elif test -d /usr/share/pvm3 ; then
	    PVM_ROOT=/usr/share/pvm3
	elif test -d /usr/local/lib/pvm3 ; then
	    PVM_ROOT=/usr/local/lib/pvm3
	elif test -d /usr/pvm/pvm3 ; then
	    PVM_ROOT=/usr/pvm/pvm3
	elif test -d $HOME/pvm3 ; then
	    PVM_ROOT=$HOME/pvm3
	else
	    echo pvm: PVM_ROOT not defined >&2
	    exit 1
	fi
	export PVM_ROOT
    fi
fi

#
# these lines let you set environment variables on systems
# where a shell profile doesn't get read when rshing in.
#
if test -f $PVM_ROOT/.pvmprofile ; then
	. $PVM_ROOT/.pvmprofile
fi
if test -f $HOME/.pvmprofile ; then
	. $HOME/.pvmprofile
fi

if test -z "$PVM_ARCH" -o "$PVM_ARCH" = "UNKNOWN" ; then
    PVM_ARCH="`$PVM_ROOT/lib/pvmgetarch`"
    if test -z "$PVM_ARCH"; then
	echo pvm: can\'t set arch >&2
	exit 1
    fi
fi

if test "$1" = "-exe" ; then
	sfx=".exe"
	shift
else
	sfx=""
fi

err=""
for d in bin lib ; do
    program="${PVM_ROOT}/${d}/${PVM_ARCH}/${wrapped}${sfx}"
    if test -x ${program} ; then
	export PVM_ARCH
	exec ${program} $@
	exit 1
    fi
    err="$program $err"
done

for p in $err ; do
    echo "${wrapped}: $p doesn't exist." >&2
done
echo "Make sure PVM is built and PVM_ROOT is set correctly" >&2
exit 1

