#!/bin/bash
#
# openafs-server	Start/Stop the OpenAFS Server
#
# chkconfig: 2345 49 51
# description: OpenAFS is a distributed filesystem.
#
# $Revision$

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


start() {
	echo -n $"Starting openafs-server: "
	if [ -e /var/lock/subsys/openafs-server ] ; then
		echo -n $"cannot start openafs-server: already running"
		failure $"cannot start openafs-server: already running"
		echo
		return 1
	fi
	/usr/afs/bin/bosserver $BOSSERVER_ARGS
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/openafs-server
	return $RETVAL
}

stop() {
	echo -n $"Stopping openafs-server: "
	if [ ! -e /var/lock/subsys/openafs-server ] ; then
		echo -n $"cannot stop openafs-server: not running"
		failure $"cannot stop openafs-server: not running"
		echo
		return 1
	fi
	if [ "x$BOSSERVER_HOST" = "x" ] ; then
		if [ -f /usr/afs/local/bosserver.rxbind ] ; then
			BOSSERVER_HOST=`cat /usr/afs/local/bosserver.rxbind`
		else
			BOSSERVER_HOST="localhost"
		fi
	fi
	/usr/bin/bos shutdown $BOSSERVER_HOST -wait -localauth
	RETVAL=$?
	if [ $RETVAL -ne 0 ] ; then
		echo -n $"failed to shutdown OpenAFS"
		failure $"failed to shutdown OpenAFS"
		echo
		return $RETVAL
	fi
	killproc bosserver
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/openafs-server
	[ $RETVAL -eq 0 ] && rm -f /usr/afs/local/bosserver.rxbind
	return $RETVAL
}

rhstatus() {
	status bosserver
}

restart() {
	stop || exit
	start
}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        restart
        ;;
  status)
        rhstatus
        ;;
  condrestart)
	if [ "x$AFS_USE_BINARY_RESTART" = "xyes" ]; then
	    # this is what never shows up as
	    if [ `grep -c 'checkbintime 16 0 0 0 0' /usr/afs/local/BosConfig` = 1 ]; then
		[ -f /var/lock/subsys/openafs-server ] && restart || :
	    else
		echo $"deferring for newbinary restart"
	    fi
	else
	    [ -f /var/lock/subsys/openafs-server ] && restart || :
	fi
        ;;
  *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart}"
        exit 1
esac
