#!/bin/sh
#
#   Copyright (c) 1988-1993 Frame Technology Corp. All rights reserved.
#
#   $Id: .wrapper,v 1.8 1993/01/16 04:44:47 tvb Exp $
#
# Frame startup script.

# Unset $ENV so ksh desguising itself as sh doesn't cause loads
# of problems if a .kshrc file happens to echo something.
if [ ! "X${ENV}" = 'X' ]; then
   unset ENV; export ENV
fi

# Determine FMHOME.
# Always reset FMHOME! 
arg0=$0
mydir=`expr \
  ${arg0-.}'/' : '\(/\)[^/]*/$' \
  \| ${arg0-.}'/' : '\(.*[^/]\)//*[^/][^/]*//*$' \
  \| .`

# myname=`basename $arg0`
myname=`expr //$arg0 : '.*/\(.*\)'`

cdir=`pwd`
case $mydir in
/*)
    thisdir=$mydir
    ;;
.)
    thisdir=$cdir
    ;;
*)
    thisdir=$cdir/$mydir
    ;;
esac
FMHOME=`expr \
  ${thisdir-.}'/' : '\(/\)[^/]*/$' \
  \| ${thisdir-.}'/' : '\(.*[^/]\)//*[^/][^/]*//*$' \
  \| .`
export FMHOME
[ ! -d $FMHOME/fminit ] && {
    echo ""
    echo "$myname: this Frame software is not correctly installed.  Failed"
    echo 'to set $FMHOME to the installation directory (could not find'
    echo "$FMHOME/fminit)."
    echo "Please see your system administrator."
    echo ""
    exit 1
}

# Determine FMARCH if it is not already set or seems incorrect.
if [ -z "$FMARCH" -o ! -d "$FMHOME/bin/$FMARCH" ]; then
    FMARCH=`$FMHOME/bin/scripts/fmarch`
fi
if [ ! -d $FMHOME/bin/$FMARCH ]; then
    echo "$myname: unable to locate binary directory for this architecture."
    echo "Please see your system administrator."
    exit 1
fi
export FMARCH

# We now have enough info to check if bin/maker is a link.
if [ -x $FMHOME/bin/scripts/isslink ]; then
    $FMHOME/bin/scripts/isslink $FMHOME/bin/$myname || {
	echo "
""$myname: this Frame software is not correctly installed.  The file""
""($FMHOME/bin/$myname) must be a symbolic link.""
""Please see your system administrator."
	exit 1
    }
fi

# Set the USER environment variable if need be.
# Do we need USER set?
if [ ! -z "$USER" ]; then
    USER="$USER"
elif [ ! -z "$LOGNAME" ]; then
    USER="$LOGNAME"
elif [ -x /usr/ucb/whoami ]; then
    USER=`/usr/ucb/whoami`
else
    USER=unknown
fi
export USER

PATH="$FMHOME/bin:$PATH"
export PATH
if [ -x $FMHOME/bin/scripts/$myname ]; then
    exec $FMHOME/bin/scripts/$myname ${1+"$@"}
elif [ -x $FMHOME/bin/$FMARCH/$myname ]; then
    exec $FMHOME/bin/$FMARCH/$myname ${1+"$@"}
fi

# There is no try.
case $FMARCH in
*unknown*)
    echo "Unable to determine binary directory for this architecture."
    ;;
*)
    echo "$myname: Cannot find the script $FMHOME/bin/scripts/$myname"
    echo "  or the binary $FMHOME/bin/$FMARCH/$myname."
    ;;
esac
exit 1
