#! /bin/csh -f
#########################################################################
## Install : csh script for Installing programs in the PlaNet system.  ##
#########################################################################

###############################################
## 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
set MAIL = /usr/ucb/mail
# set ARCH = hp # set to machine architecture name if the
		# machine does not have the program arch.
		# eg. hp (for HP bobcats).

###################################################
## Error handling - send mail to the installer. ###
###################################################
alias ERR 'set ErrMsg = \!* ; goto HandleError '

########################################
## 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

MSG "		This is a PlaNet Installation Script."

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

if( $?ARCH ) then
  $SAY ARCH=$ARCH
else if( -x /bin/arch ) then
  set ARCH = `/bin/arch`
else if( -x /usr/bin/arch ) then
  set ARCH = `/usr/bin/arch`
else if( -x /usr/local/bin/arch ) then
  set ARCH = `/usr/local/bin/arch`
endif

if( ! $?ARCH ) then
	ERR "Program arch not available.  Please set the variable\
ARCH in me to the name of the architecture and run me again."
endif

if( $ARCH == hp ) then
  alias SAYN "$SAY \!* '\c'"
else
  alias SAYN "$SAY -n"
endif

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

if( ! -f src/sunnet.c ) then
  ERR "I cannot find the source directory in this directory ($cwd).  Please \
run me in the directory in which I reside."
endif

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

###########################################
## Check which window system we are in.  ##
###########################################
if( $#argv > 0 && $1 =~ *Net ) then
    set PROG = $1
    shift
    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( $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

PROG.CHOSEN:

$SAY "For this configuration, I will install the program $ARCH/$PROG.\
"

#############################################
## Install the main program if not there.  ##
#############################################

if( ! -d $ARCH ) mkdir $ARCH
if( ! -w $ARCH ) then
  ERR "You don't have the write permission for directory $ARCH."
endif
if( -f $ARCH/$PROG & ! -x $ARCH/$PROG ) /bin/rm $ARCH/$PROG

TryInstall:

$SAY "Installing $ARCH/$PROG..."
switch( $PROG )
case SunNet:
  set objDir = sunnet.$ARCH
  set makeDir = sunnetDir
  breaksw
case XNet:
  set objDir = xnet.$ARCH
  set makeDir = xnetDir
  breaksw
case StarNet:
  set objDir = starnet.$ARCH
  set makeDir = sunnetDir
  breaksw
case PlaNet:
  set objDir = planet.$ARCH
  set makeDir = planetDir
  breaksw
endsw

if( ! -d $objDir ) mkdir $objDir

## machines that don't understand VPATH in makefile: needs linking of the
## source files.
if( $ARCH == dec-mips & ! -f $objDir/sunnet.c ) make "xnetDir = $objDir" link
if( $ARCH == hp & ! -f $objDir/sunnet.c ) make "xnetDir = $objDir" link

$SAY "make $* '$makeDir = $objDir' $PROG"
if( $#argv > 0 ) then
  make "$*" "$makeDir = $objDir" $PROG; set error = $status
else
  make "$makeDir = $objDir" $PROG; set error = $status
endif

if( $error ) then
  ERR "cannot install program $cwd/$ARCH/$PROG"
endif

if( $objDir != $ARCH & ! -e $ARCH/$PROG ) then
  cd $ARCH
  ln -s ../$objDir/$PROG $PROG
  cd ..
endif

$SAY "$ARCH/$PROG installed successfully."

#########################################
## Make sure we can use 'netcommand'.  ##
#########################################

if( ($ARCH == dec-mips | $ARCH == hp) & ! -f $ARCH/netcommand.c ) then
  cd $ARCH; ln -s ../src/port.h ../src/netcommand.c . ; cd ..
endif

make "sunnetDir = $ARCH" netcommand
if( ! -x $ARCH/netcommand ) then
  $SAY "	Installing $ARCH/netcommand ..."
  cc src/netcommand.c -o $ARCH/netcommand
endif
if( ! -x $ARCH/netcommand ) then
  ERR "cannot install $cwd/$ARCH/netcommand"
else
  $SAY "$ARCH/netcommand installed successfully."
endif

set setDir = `egrep 'set[ 	]*NETDIR[ 	]*=' RunNet`

if( "$setDir" != "set NETDIR = $cwd" ) then
  $SAY "Modifying RunNet."
  sed -e "/set[ 	]*NETDIR[ 	]*=/c\\
set NETDIR = $cwd" RunNet >! RunNet.tmp
  /bin/mv RunNet.tmp RunNet
  if( -f RunNet.ksh ) then
    $SAY "Modifying RunNet.ksh."
    sed -e "/NETDIR[ 	]*=/c\\
NETDIR=$cwd" RunNet.ksh >! RunNet.tmp
    /bin/mv RunNet.tmp RunNet.ksh
  endif
endif

exit 0

HandleError:
MSG "	Sorry," $ErrMsg
exit 1
