#!/bin/sh
#-----------------------------------------------------------------------
#;  Copyright (C) 1995-2002, 2005-2006
#;  Associated Universities, Inc. Washington DC, USA.
#;
#;  This program is free software; you can redistribute it and/or
#;  modify it under the terms of the GNU General Public License as
#;  published by the Free Software Foundation; either version 2 of
#;  the License, or (at your option) any later version.
#;
#;  This program is distributed in the hope that it will be useful,
#;  but WITHOUT ANY WARRANTY; without even the implied warranty of
#;  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#;  GNU General Public License for more details.
#;
#;  You should have received a copy of the GNU General Public
#;  License along with this program; if not, write to the Free
#;  Software Foundation, Inc., 675 Massachusetts Ave, Cambridge,
#;  MA 02139, USA.
#;
#;  Correspondence concerning AIPS should be addressed as follows:
#;         Internet email: aipsmail@nrao.edu.
#;         Postal address: AIPS Project Office
#;                         National Radio Astronomy Observatory
#;                         520 Edgemont Road
#;                         Charlottesville, VA 22903-2475 USA
#-----------------------------------------------------------------------
# Usage: XASERVERS
#-----------------------------------------------------------------------
#
# Start AIPS TV, graphics and message servers on an X workstation.
# MUST be invoked via $AIPS_ROOT/START_TVSERVERS which is responsible for
# defining essential AIPS env.vars (including TVDEV, etc) via HOSTS.SH,
# TVDEVS.SH, and AIPSPATH.SH, as well as DISPLAY.  This script uses
# Internet domain sockets; see UNIXSERVERS for Unix Domain Socket use.
#
# RESOURCES:  If XAS starts first it grabs most of the colormap
# ----------  resources leaving other X clients bereft.  XAS will use
# a virtual colormap if it has to.  See AIPStv*maxGreyLevel below; set
# this to a smaller value if you wish (but no smaller than 111).
#
# XAS resources:  The following may be used to customize XAS:
#
#   AIPStv*cursorB:         Amount of blue  in cursor, 0-255
#   AIPStv*cursorG:         Amount of green in cursor, 0-255
#   AIPStv*cursorR:         Amount of red   in cursor, 0-255
#   AIPStv*cursorShape:     Number representing cursor shape, e.g. 30
#   AIPStv*geometry:        X11 geometry argument, e.g. 518x518-0-125
#   AIPStv*graphics1B:      Amount of blue  in graphics plane 1
#   AIPStv*graphics1G:      Amount of green in graphics plane 1
#   AIPStv*graphics1R:      Amount of red   in graphics plane 1
#
#   (similarly graphics2B, ... graphics4R)
#
#   AIPStv*maxGreyLevel:    Number of colors for XAS to use, max 199
#   AIPStv*useSharedMemory: 1 or 0 to use or not (MIT shared mem Xtens.)
#   AIPStv*maxCommDelay:    Max # of X instructions to cache.
#
# OTHER SERVERS: This script will also start the AIPS message and
# -------------  Tektronix (graphics) servers within xterms by default.
# The terminal emulator actually runs the TEKSRV or MSGSRV program.
# In theory, any terminal emulator may be used provided that it has the
# necessary capabilities.  The actual emulators used are controlled by
# the following environment variables:
#
#   Variable            Description                         Default
#   ----------------------------------------------------------------
#   AIPS_TEK_EMULATOR   Graphics (Tektronix) output         xterm
#   AIPS_MSG_EMULATOR   Task (text) output other than AIPS  xterm
#
# The emulator for graphics will have the X11 name "AIPStek" and the
# one for text "AIPSmsg".  Any X resources appropriate for the emulator
# actually used may be specified in the users' .Xdefaults or
# .Xresources files.
#
# The string "none" is special and requests that the server not be
# started.  If a program name is given it must be one in the user's
# command search path (even for rsh/remsh) or must be a full pathname.
#
# Any terminal emulator used must be capable of running a command
# specified on the command line.  Most terminal emulators allow
# such a command to be specified using the flag "-e", including xterm,
# aixterm, hpterm and most derivatives of xterm.  If one of the terminal
# emulators requires a different flag it should be specified in the
# environmental variable AIPS_TEK_EXE_FLAG and/or AIPS_MSG_EXE_FLAG.
# Also, the variable AIPS_TEK_NAME_FLAG and AIPS_MSG_NAME_FLAG should be
# changed (in your .login or .profile) if you use an emulator other than
# xterm.
#
#  Solaris version cannot have quotes in the xterm command line
#-----------------------------------------------------------------------
#                                       Make sure that X programs are in
#                                       the path in case this script is
#                                       being run via remote shell.
#                                       If TEKSRV/MSGSRV start locally
#                                       but not remotely, you need to
#                                       add to the following list of
#                                       places where X programs might be
#                                       hiding.
for xdir in /usr/bin/X11 \
            /usr/X11R6/bin \
            /usr/X11/bin \
            /usr/X386/bin \
            /usr/Openwin/bin \
            /usr/openwin/bin \
            /usr/local/bin \
            /usr/local/bin/X11 \
            /usr/local/X11/bin \
            /opt/local/bin \
            /opt/local/bin/X11 \
            /opt/local/X11/bin \
            /usr/local/X11R6/bin \
            /usr/local/X11R5/bin \
            /usr/local/X11R4/bin
