#!/bin/bash
#
# openafs-client	Start/Stop the OpenAFS Client
#
# chkconfig: 2345 50 50
# description: OpenAFS is a distributed filesystem.
# Should-Start: dkms
#
# $Revision$

[ -f /etc/init.d/functions ] && . /etc/init.d/functions
[ -f /etc/sysconfig/openafs ] && . /etc/sysconfig/openafs


start() {
	if [ -h /usr/vice/etc/CellServDB ]; then
		rm -f /usr/vice/etc/CellServDB
	fi
	echo -n $"Updating CellServDB: "
	cat /usr/vice/etc/CellServDB.local /usr/vice/etc/CellServDB.dist > \
	/usr/vice/etc/CellServDB
	chmod 644 /usr/vice/etc/CellServDB
	echo
	echo -n $"Starting openafs-client: "
	if [ -e /var/lock/subsys/openafs-client ] ; then
		echo -n $"cannot start openafs-client: already running"
		failure $"cannot start openafs-client: already running"
		echo
		return 1
	fi
	modprobe openafs
	RETVAL=$?
	if [ $RETVAL -ne 0 ] ; then
		echo -n $"failed to load openafs kernel module."
		failure $"failed to load openafs kernel module."
		echo
		return $RETVAL
	fi
	/usr/vice/etc/afsd $AFSD_ARGS
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/openafs-client
	[ $RETVAL -eq 0 ] && [ "$AFS_POST_INIT" ] && $AFS_POST_INIT
	return $RETVAL
}

stop() {
	echo -n $"Stopping openafs-client: "
	if [ ! -e /var/lock/subsys/openafs-client ] ; then
		echo -n $"cannot stop openafs-client: not running"
		failure $"cannot stop openafs-client: not running"
		echo
		return 1
	fi
	umount /afs
	RETVAL=$?
	echo
	if [ $RETVAL -eq 0 ] ; then
		rmmod openafs
		RETVAL=$?
	fi
	if [ $RETVAL -eq 0 ] ; then
		rm -f /var/lock/subsys/openafs-client
	fi
	return $RETVAL
}

rhstatus() {
	status afsd
	return $?
}

restart() {
	stop || exit
	start
}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        restart
        ;;
  status)
        rhstatus
	exit $?
        ;;
  condrestart)
        [ -f /var/lock/subsys/openafs-client ] && restart || :
        ;;
  *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart}"
        exit 1
esac
