#!/bin/sh
# Copyright 2000, International Business Machines Corporation and others.
# All Rights Reserved.
# 
# This software has been released under the terms of the IBM Public
# License.  For details, see the LICENSE file in the top-level source
# directory or online at http://www.openafs.org/dl/license10.html

# afs.rc: rc script for AFS on SGI Irix platforms
#
# Install this script as /etc/init.d/afs
# then make links like this:
# ln -s ../init.d/afs /etc/rc0.d/K35afs
# ln -s ../init.d/afs /etc/rc2.d/S35afs 
#
# Config Files (in /etc/config)
#	afsclient - machine should be an AFS client
#	afsserver - machine should be an AFS server
#	afsxnfs   - machine should be an AFS/NFS translator (must also be
#			an AFS client)
#	afsml	  - dynamically load afs (using ml(1M))
#
# Option files (in /etc/config)
#	afsd.options - options to pass to afsd. If present it overrides
#			the defaults here.
#
# The following are 'recommended' values for various sized machines.
# These options should be given in /etc/config/afsd.options.
# If afsd.options does not exist then $OPTIONS is used.
#

# EXTRAOPTS can be used to enable/disable AFSDB support (-afsdb)
# and Dynroot (dynamically-generated /afs) support (-dynroot).
EXTRAOPTS="-afsdb"

LARGE="-stat 2800 -dcache 2400 -daemons 5 -volumes 128"
MEDIUM="-stat 2000 -dcache 800 -daemons 3 -volumes 70"
SMALL="-stat 300 -dcache 100 -daemons 2 -volumes 50"
OPTIONS="$MEDIUM $EXTRAOPTS"

CONFIG=/etc/config
IS_ON=/etc/chkconfig
AFSDOPT=$CONFIG/afsd.options
AFSMT=/afs
COMMON_CLIENT=0

# The verbose flag controls the printing of the names of daemons as they
# are started 

if $IS_ON verbose ; then
    ECHO=echo
    VERBOSE=-v
else		# For a quiet startup and shutdown
    ECHO=:
    VERBOSE=
fi

#
# Run afsd in -nosettime mode if we have an alternative time sync
# OR we are a server and a client machine
#
if $IS_ON timed || $IS_ON timeslave || \
		( $IS_ON afsserver && $IS_ON afsclient ) ; then
    TIMESYNC=-nosettime
fi

case "$1" in
'netstart')
	# The network startup script (/etc/init.d/network) used to run
	# "/etc/init.d/afs netstart" to accomodate the loading of AFS 3.3
	# at a particular time relative to NFS. In AFS 3.4, we need to
	# start up after the network script has completed, so the 'netstart'
	# option is now a no-op and the 'start' option loads AFS.
	;;
