#!/bin/sh
#
# nfs-kernel-server
#		This shell script takes care of starting and stopping
#               the kernel-mode NFS server.
#
# 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.
#

set -e

# What is this?
DESC="NFS kernel daemon"
PREFIX=/usr

# Exit if required binaries are missing.
[ -x $PREFIX/sbin/rpc.nfsd    ] || exit 0
[ -x $PREFIX/sbin/rpc.mountd  ] || exit 0
[ -x $PREFIX/sbin/exportfs    ] || exit 0
[ -x $PREFIX/sbin/rpc.svcgssd ] || exit 0

# Read config
DEFAULTFILE=/etc/default/nfs-kernel-server
RPCNFSDCOUNT=8
RPCMOUNTDOPTS=
NEED_SVCGSSD=yes
RPCGSSDOPTS=
RPCSVCGSSDOPTS=
PROCNFSD_MOUNTPOINT=/proc/fs/nfsd
if [ -f $DEFAULTFILE ]; then
    . $DEFAULTFILE
fi

do_modprobe() {
    modprobe -q "$1" || true
}

do_mount() {
    if ! grep -E -qs "$1\$" /proc/filesystems
    then
	return 1
    fi
    if ! mountpoint -q "$2"
    then
	mount -t "$1" "$1" "$2"
	return
    fi
    return 0
}

# See how we were called.
case "$1" in
  start)
	cd /	# daemons should have root dir as cwd
	# FIXME if grep -q '^/' /etc/exports
	if [ -f /etc/exports ]
	then
		do_modprobe nfsd
		do_mount nfsd $PROCNFSD_MOUNTPOINT || NEED_SVCGSSD=no
		printf "Exporting directories for $DESC..."
		$PREFIX/sbin/exportfs -r
		echo "done."

		printf "Starting $DESC:"
		if [ "$NEED_SVCGSSD" = yes ]
		then
		    printf " svcgssd"
		    start-stop-daemon --start --quiet \
			    --make-pidfile --pidfile /var/run/rpc.svcgssd.pid \
			    --exec $PREFIX/sbin/rpc.svcgssd -- $RPCSVCGSSDOPTS
		fi

		printf " nfsd"
		start-stop-daemon --start --quiet \
		    --exec $PREFIX/sbin/rpc.nfsd -- $RPCNFSDCOUNT

		printf " mountd"

		# make sure 127.0.0.1 is a valid source for requests
		ClearAddr=
		if [ -f /proc/net/rpc/auth.unix.ip/channel ]
		then
		    fgrep -qs 127.0.0.1 /proc/net/rpc/auth.unix.ip/content || {
			echo "nfsd 127.0.0.1 2147483647 localhost" >/proc/net/rpc/auth.unix.ip/channel
			ClearAddr=yes
		    }
		fi

		if [ -x $PREFIX/bin/rpcinfo ] ; then
			RPCINFO=$PREFIX/bin/rpcinfo
		elif [ -x $PREFIX/sbin/rpcinfo ] ; then
			RPCINFO=$PREFIX/sbin/rpcinfo
		else
			echo "Error: could not locate rpcinfo binary." >&2
			exit 1
		fi
		$RPCINFO -u localhost nfs 3 >/dev/null 2>&1 ||
		    RPCMOUNTDOPTS="$RPCMOUNTDOPTS --no-nfs-version 3"

		[ -z "$ClearAddr" ] || echo "nfsd 127.0.0.1 1" >/proc/net/rpc/auth.unix.ip/channel

		start-stop-daemon --start --quiet \
		    --exec $PREFIX/sbin/rpc.mountd -- $RPCMOUNTDOPTS
		echo "."
	else
		echo "Not starting $DESC: No exports."
	fi
	;;

  stop)
	printf "Stopping $DESC: mountd"
	start-stop-daemon --stop --oknodo --quiet \
	    --name rpc.mountd --user 0
	if [ "$NEED_SVCGSSD" = yes ]
	then
	    printf " svcgssd"
	    start-stop-daemon --stop --oknodo --quiet \
		    --name rpc.svcgssd --user 0
	    rm -f /var/run/rpc.svcgssd.pid
	fi
	printf " nfsd"
	start-stop-daemon --stop --oknodo --quiet \
	    --name nfsd --user 0 --signal 2
	echo "."

	printf "Unexporting directories for $DESC..."
	$PREFIX/sbin/exportfs -au
	echo "done."
	;;

  reload | force-reload)
	printf "Re-exporting directories for $DESC..."
	$PREFIX/sbin/exportfs -r
	echo "done."
	;;

  restart)
	$0 stop
	sleep 1
	$0 start
	;;

  *)
	echo "Usage: nfs-kernel-server {start|stop|reload|force-reload|restart}"
	exit 1
	;;
esac

exit 0
