#!/bin/csh -f

#ifndef _BLURB_
#define _BLURB_
#/*
#
#            Coda: an Experimental Distributed File System
#                             Release 3.1
#
#          Copyright (c) 1987-1995 Carnegie Mellon University
#                         All Rights Reserved
#
#Permission  to  use, copy, modify and distribute this software and its
#documentation is hereby granted,  provided  that  both  the  copyright
#notice  and  this  permission  notice  appear  in  all  copies  of the
#software, derivative works or  modified  versions,  and  any  portions
#thereof, and that both notices appear in supporting documentation, and
#that credit is given to Carnegie Mellon University  in  all  documents
#and publicity pertaining to direct or indirect use of this code or its
#derivatives.
#
#CODA IS AN EXPERIMENTAL SOFTWARE SYSTEM AND IS  KNOWN  TO  HAVE  BUGS,
#SOME  OF  WHICH MAY HAVE SERIOUS CONSEQUENCES.  CARNEGIE MELLON ALLOWS
#FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION.   CARNEGIE  MELLON
#DISCLAIMS  ANY  LIABILITY  OF  ANY  KIND  FOR  ANY  DAMAGES WHATSOEVER
#RESULTING DIRECTLY OR INDIRECTLY FROM THE USE OF THIS SOFTWARE  OR  OF
#ANY DERIVATIVE WORK.
#
#Carnegie  Mellon  encourages  users  of  this  software  to return any
#improvements or extensions that  they  make,  and  to  grant  Carnegie
#Mellon the rights to redistribute these changes without encumbrance.
#*/
#
#static char *rcsid = "$Header: newvenus,v 3.2.1.1 95/10/11 10:18:32 raiff Exp $";
#endif /*_BLURB_*/


#
# Install a new venus on a machine.
#

#
# Must be run as root.  There should be no open file descriptors
# in Venus when this is run.
#

#
# A hack to determine what @sys is when we don't necessarily get
# fs (like, on a portable)
# 

set pmaxP
set i386P
set rtP

if (-d /usr/misc/.gnu-emacs) then
	pushd /usr/misc/.gnu-emacs >> /dev/null
	set pmaxP = `pwd | grep pmax_mach`
	set i386P = `pwd | grep i386_mach`
	set rtP = `pwd | grep rt_mach`
	popd >> /dev/null
else
	# we're probably on a 386
	echo -n "Enter @sys value [i386_mach] "
	set systype = "$<"
	if ("$systype" == "") set systype = "i386_mach"
	set pmaxP = `echo $systype | grep -i pmax`
	set i386P = `echo $systype | grep -i 386`
	set rtP = `echo $systype | grep -i rt`
endif

if ($#pmaxP == 0 && $#i386P == 0 && $#rtP == 0) then
	echo "Cannot determine system type, or system not supported"
	exit -1
endif

# CODADIR is the directory where local Coda binaries live
if (! ${?CODADIR}) then
	set CODADIR = /usr/coda/etc
endif

# CODACACHE is the directory where venus.cache for Coda lives
if (! ${?CODACACHE}) then
	set CODACACHE = /usr/coda/venus.cache
endif

# CODAROOT is the installation directory *within* Coda
# used as a first try
if (! ${?CODAROOT}) then
	set CODAROOT = /coda/project/coda/alpha/bin
endif

# CODARFSROOT is the installation directory if Coda is not
# running on this machine.
if (! ${?CODARFSROOT}) then 
	if ($#pmaxP != 0) then 
	    set CODARFSROOT = /../bach/coda/project/coda/alpha/bin
        else if ($#i386P != 0) then
            set CODARFSROOT = /../weber/coda/project/coda/alpha/bin
        else if ($#rtP != 0) then
            set CODARFSROOT = /../strauss/coda/project/coda/alpha/bin
        else
            echo "No system type set"
	    exit -1
	endif
        endif
        endif
endif

echo $CODARFSROOT
exit

# COPY is the preferred copy program.  Creation times and
# modes should be preserved.
if (! ${?COPY}) then
	set COPY = "cp -p "
endif

#
# Now, try to copy the file, first from the user's
# coda space, then from the rfs machine's.
#
# Copy venus.stripped if both of the following are the
# case:
#       venus.stripped exists
#  and  venus.stripped has a creation date at least as
#                      great as venus in the same directory

# First, get to an available Coda namespace

if (-d ${CODAROOT}) then
	pushd ${CODAROOT}
else if (-d ${CODARFSROOT}) then
	pushd ${CODARFSROOT}
else
	echo "Cannot find Venus binary"
	exit -1	
endif
endif

if (! -f ${cwd}/venus) then
	echo "Venus not installed in "$cwd
	exit -1
endif

# If a venus.stripped exists, and is newer than venus, get it

set predicate = `find . -newer venus -name venus.stripped -print`

if ($#predicate == 1) then
	set TARGET = venus.stripped
else
	set TARGET = venus
endif

echo "Fetching new Venus"

$COPY ${cwd}/${TARGET} ${CODADIR}/venus.new

# get out of Coda.  We don't want to be here when SHTF
popd

echo "Shutting down Venus"

${CODADIR}/vutil -shutdown

# let the shutdown complete
sleep 15

echo "Relinking Venus"

mv ${CODADIR}/venus ${CODADIR}/venus.old
mv ${CODADIR}/venus.new ${CODADIR}/venus

echo -n "Starting new Venus "

if (${?CODA_NO_INIT}) then
	echo "with old RVM contents"
	/usr/coda/etc/venus -console /usr/coda/etc/console &
else
	echo "and reinitializing"
	touch /usr/coda/venus.cache/INIT
	/usr/coda/etc/venus -console /usr/coda/etc/console &
endif

sleep 10

#check to see if there is some irreparable problem:

set problemline = `head -1 /usr/coda/etc/console | grep '(16)'`

if ($#problemline != 0) then
	echo "A process that held a reference to a file in Coda was"
	echo "active when Venus was shut down.  There are two ways"
	echo "out of this situation.  The first is to reboot the"
	echo "machine.  The second is to identify the process, either"
	echo "kill it or force it to give up the reference, and then"
	echo "start venus with the following command (as root):"
	echo
	if (${?CODA_NO_INIT}) then
		echo '/usr/coda/etc/venus -console /usr/coda/etc/console &'
	else
		echo '/usr/coda/etc/venus -console -init /usr/coda/etc/console &'
	endif
	exit -1
endif

#check to see if there is a repair-able problem

set problemline = `head -1 /usr/coda/etc/console | grep '(2)'`

if ($#problemline != 0) then
    if (${?CODA_NO_INIT}) then
	/usr/coda/etc/venus -console /usr/coda/etc/console &
    else
	touch /usr/coda/venus.cache/INIT
	/usr/coda/etc/venus -console /usr/coda/etc/console &
    endif
    sleep 10
endif

#if there is still a problem, we're hosed.

set problemline = `head -1 /usr/coda/etc/console | grep failed`

if ($#problemline != 0) then
	echo "Venus cannot be started.  The error message is:"
	echo $problemline
	echo
	echo "Rebooting the machine should work."
	exit -1
endif

echo "New Venus Installed."
echo
echo "If space is a consideration, you may want to remove the"
echo "old binary "${CODADIR}"/venus.old."

