#!/bin/sh
#
# nfs           This shell script takes care of starting and stopping
#               the NFS services.
#
# chkconfig: 234 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/init.d/functions

# Source networking configuration.
# Check that networking is up.
SourceIfNotEmpty /etc/sysconfig/network && [ "$NETWORKING" != no ] || exit

[ -x /usr/sbin/rpc.nfsd -a -x /usr/sbin/rpc.mountd -a -x /usr/sbin/exportfs -a -s /etc/exports ] || exit

# Number of servers to be started up by default
RPCNFSDCOUNT=8

# NFSv3 only if kernel >= 2.2.18
OS_RELEASE=`uname --release`
OS_RELEASE_MINOR=`echo "$OS_RELEASE" | sed 's/\(^[0-9]\)\.\([0-9]*\).*/\2/'`
OS_RELEASE_VERSION=`echo "$OS_RELEASE" | sed 's/\(^[0-9]\)\.\([0-9]*\)\.\([0-9]*\).*/\3/'`
if [ "$OS_RELEASE_MINOR" -gt 2 ]; then
	RPCMOUNTDOPTS=
elif [ "$OS_RELEASE_MINOR" -eq 2 -a "$OS_RELEASE_VERSION" -ge 18 ]; then
	RPCMOUNTDOPTS=
else
	RPCMOUNTDOPTS="--no-nfs-version 3"
fi

LOCKFILE=/var/lock/subsys/nfs
RETVAL=0

start()
{
	# Start daemons.
	action "Starting NFS services:" /usr/sbin/exportfs -r

	echo -n "Starting NFS quotas: "
	daemon rpc.rquotad
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] || return

	echo -n "Starting NFS daemon: "
	daemon rpc.nfsd $RPCNFSDCOUNT
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] || return

	# Let's see if we support NFS version 3.
	/usr/sbin/rpcinfo -u localhost nfs 3 &>/dev/null
	if [ $? -ne 0 ]; then
		RPCMOUNTDOPTS="--no-nfs-version 3"
	fi

	echo -n "Starting NFS mountd: "
	daemon rpc.mountd $RPCMOUNTDOPTS
	RETVAL=$?
	echo
}

stop()
{
	# Stop daemons.
	echo -n "Shutting down NFS mountd: "
	killproc rpc.mountd
	echo
	echo -n "Shutting down NFS daemon: "
	killproc nfsd
	echo
	echo -n "Shutting down NFS quotas: "
	killproc rpc.rquotad
	echo
	# Do it the last so that clients can still access the server
	# when the server is running.
	action "Shutting down NFS services:" /usr/sbin/exportfs -au
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && rm -f "$LOCKFILE"
}

restart()
{
	stop
	start
}

reload()
{
	/usr/sbin/exportfs -r
	RETVAL=$?
}

probe()
{
	if [ ! -e "$LOCKFILE" ]; then
		echo start
		exit 0
	fi
	/sbin/pidof rpc.mountd &>/dev/null; MOUNTD="$?"
	/sbin/pidof nfsd &>/dev/null; NFSD="$?"
	if [ $MOUNTD = 1 -o $NFSD = 1 ]; then
		echo restart
		exit 0
	fi
	if [ /etc/exports -nt "$LOCKFILE" ]; then
		echo reload
		exit 0
	fi
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	restart)
		restart
		;;
	reload)
		reload
		;;
	condstop)
		if [ -e "$LOCKFILE" ]; then
			stop
		fi
		;;
	condrestart)
		if [ -e "$LOCKFILE" ]; then
			restart
		fi
		;;
	status)
		status rpc.mountd
		status nfsd
		status rpc.rquotad
		RETVAL=$?
		;;
	probe)
		probe
		;;
	*)
		echo "Usage: ${0##*/} {start|stop|reload|restart|condstop|condrestart|status}"
		RETVAL=1
esac

exit $RETVAL
