#!/bin/sh
#
# afs.rc: rc script for AFS on Solaris 2.x platforms
#
# Install this script as /etc/init.d/afs.rc
# then make links like this:
# ln -s ../init.d/afs.rc /etc/rc0.d/K66afs
# ln -s ../init.d/afs.rc /etc/rc3.d/S99afs 
#
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

# Need the commands ps, awk, kill, sleep
PATH=${PATH}${PATH:+:}/sbin:/bin:/usr/bin

killproc() {            # kill the named process(es)
	awkfield2='$2'
        pid=`ps -ef | awk "/$1/ && ! /awk/ {print $awkfield2}"`
        [ "$pid" != "" ] && kill -KILL $pid
}

case $1 in
'start')

#
# Athena additions
#
. /etc/athena/rc.conf
if [ "$AFSCLIENT" = false -a "$AFSSRV" = false ]; then
	exit 0
fi
AFSCLIENTNUM=4
if [ "$AFSCLIENT" != true ]; then
	AFSCLIENTNUM="$AFSCLIENT"
else
	AFSCLIENTNUM=4
fi
OPTIONS="-stat 2000 -dcache 800 -daemons $AFSCLIENTNUM -volumes 70"
if [ \( "$AFSADJUST" = true -o $PUBLIC = true \) -a -d /usr/vice/cache/ ]; then
	echo "Adjusting the cache size" > /dev/console
	rm -f /usr/vice/etc/cacheinfo.old
	mv -f /usr/vice/etc/cacheinfo /usr/vice/etc/cacheinfo.old
	df -bk /usr/vice/cache/ | awk '
		(NR == 2) {
			printf("/afs:/usr/vice/cache:%d\n", int($2 * 3 / 4));
		}' > /usr/vice/etc/cacheinfo
	sed -n '1s/^.*:\([^:]*\)$/New AFS cache size = \1K/p' \
		/usr/vice/etc/cacheinfo	> /dev/console
fi

#
# Load kernel extensions
#

# nfssrv has to be loaded first
if [ -f /kernel/misc/nfssrv ]; then
	echo "Loading NFS server kernel extensions"
	modload /kernel/misc/nfssrv
else
	echo "/kernel/misc/nfssrv does not exist. Skipping AFS startup."
	exit 1
fi

if [ -f /kernel/fs/afs ]; then
	echo "Loading AFS kernel extensions"
	modload /kernel/fs/afs
else
	echo "/kernel/fs/afs does not exist. Skipping AFS startup."
	exit 1
fi

#
# Start the AFS server processes if a bosserver exists
#

if [ -x /usr/afs/bin/bosserver -a "$AFSSRV" = true ]; then
	echo "Starting AFS Server processes"
	/usr/afs/bin/bosserver &
	OPTIONS="$OPTIONS -nosettime"
	sleep 30
fi

if [ "$AFSCLIENT" = false ]; then
	exit 0
fi

#
# Check that all of the client configuration files exist
#

for file in /etc/afsd /usr/vice/etc/cacheinfo \
	    /usr/vice/etc/ThisCell /usr/vice/etc/CellServDB
do
	if [ ! -f ${file} ]; then
		echo "${file} does not exist. Not starting AFS client."
		exit 1
	fi
done

#
# Check that the root directory for AFS (/afs) 
# and the cache directory (/usr/vice/cache) both exist
#

for dir in `awk -F: '{print $1, $2}' /usr/vice/etc/cacheinfo`
do
	if [ ! -d ${dir} ]; then
		echo "${dir} does not exist. Not starting AFS client."
		exit 2
	fi
done

echo "Starting afsd"
/etc/afsd $OPTIONS

#
# Start AFS inetd services
# (See the AFS Command Ref. for notes on the proper configuration of inetd.afs)
#
if [ -f /usr/sbin/inetd.afs -a -f /etc/inetd.conf.afs ]; then
	/usr/sbin/inetd.afs /etc/inetd.conf.afs
fi

echo ;;

'stop')

#
# Stop the AFS inetd and server processes
# Note that the afsd processes cannot be killed
#

echo "Killing inetd.afs"
killproc inetd.afs

bosrunning=`ps -ef | awk '/bosserver/ && ! /awk/'`
if [ "${bosrunning}" != "" ]; then
	echo "Shutting down AFS server processes"
	/usr/afs/bin/bos shutdown localhost -localauth -wait
	echo "Killing AFS bosserver"
	killproc bosserver
fi

echo ;;

*) 	echo "Invalid option supplied to $0"
	exit 1;;
esac
