#! /bin/sh

#	Copyright (c) 1988-89 Frame Technology Corp.
# 
# Frame generic startup script
# ejk

# return path part of filename (up to last /)
# non-zero exit status indicates that only a filename given (no path)
thisdirname() {
	expr \
	  ${1-.}'/' : '\(/\)[^/]*/$' \
	  \| ${1-.}'/' : '\(.*[^/]\)//*[^/][^/]*//*$' \
	  \| ""
}

# return directory on path containing this executable
whichdir() {
	IFS=:
	for there in $PATH; do
		if [ -x $there/$1 ]; then
			echo $there
			return;
		fi
	done;
}

# make arg fully rooted and either remove bin from end of arg or add ..
stripbin() {
	if [ `expr index $1 '//'` = 1 ]; then	# fully rooted
		newdir=$1
	else
		if [ $1 = "." ]; then		# running from '.'
			newdir=$cwd		# remove . so we can remove bin
		else
			newdir=$cwd/$1
		fi
	fi

	if [ `basename $newdir` = "bin" ]; then
		thisdirname $newdir
	else
		echo "$newdir/.."
	fi
}

# make sure that we have good stuff on our path
PATH=/bin:/usr/bin:/usr/ucb:$PATH

if [ "$PWD" = "" ]; then
	cwd=`pwd`
else
	cwd=$PWD
fi

myname=`basename $0`
mydir=`thisdirname $0`

if [ $? != 0 ]; then
	# no path given
	mydir=`whichdir $0`
	if [ "$mydir" = "" ]; then
		# not in path, ./cmd?
		# this case should never happen
		FMHOME="$cwd/.."
	else
		# typical case <install_dir>/bin or in .
		FMHOME=`stripbin $mydir`
	fi
else
	# path to script given by user (could be ./)
	FMHOME=`stripbin $mydir`
fi

if [ ! -d $FMHOME/.fminit2.0 ]; then
	echo "
The Frame software is not correctly installed.  Failed to set
\$FMHOME to the installation directory (could not find
$FMHOME/.fminit2.0).
Please see your system administrator.
"
	exit 1
fi

if [ ! -h $FMHOME/bin/$myname ]; then
	echo "
The Frame software is not correctly installed.  This routine must be a
symbolic link ($FMHOME/bin/$myname).
Please see your system administrator.
"
	exit 1
fi

USER=`whoami`; export USER
PATH=$FMHOME/bin:$PATH; export PATH
export FMHOME

if [ -x $FMHOME/bin/scripts/$myname ]; then
	case $# in
	0) exec $FMHOME/bin/scripts/$myname ;;
	*) exec $FMHOME/bin/scripts/$myname "$@" ;;
	esac
else
	case $# in
	0) exec $FMHOME/bin/bin.`arch`/$myname ;;
	*) exec $FMHOME/bin/bin.`arch`/$myname "$@" ;;
	esac
fi
