#!/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 "NFS daemon"
		exit 1
	fi
else
	exit 0
fi

start() {
	# Check if the service is already running?
	if [ ! -f /var/lock/subsys/nfs ]; then
		if [ -x /sbin/pidof ]; then
			if [ -z "`/sbin/pidof portmap`" ]; then
			   	nls "Error: portmap isn't running"
			   	exit 0
			fi
		fi

		if ! is_yes "$NFS4" ; then
			RPCMOUNTOPTIONS="$RPCMOUNTOPTIONS --no-nfs-version 4"
		fi

		# Start daemons.
		modprobe -s nfsd > /dev/null 2>&1
		if [ "$(kernelverser)" -ge "002006" ]; then
		    grep -q nfsd /proc/filesystems && \
		    ! grep -q nfsd /proc/mounts && \
			run_cmd "Mounting /proc/fs/nfsd filesystem" mount -t nfsd nfsd /proc/fs/nfsd
		fi
		msg_starting "NFS exportfs"
		daemon /usr/sbin/exportfs -r
		msg_starting "NFS mountd"
		daemon rpc.mountd $RPCMOUNTOPTIONS
		if is_yes "$NFS4" ; then
		    if (grep -q rpc_pipefs /proc/filesystems); then
			! grep -q rpc_pipefs /proc/mounts && \
			    run_cmd "Mounting /var/lib/nfs/rpc_pipefs filesystem" mount -t rpc_pipefs rpc_pipefs /var/lib/nfs/rpc_pipefs
			if [ ! -f /var/lock/subsys/idmapd ]; then
			    msg_starting "NFS idmapd"
			    daemon rpc.idmapd $RPCIDMAPOPTIONS
			    [ $? = 0 ] && touch /var/lock/subsys/idmapd
			fi
			msg_starting "NFS svcgssd"
			daemon rpc.svcgssd $RPCSVCGSSOPTIONS
		    fi
		fi
		msg_starting "NFS daemon"
		daemon rpc.nfsd $RPCNFSDCOUNT
		touch /var/lock/subsys/nfs
	else
		msg_already_running "NFS daemon"
	fi
}

stop() {
	if [ -f /var/lock/subsys/nfs ]; then
		# Stop daemons.
		msg_stopping "NFS mountd"
		killproc rpc.mountd
		msg_stopping "NFS daemon"
		killproc nfsd -QUIT
		if is_yes "$NFS4" ; then
			if (grep -q rpc_pipefs /proc/filesystems); then
			    msg_stopping "NFS svcgssd"
			    killproc rpc.svcgssd
			    if [ -f /var/lock/subsys/idmapd -a ! -f /var/lock/subsys/nfsfs ]; then
				msg_stopping "NFS idmapd"
				killproc rpc.idmapd
				rm -f /var/lock/subsys/idmapd
			    fi
			fi
		fi
		msg_stopping "NFS"
		daemon /usr/sbin/exportfs -au
		rm -f /var/lock/subsys/nfs
	else
		msg_not_running "NFS"
	fi
}

RETVAL=0
# See how we were called.
case "$1" in
  start)
  	start
	;;
  stop)
  	stop
	;;
  restart)
	stop
	start
	;;
  reload|force-reload)
	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"
		exit 7
	fi
	;;
  probe)
	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
	;;
  status)
	status rpc.mountd
	RETVAL=$?
	status nfsd
	RET=$?
	[ $RETVAL -eq 0 ] && RETVAL=$RET
	;;
  *)
	msg_usage "$0 {start|stop|restart|reload|force-reload|probe|status}"
	exit 3
esac

exit $RETVAL
