#!/bin/sh
#
# This should get called from /etc/rc or /etc/rc.local BEFORE any
# AFS stuff is used!
#
PATH=/bin:/usr/bin; export PATH

AFSLOAD=true
AFSSRV=false
AFSCLIENT=true
AFSADJUST=false

#
# Stop AFS and unload the module if called with "stop"
#
if [ "$1" = "stop" ]; then
	sync
	pidfile=/var/run/afsd.pid
	if [ ! -f $pidfile ]; then
		echo "AFS doesn't seem to run: no pidfile $pidfile"
		exit 1
	fi
	kill `cat $pidfile`
	rc=$?
	if [ $rc != 0 ]; then
		echo "shuting AFS down failed: $rc"
		exit $rc
	fi
	# it seems if we remove the library before the last afsd is gone
	# there is a kernel problem ...
	typeset -i counter=0
	while [ "`ps aux | grep afsd | grep -v grep`" != "" ]; do
	 	echo "waiting for AFS daemon to stop ..."
	 	sleep 1
		counter=$counter+1
		if [ $counter = 30 ]; then
			echo "afsd still running after 30 seconds .."
			exit 1;
		fi
	done
	/sbin/rmmod libafs.o
	exit $?
fi

# Load AFS into the kernel if we need to

if [ "${AFSLOAD}" = "true" ]; then

	if [ -r /usr/vice/etc/dkload/rc.dkload.client ]; then
		LOADPROG=/usr/vice/etc/dkload/rc.dkload.client
	elif [ -r /usr/afs/bin/dkload/rc.dkload.server ]; then
		LOADPROG=/usr/afs/bin/dkload/rc.dkload.server

        elif [ -r /usr/vice/etc/modload/rc.modload.client ]; then
		LOADPROG=/usr/vice/etc/modload/rc.modload.client
	elif [ -r /usr/afs/bin/modload/rc.modload.server ]; then
		LOADPROG=/usr/afs/bin/modload/rc.modload.server

	else
                echo "Cannot load AFS - no kernel library" > /dev/console
		AFSSRV=false
		AFSCLIENT=false
		AFSADJUST=false
		echo "Cancelled AFS service startups." >/dev/console
        fi
	if [ x$LOADPROG != x ]; then
		echo "Invoking AFS Kernel Loader" > /dev/console
		sh $LOADPROG
		if [ $? != 0 ]; then
			AFSSRV=false
			AFSCLIENT=false
			AFSADJUST=false
			echo "Error loading kernel, canceled AFS startup." > /dev/console
		fi
	fi
        cd /
fi

# Start the server before the client, in case this is a client of itself.
if [ "${AFSSRV}" = "true" ]; then
        echo ""                                                 >/dev/console
        echo -n "AFS server: "                                  >/dev/console
        if [ -f /usr/afs/bin/bosserver ]; then
                if [ -f /usr/afs/logs/BosLog ]; then
                        mv -f /usr/afs/logs/BosLog /usr/afs/logs/BosLog.old
                fi
                /usr/afs/bin/bosserver >> /usr/afs/logs/BosLog 2>&1 &
                echo "started."                                 >/dev/console
        else
                echo "Can't start AFS server!"
        fi
fi

# Start up afsd in foreground, in case any daemons are coming off AFS
if [ "${AFSCLIENT}" != "false" ]; then
        echo ""                                                 >/dev/console
        echo -n "Starting AFS client: "                         >/dev/console

        if [ \( "${AFSADJUST}" = "true" -o "${PUBLIC}" = "true" \) \
             -a -d /usr/vice/cache/ ]; then

                echo -n "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 /usr/vice/cache/ ;\
		 du -s /usr/vice/cache/) | \
                 awk ' \
			(NR == 1) { y=0 } \
			(NR == 2) { free = $4 + ($3 * y); } \
			(NR == 3) { x = int((free+$1*(1-y)-3000)*3/4); \
                                if (x < 2000) { x = 2000 } ; \
                                if (x > 40000) { x = 40000 } ; \
                                printf("/afs:/usr/vice/cache:%d\n",x);}' \
                  > /usr/vice/etc/cacheinfo

                sed -n -e '1s/^.*:\([^:]*\)$/New AFS cache size = \1K/p' \
                        /usr/vice/etc/cacheinfo                 >/dev/console
        fi
        if [ "${AFSCLIENT}" = "true" ]; then
                AFSCLIENT=4
        fi
        /usr/vice/etc/afsd -daemons "${AFSCLIENT}"

	# Configure AFS (copies CellServDB and configures cells)
	/usr/vice/etc/config_afs

	# Check for new version of Linux-AFS
	files=`ls -t /usr/vice/etc/dkload/libafs.o /afs/athena.mit.edu/system/config/linux-afs/current`
	newer=`echo $files | awk '{print $1}'`
	if [ $newer = /afs/athena.mit.edu/system/config/linux-afs/current ]; then
		echo ""
		echo "There is a new Linux-AFS release available.  You should"
		echo "try to obtain it!"
		echo ""
		cat /afs/athena.mit.edu/system/config/linux-afs/current
	fi
fi

