#!/bin/sh

# 
# This script invokes the appropriate proctool version based on OS/platform.
# 
# This script may be placed in a commonly available directory (for
# example /usr/local/bin) by setting the variable INSTALLDIR (below) 
# to the directory where proctool is installed (ie. /opt/proctool).
# 
# Within limits, this script can also perform remote execution of
# proctool (via "rsh").  To enable this capability:
# 
#     1) Install the proctool distribution onto a file system
#        which is exported to the network.
# 
#     2) Make sure that the remote mounts allow the execution
#        of setuid programs.
# 
#     3) Set INSTALLDIR such that it can access the installation
#        from remote machines (for example /net/machine/somepath or
#        /usr/local/install/proctool rather than a local path like
#        /opt/proctool).
# 
#     4) Make sure "rsh" is enabled to the remote machine.
# 
# This script will invoke proctool remotely with the $DISPLAY set
# back to the original machine.  It's a KLUDGE, but it's handy.
# 
# If $INSTALLDIR is not set, then the script will attempt to locate
# the appropriate binaries automatically.
# 

############### Set this to proctool installation directory ###########
#
#INSTALLDIR=/opt/proctool/
INSTALLDIR=



#
# If INSTALLDIR is not set, then try to determine it
#
if [ -z "$INSTALLDIR" ] ; then
    case $0 in
        */* ) BINDIR=`dirname $0`;;
        *   ) LOC=`which $0`
              BINDIR=`dirname $LOC` ;;
    esac
    INSTALLDIR=`dirname $BINDIR`
else
    BINDIR="$INSTALLDIR/bin/"
fi


B=`basename $0`
if [ ! -x $BINDIR/$B ] ; then
    echo "$0: INSTALLDIR set incorrectly within \'$0\'"
    exit
fi


#
#  Parse the command line arguments
#
#  We will allow a KLUDGE such that a single argument not preceded by
#  a "-" will be interpretted as a host name.  This isn't pretty and
#  might even be a mistake; comments welcome.
#
cmdcount=$#
host=
options=
until [ $# -eq 0 ] ; do

    case $1 in 
        -d   ) shift; DISPLAY="$1" ; export DISPLAY;;
        -h   ) shift; host="$1";;
        -o   ) shift; OPENWINHOME="$1" ; export OPENWINHOME;;
        -p   ) shift; options="-p $1 $options";;
        -v   ) options="$1 $options";;
        -*   ) options="$1 $options";;
         *   ) if [ $cmdcount -eq 1 ] ; then
                   host="$1"
               else
                   options="$1 $options"
               fi
               ;;
    esac
    shift

done


#
# If the DISPLAY variable is not set, then assume that the person
# is not at the console, so try to figure out where they are logged
# in from and send the display back to them.
#
if [ -z "$DISPLAY" ] ; then
    tty=`tty | sed "s@/dev/@@"`
    if [ "$tty" != "not a tty" ] ; then
        machine=`who | grep $tty | sed -e 's/^.*(//' -e 's/)//'`
        DISPLAY="${machine}:0" ; export DISPLAY
        echo "DISPLAY variable not set, trying \"$DISPLAY\""
    fi
fi

#
#  As a minor convenience, if a host name is passed in, perform an rsh
#  to that machine, and run proctool remotely.  Passing in the DISPLAY
#  and OPENWIN path as arguments saves us from having to determine the
#  correct shell syntax (which varies for each shell) if we were 
#  trying to set it in the "rsh" outside the script.
#
if [ -n "$host" ] ; then

    if [ `xhost | egrep -c "(control disabled|$host)"` -eq 0 ] ; then
        echo "$0: Enabling X access from $host"
        xhost +$host > /dev/null
    fi

    # can't use $DISPLAY since it is usually just ":0.0"
    D=`uname -n`:0
    B=`basename $0`
    rsh -n $host $BINDIR/$B -d \"$D\" -o \"$OPENWINHOME\"
    exit
fi

#
#  Execute proctool on the (now) current machine
#
#  (Note that this may be from a recursive call via an rsh.)
#
#  Assume that SPARC and MOTIF are the defaults, and special case other
#  architectures and window systems.  In the long run, OW will disappear
#  and maybe the architecture will become part of the normal naming 
#  convention.
#

OS=`uname -r`

#
# 5.7_intel32 is easier to read than 5.7_i38632
#
CHIP=`uname -p`
if [ $CHIP = "i386" ] ; then
    CHIP="intel"
fi

#
# Something else will have to be added for merced
#
SIZ=32
if [ -x /usr/bin/isainfo ] ; then
    case `/usr/bin/isainfo` in
        sparcv9* ) SIZ=64;;
    esac
fi

if [ -x $BINDIR/${OS}_${CHIP}${SIZ}/proctool ] ; then
    exec $BINDIR/${OS}_${CHIP}${SIZ}/proctool $options
else
    echo "$0: proctool not currently installed for OS \"$OS\" ($SIZ bit) on $CHIP processor"
fi