'start')

	if $IS_ON afsml
	then
		#
		# Figure out what kind of machine we're on.
		#
		CPU=`hinv -t cpu`
		OS_VERS=`uname -r`
		if echo $OS_VERS | fgrep 6.4 > /dev/null
		then
			COMMON_CLIENT=1
		elif echo $CPU | fgrep R3000 >/dev/null
		then
			CPU=R3000
		elif echo $CPU | egrep 'R4000|R4400|4600' >/dev/null
		then
			# R4xxx series can be either 32 or 64 bit kernels.
			# Based on OS and CPUBOARD
			CPU=R4000
			if echo $OS_VERS | fgrep 6.2 > /dev/null
			then
				IP_TYPE=`uname -m`
				if echo $IP_TYPE | egrep '17|20|22' > /dev/null
				then
					CPU=R4000_32
				fi
			fi
		elif echo $CPU | egrep R5000 >/dev/null
		then
			# For 6.3, we have an R5000 library.
			if echo $OS_VERS | fgrep 6.3 > /dev/null
			then
				CPU=R5000
			else
				CPU=R4000_32
			fi
		elif echo $CPU | egrep R8000 >/dev/null
		then
			CPU=R8000
		elif echo $CPU | egrep R10000 >/dev/null
		then
			CPU=R10000
		else
			echo "Cannot determine system type: $CPU not understood"
			echo "Aborting AFS start"
			exit 1
		fi

		#
		# Figure out if the machine is a multiprocessor or uniprocessor
		# or both
		#
		if [ $COMMON_CLIENT = 1 ]
		then
			#
			# Figure out if we need the .nonfs library
			#
			if [ -r /etc/havenfs -a /etc/havenfs ];
			then
				NAME="libafs.o"
			else
				NAME="libafs.nonfs.o"
			fi
		else
			ARCH=`hinv | awk '/Processor/ && /MHZ/ {print $1}'`
			if test `echo $ARCH | wc -w` -gt 1
			then
				ARCH=MP
			elif test $ARCH -eq 1
			then
				ARCH=SP
			elif test $ARCH -gt 1
			then
			ARCH=MP
			else
	                        echo "Cannot determine if system is MP or SP"
	                        echo "Aborting AFS start"
	                        exit 1
			fi

			#
			# Figure out if we need the .nonfs library
			#
			if [ -r /etc/havenfs -a /etc/havenfs ];
			then
				NAME="libafs.${ARCH}.${CPU}.o"
			else
				NAME="libafs.${ARCH}.${CPU}.nonfs.o"
			fi
		fi
		# End of COMMON_CLIENT

		if [ -r /usr/afs/bin/sgiload/$NAME ]
		then
			MOD=/usr/afs/bin/sgiload/$NAME
		elif [ -r /usr/vice/etc/sgiload/$NAME ]
		then
			MOD=/usr/vice/etc/sgiload/$NAME
		else
			echo "Cannot find afs kernel library: $NAME"
			echo "Aborting AFS start"
			exit 1
		fi

		$ECHO "Loading AFS from $MOD...\c"
		if ml ld $VERBOSE -j $MOD -p Afs_ -a afs
		then
			:
		else
			echo "Dynamic load of AFS failed - Aborting AFS start"
			exit 1
		fi
	else
		$ECHO "Not loading AFS kernel extensions - afsml config option is off"
	fi

	if $IS_ON afsserver && test -x /usr/afs/bin/bosserver 
	then
		$ECHO "Starting AFS bosserver"
		/usr/afs/bin/bosserver >/dev/console 2>&1
	fi

	if $IS_ON afsclient && test -x /usr/vice/etc/afsd ; then
		if test -s /usr/vice/etc/ThisCell ; then
			MYCELL=`cat /usr/vice/etc/ThisCell`
		else
			echo "/usr/vice/etc/ThisCell not valid - Aborting AFS start"
			exit 1
		fi
		if test -r $AFSDOPT ; then
			OPTIONS=`cat $AFSDOPT`
		fi
		if $IS_ON afsxnfs ; then
			OPTIONS="$OPTIONS -rmtsys"
		fi
		/usr/vice/etc/afsd $TIMESYNC $OPTIONS >/dev/console 2>&1

		# Let AFS think for a while
		i=30
		while test ! -x $AFSMT/$MYCELL -a $i -gt 0 ; do
			sleep 1
			i=`expr $i - 1`
		done
		if test ! -x $AFSMT/$MYCELL ; then
			echo "Warning AFS initialization did not complete correctly"
		fi

		# now that /afs is mounted - start up alternate inetd
		AFSWS=$AFSMT/$MYCELL/@sys/usr/afsws
		if test -x $AFSWS/etc/inetd.afs -a -r $AFSWS/etc/inetd.conf ; then
			$ECHO "Starting AFS inetd"
			$AFSWS/etc/inetd.afs $AFSWS/etc/inetd.conf 2>/dev/null </dev/null&
		fi
	fi

    ;;

'stop')
	#
	# Stop the AFS inetd and server processes
	# Note that the afsd processes cannot be killed
	#
	if $IS_ON afsclient ; then
		killall inetd.afs
	fi

	if $IS_ON afsserver && test -x /usr/afs/bin/bos
	then
		$ECHO "Stopping AFS bosserver"
		/usr/afs/bin/bos shutdown localhost -localauth -wait
		killall -HUP bosserver
	fi
    ;;

*)
    echo "usage: $0 {start|stop}"
    ;;
esac
