#!/bin/sh
#
# chkconfig: 35 70 40
# description: Starts and stops the ARLA AFS client.
# description: Starts and stops the Andrew File System (AFS) client.

PATH=/bin:/usr/bin:/sbin:/usr/sbin; export PATH

# Source function library.
. /etc/rc.d/init.d/functions

# Load Configuration file to see what we should load
if [ -f /etc/athena/rc.conf ]; then
    . /etc/athena/rc.conf
else
    AFSCLIENT=arla
    AFSADJUST=false
    PUBLIC=false
fi

case "$1" in
    stop)
       sync
       echo -n "Unmounting /afs:"
       umount /afs
       if [ $? != 0 ]; then
            echo_failure
	    echo
	    exit 1
       fi
       echo_success
       echo

       echo -n "Stopping arlad:"
       killproc arlad
       echo

       # it seems if we remove the library before the last afsd is gone
       # there is a kernel problem ...
       typeset -i counter=0
       echo -n "Waiting for arlad to stop: "
       while [ "`ps aux | grep arlad | grep -v grep`" != "" ]; do
	    sleep 1
	    counter=$counter+1
	    if [ $counter = 30 ]; then
		echo -n "still running after 30 seconds..."
		echo_failure
		echo
		exit 1
	    fi
       done
       echo_success
       echo

       echo -n "Removing xfs module from kernel:"
       /sbin/rmmod xfs
       if [ $? = 0 ]; then
	    echo_success
	    echo
	    rm -f /var/lock/subsys/arla
	    exit 0
       else
            echo_failure
	    echo
	    rm -f /var/lock/subsys/arla
	    exit 1
       fi
       ;;

    start)
        if [ -f /var/lock/subsys/arla ]; then
	    exit 0
	fi

        echo -n "Installing xfs kernel module for Arla:"
        /sbin/insmod /usr/athena/bin/xfs.o
        if [ $? != 0 ]; then
	   AFSCLIENT=false
	   AFSADJUST=false
	   echo_failure
	   echo
        else
	   echo_success
	   echo
        fi
        cd /

	echo -n "Starting arlad:"
	daemon /usr/athena/bin/arlad
	echo

	echo -n "Mounting afs with Arla: ";
	mount -t xfs arla /afs
	if [ $? != 0 ]; then
	    echo_failure
	    echo
	    exit 1
	else
	    echo_success
	    echo
	fi

	/etc/athena/config_arla
	exitval=$?;

	touch /var/lock/subsys/arla
	
	exit $exitval
	;;

    *)
    echo "Usage: $0 {start|stop}"
    exit 1
    ;;
esac

exit 0
