#!/bin/sh
#
# nfs		This shell script takes care of starting and stopping
#		the NFS services.
#
# chkconfig:	345 60 20
# description:	NFS is a popular protocol for file sharing across TCP/IP \
#		networks. This service provides NFS server functionality, \
#		which is configured via the /etc/exports file.
# probe:	true

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

# Get network config
. /etc/sysconfig/network

# Get service config
[ -f /etc/sysconfig/nfsd ] && . /etc/sysconfig/nfsd

# Check that networking is up.
if is_yes "${NETWORKING}"; then
	if [ ! -f /var/lock/subsys/network -a "$1" != stop -a "$1" != status ]; then
		msg_network_down NFSD
		exit 1
	fi
else
	exit 0
fi

if [ -x /sbin/pidof ] && [ "$1" != "stop" ]; then
	[ -z "`/sbin/pidof portmap`" ] && nls "Error: portmap isn't running" && exit 0
fi

# Sanity checks
[ -x /usr/sbin/rpc.nfsd ] || exit 0
[ -x /usr/sbin/rpc.mountd ] || exit 0
[ -f /etc/exports ] || exit 0

RETVAL=0
# See how we were called.
case "$1" in
  start)
	# Check if the service is already running?
	if [ ! -f /var/lock/subsys/nfs ]; then
		# Start daemons.
		if [ "$NFSDTYPE" = "U" ]; then
			msg_starting "NFS mountd"
			daemon rpc.mountd
			msg_starting "NFS daemon"
			daemon rpc.nfsd
		else
			msg_starting "NFS"
			daemon /usr/sbin/exportfs -r
			msg_starting "NFS mountd"
			daemon rpc.mountd $RPCMOUNTDOPTS
			msg_starting "NFS daemon"
			daemon rpc.nfsd $RPCNFSDCOUNT
		fi
		touch /var/lock/subsys/nfs
	else
		msg_already_running "NFS"
	fi
	;;
  stop)
	if [ -f /var/lock/subsys/nfs ]; then
		# Stop daemons.
		if [ "$NFSDTYPE" = "U" ]; then
			msg_stopping "NFS mountd"
			killproc rpc.mountd
			msg_stopping "NFS daemon"
			killproc rpc.nfsd
		else
			msg_stopping "NFS mountd"
			killproc rpc.mountd
			msg_stopping "NFS daemon"
			killproc nfsd -QUIT
			msg_stopping "NFS"
			daemon /usr/sbin/exportfs -au
		fi
		rm -f /var/lock/subsys/nfs
	else
		msg_not_running "NFS"
	fi
	;;
  status)
	status rpc.mountd
	RETVAL=$?
	if [ "$NFSDTYPE" = "U" ]; then
		status rpc.nfsd
	else
		status nfsd
	fi
	RET=$?
	[ $RETVAL -eq 0 ] && RETVAL=$RET
	;;
  restart)
	$0 stop
	$0 start
	exit $?
	;;
  force-reload)
	if [ "$NFSDTYPE" = "U" ]; then
		$0 restart
	else
		$0 reload
	fi
	exit $?
	;;
  reload)
	[ "$NFSDTYPE" = "U" ] && exit 0
	if [ -f /var/lock/subsys/nfs ]; then
		msg_reloading "NFS"
		busy
		/usr/sbin/exportfs
		[ $? -ne 0 ] && RETVAL=7
		[ $RETVAL -eq 0 ] && ok || died	
	else
		msg_not_running "NFS" >&2
		exit 7
	fi
	;;
  probe)
	[ "$NFSDTYPE" = "U" ] && exit 0
	if [ ! -f /var/lock/subsys/nfs ]; then
		echo start
		exit 0
	fi
	/sbin/pidof rpc.mountd >/dev/null 2>&1; MOUNTD="$?"
	/sbin/pidof nfsd >/dev/null 2>&1; NFSD="$?"
	if [ $MOUNTD = 1 -o $NFSD = 1 ]; then
		echo restart
		exit 0
	fi
	if [ /etc/exports -nt /var/lock/subsys/nfs ]; then
		echo reload
		exit 0
	fi
	;;
  *)
	msg_usage "$0 {start|stop|restart|reload|force-reload|probe|status}"
	exit 3
esac

exit $RETVAL
