##############################################################
## RunNet : csh script for running PlaNet system from csh.  ##
## this script should be run as 'source RunNet'.     	    ##
##############################################################
###############################################
## Change these according to specific site.  ##
###############################################

set AUTHOR = miyata@dendrite.colorado.edu
### set this to email address of the person responsible for installing
### the programs who should be notified in case of installation problems.
set INSTALLER = # $AUTHOR

if( -x /usr/ucb/mail ) then
  set MAIL = /usr/ucb/mail
else if ( -x /bin/mail ) then
  set MAIL = /bin/mail
else
  set MAIL = mail
endif

#############################################
## Change this to the main directory.      ##
#############################################
set NETDIR = /soma/users/miyata/planet

# set ARCH = hp	# set to machine architecture name if the
		# machine does not implement the program arch.
		# eg. hp (for HP bobcats).

########################################
## Some primitive user interface .. ####
########################################
set SAY = /bin/echo
if( -x /usr/ucb/fmt ) set FMT = /usr/ucb/fmt

if( $?FMT ) then
  alias MSG '$SAY \\
\!* | $FMT'
else
  alias MSG '$SAY \\
\!* '
endif
###################################################
## Error handling - send mail to the installer. ###
###################################################
alias ERR 'set ErrMsg = \!* ; goto HandleError '


MSG  "		Welcome to PlaNet \!\!"

###################################################
## Find out what architecture this machine is #####
###################################################

if( -x /bin/arch | -x /usr/bin/arch | -x /usr/local/bin/arch ) then
  set ARCH = `arch`
endif

if( ! $?ARCH ) then
	ERR "Program arch not found on your machine.  Please set the variable\
ARCH in file RunNet to the name of the architecture and run again."
endif

#########################################
## Check if it's the right directory.  ##
#########################################

if( ! -d $NETDIR ) then
  MSG "I cannot find the directory $NETDIR.  Please set variable NETDIR\
in file RunNet to the appropriate directory and try again."
  exit
endif
set DIR = $cwd

#######################################
## Find out what machine we are on.  ##
#######################################
set HOST = `hostname`
$SAY "Your machine $HOST is a $ARCH, "

##################################################################
## Choose program appropriate for the window system we are in.  ##
##################################################################

if( $?NETPROG ) then
  set PROG = $NETPROG
  goto PROG.CHOSEN
endif

if( $?DISPLAY ) then
  $SAY "and I assume your window system is X (with server $DISPLAY)."
  set PROG = XNet
else if( $?WINDOW_ME ) then
  $SAY "and your window system is suntools."
  set PROG = SunNet
else if( $ARCH =~ sun* ) then
  $SAY "and it appears you are not in a window system."
  set PROG = StarNet
#else if( $TERM == sun ) then
  #$SAY "and I assume your window system is suntools."
  #set PROG = PlaNet
else
  $SAY "and it appears you are not in a window system."
  set PROG = StarNet

endif

######################
## Label the window ##
######################

# if( $PROG =~ SunNet ) then
  # alias LABEL '$SAY -n "${E}]l$PROG \!*${E}\"'
  # LABEL
# else
  # alias LABEL
# endif

#####################################################
## If from suntools, make sure we are in gfxtool.  ##
#####################################################

if( $?WINDOW_ME ) then
 if( $WINDOW_ME == $WINDOW_GFX ) then
    MSG  "	Not in a graphics tool window.  Graphics disabled"
    set PROG = StarNet
 endif
endif

PROG.CHOSEN:

MSG "For this configuration, we are going to use the program $NETDIR/$ARCH/$PROG.\
"

##############################################################
## Make sure we are authorized to connect to the X server.  ##
##############################################################

if( $PROG == XNet ) then
  set X = `xhost` ; if( $status == 0 ) goto OK
  MSG  $X
  MSG  "We cannot connect to the X server. Please run 'xhost\
$HOST' on the server machine and run me again."
  exit;
endif
OK:

#########################################################################
## Secure a command port.  Use /tmp/planet.$USER.x  or port.tutorial.  ##
#########################################################################

if ( ! $?USER ) then
  MSG "Please do 'set USER = your-login-name' and run me again."
  exit
endif
@ port = 0
while( -e /tmp/planet.$USER.$port )
  @ port++
end
setenv NETPORT /tmp/planet.$USER.$port

##############################
## Start the main program.  ##
##############################

if( ! -x $NETDIR/$ARCH/$PROG ) then
  MSG "$NETDIR/$ARCH/$PROG not installed.  Run '$NETDIR/Install $PROG'."
  exit
endif

MSG  "\
	Running $NETDIR/$ARCH/$PROG in the background."
if( $PROG == PlaNet ) then
  $NETDIR/$ARCH/$PROG &
else if( $PROG == XNet & $?XNetARGS ) then
  $NETDIR/$ARCH/$PROG $XNetARGS > /dev/null &
else
  $NETDIR/$ARCH/$PROG > /dev/null &
endif

alias netcommand $NETDIR/$ARCH/netcommand

######################
## Run netcsh file ###
######################

MSG  "\
	Running $NETDIR/netcsh."
source $NETDIR/netcsh

$SAY -n "Checking $PROG..  "
netcommand whatis eta > /dev/null
if( $status ) then 
   ERR "There appears to be a problem with $PROG."
endif

setintr

MSG  "\
	$PROG is ready.  Type in any $PROG commands."

#unalias MSG
#unset $NETDIR
#unset $DIR
#unset $PROG
#unset SAY
#unset FMT
#unalias MSG
exit

###################################################
## Error handling - send mail to the installer. ###
###################################################
HandleError:
MSG "	Sorry," $ErrMsg
if( $?INSTALLER & ${INSTALLER}null != null ) then
  MSG "	Notifying $INSTALLER... $INSTALLER will let you know when\
the problem is fixed."
  MSG "$ErrMsg for user $USER on machine $HOST" | $MAIL $INSTALLER
endif
exit
