:
#
#  This script depends on BSD-like output from the who command.  If
#  the who command does not provide the remote hostname, this script
#  will fail to automatically determine the display name.
#
#  You should make sure your PATH is set to include the X applications
#  such as the window manager.
#

#
#  Set the default window manager.  We'll use uwm.
#
DFT_WM=uwm

#
#  The WM can be inherited from the environment or it can be specified
#  on the command line as the first argument.  If neither of these
#  cases results in a window manager, use the default defined above.
#
WM=${WM-$DFT_WM}
WM=${1-$WM}

#
#  Figure out the name of the tty device we're using.  We don't
#  want the /dev/ part, however.
#
TTY=`tty | awk -F/ '{print $NF}'`

#
#  Now get the last field of the line of who output that corresponds
#  to the tty we're using.  This should be the remote host name or
#  address from which we've connected.  If we've connected via the
#  telnet session, this will be the host name or address of the NCD
#  unit.
#
REMHOST=`who | awk '$2=="'"$TTY"'" {print $NF}'`

#
#  If we're running in a window on the NCD unit we'll have an address
#  of the form "(addr:0.0)".  If this is the case we want only the name
#  part.  If we've connected from the telnet session, we'll have an
#  address of the form "(addr)".
#
DISPLAY_HOST=`expr X"$REMHOST" : X'(\(.*\):' '|' X"$REMHOST" : X'(\(.*\))' '|' UNKNOWN`

#
#  Make sure we have a DISPLAY_HOST.
#
if test "$DISPLAY_HOST" = "UNKNOWN"
then
    echo "A display could not be determined.  Please enter the name or"
    echo "address (e.g., 192.43.153.79) of your NCD unit."
    read DISPLAY_HOST
fi

#
#  The display we'll use is then "addr:0".
#
DISPLAY="$DISPLAY_HOST":0

#
#  Now start the appropriate window manager directing all standard output
#  and error output to the file named using the window managers name.
#
echo "Starting $WM on $DISPLAY."
WM_NAME=`basename $WM`
( $WM > $WM_NAME.output 2>&1 & )

#
#  You may also want to keep an eye on `basename $WM`.output.  If
#  so, uncomment the lines below.
#
#sleep 5
#xterm -geometry 30x10-5-5 -name wmoutput -T ${WM}Output -e tail -f $WM_NAME.output &