#                                      If you add to this list remember
#                                      to end every line except the
#                                      last with a backslash
do
   [ -d $xdir ] && PATH=$PATH:$xdir    # Append to end of path so that
                                       # the user's preferences are
                                       # kept intact
done
#                                       Set defaults if necessary
[ -z "$AIPS_TEK_EMULATOR" ] && AIPS_TEK_EMULATOR=xterm
[ -z "$AIPS_MSG_EMULATOR" ] && AIPS_MSG_EMULATOR=xterm
[ -z "$AIPS_TEK_EXE_FLAG" ] && AIPS_TEK_EXE_FLAG="-e"
[ -z "$AIPS_MSG_EXE_FLAG" ] && AIPS_MSG_EXE_FLAG="-e"
#                                       Default naming convention
#                                       Watch out!  -title takes 1 arg!
if [ -z "$AIPS_TEK_NAME_FLAG" -a "$AIPS_TEK_EMULATOR" = "xterm" ] ; then
   AIPS_TEK_NAME_FLAG="-title AIPS_TEKSRV -n TEKSRV -name AIPStek"
   AIPS_TEK_NAME_FLAG="$AIPS_TEK_NAME_FLAG -iconic"
fi
if [ -z "$AIPS_MSG_NAME_FLAG" -a "$AIPS_MSG_EMULATOR" = "xterm" ] ; then
   AIPS_MSG_NAME_FLAG="-title AIPS_MSGSRV -n MSGSRV -name AIPSmsg"
fi
#                                       allow quiet output
ei6 () {
  if [ "$AIPS_QUIET" = "" ] ; then
    echo "XASERVERS: $*"
  fi
}
#                                       Use which variant of "ps"...
#                                       (if GNU ps, this won't work)
case $ARCH in
   SOL*|SUL|SGI|HP*) psf="-ef";;
   *) psf="auxww";;
esac
#                                       Double-check we got it right
[ "$AIPSTMP" = "" ] && AIPSTMP=/tmp
tfil=${AIPSTMP}/${HOST}.${LOGNAME}.DELETEME.$$
rm -f $tfil
ps $psf >/dev/null 2>$tfil
if [ -s $tfil ] ; then
#                                       We got the wrong one.  Switch.
   if [ $psf = auxww ] ; then
      psf="-ef"
   else
      psf="auxww"
   fi
   rm -f $tfil
#                                       Try it again; it should work now
   ps $psf >/dev/null 2>$tfil
   if [ -s $tfil ] ; then
#                                       It didn't.  Abandon ship!
      pscmd=`type ps | head | awk '{print $NF}'`
      ei6 "Neither 'ps -ef' nor 'ps auxww' work on $ARCH?"
      ei6 "Check your path to make sure the right version of the 'ps'"
      ei6 "command is being used.  One of '-ef' or 'auxww' should work."
      if [ "$pscmd" = "" ] ; then
         ei6 "*NO* ps command found in your path???"
	 ei6 "PATH = $PATH"
      else
         ei6 "First 'ps' in your path is $pscmd"
      fi
      ei6 "NOT starting any servers; cannot use 'ps'."
      exit 1
   fi
fi
rm -f $tfil
if [ "$LOAD" = "" ] ; then
   ei6 "LOAD undefined, cannot start servers on $TVHOST"
   exit 1
fi
#                                       -------------------------------
#                                       Start TV servers first; if you
#                                       are using ssh, this prevents
#                                       problems on closing the link.
#                                       -------------------------------
if [ "$TVDEV" != TVDEV00 ] ; then
   ps $psf | grep -v grep | grep -v START | grep TVSERV\.EXE >/dev/null
   if [ $? -eq 1 ] ; then
      ei6 "Start TV LOCK daemon TVSERV on $TVHOST"
      ( cd $LOAD; ( cd $LOAD; ./TVSERV.EXE & ) & )
   else
#                                       See who is running it.
      cmd=`ps $psf | grep -v grep | grep -v START | grep TVSERV`
      who=`echo $cmd | awk '{print $1}'`
      [ "$who" = "" ] && who="(unknown)"
      msg="TVSERV is already running on host $TVHOST, user $who"
      ei6 $msg
   fi
#                                       Start XAS if necessary.
   ps $psf | grep -v grep | grep -v XASERVERS | grep -v 'XAS[1-9A-Z]' \
           | grep XAS >/dev/null
   if [ $? -eq 1 ] ; then
      ei6 "Start XAS on $TVHOST, DISPLAY $DISPLAY"
      ( cd $LOAD; ./XAS -display $DISPLAY \
        -t "X-AIPS tv Screen Server 98 - INET" -n "AIPS98-INET" & )
   else
#                                       See who is running it.
      cmd=`ps $psf | grep -v grep | grep -v XASERVERS \
                   | grep -v 'XAS[1-9A-Z]' | grep XAS`
      who=`echo $cmd | awk '{print $1}'`
      dis=`echo $cmd | awk '{for (i=1; i<NF; i++)
                                if ($i == "-display") print $(i+1)}'`
      [ "$who" = "" ] && who="(unknown)"
      [ "$dis" = "" ] && dis="(unknown)"
      msg="XAS    is already running on host $TVHOST,"
      msg="$msg display $dis, user $who"
      ei6 $msg
   fi
fi
#                                       Start TEKSRV
if [ "$TKDEV" != "TKDEV00" -a "$AIPS_TEK_EMULATOR" != "none" ] ; then
   ps $psf | grep -v grep | grep TEKSRV\.EXE >/dev/null
   if [ $? -eq 1 ] ; then
#                                       Not found so start a new one.
#                                       Leave -display first!
#                                       Detection below needs them.
      ei6 "Start graphics server TEKSRV on $TVHOST, DISPLAY $DISPLAY"
      ($AIPS_TEK_EMULATOR -display $DISPLAY $AIPS_TEK_NAME_FLAG \
                         $AIPS_TEK_EXE_FLAG $LOAD/TEKSRV.EXE &)
   else
#                                       Two processes will be running:
#                                       the terminal emulator and the
#                                       TEKSRV.EXE itself.  Other Unix-
#                                       based TKSRVn's may be there too.
#                                       As xterm may be set-uid root,
#                                       need to look at both processes.
#                                       The xterm has display info, and
#                                       the other has the user info.
      cmd=`ps $psf | grep -v grep | grep TEKSRV | grep -v TKSRV | \
           grep display`
      cmd2=`ps $psf | grep -v grep | grep TEKSRV\.EXE | grep -v display`
      who=`echo $cmd2 | awk '{print $1}'`
      dis=`echo $cmd | awk '{for (i=1; i<NF; i++)
                          if ($i == "-display") print $(i+1)}'`
      [ "$who" = "" ] && who="(unknown)"
      [ "$dis" = "" ] && dis="(unknown)"
      msg="TEKSRV is already running on host $TVHOST,"
      msg="$msg display $dis, user $who"
      ei6 $msg
   fi
fi
#                                       Start MSGSRV.
if [ "$TTDEV" != "TTDEV00" -a "$AIPS_MSG_EMULATOR" != "none" ] ; then
   ps $psf | grep -v grep | grep MSGSRV\.EXE >/dev/null
   if [ $? -eq 1 ] ; then
#                                       Not found so start a new one.
#                                       Leave -display first!
#                                       Detection below needs them.
      ei6 "Start message server MSGSRV on $TVHOST, DISPLAY $DISPLAY"
      ($AIPS_MSG_EMULATOR -display $DISPLAY $AIPS_MSG_NAME_FLAG \
                         $AIPS_MSG_EXE_FLAG $LOAD/MSGSRV.EXE &)
   else
#                                       see above for the gory details.
      cmd=`ps $psf | grep -v grep | grep MSGSRV | grep -v MSSRV | \
           grep display`
      cmd2=`ps $psf | grep -v grep | grep MSGSRV\.EXE | grep -v display`
      who=`echo $cmd2 | awk '{print $1}'`
      dis=`echo $cmd | awk '{for (i=1; i<NF; i++)
                                if ($i == "-display") print $(i+1)}'`
      [ "$who" = "" ] && who="(unknown)"
      [ "$dis" = "" ] && dis="(unknown)"
      msg="MSGSRV is already running on host $TVHOST,"
      msg="$msg display $dis, user $who"
      ei6 $msg
   fi
fi

