#!/bin/sh
### BEGIN INIT INFO
# Provides:          nfs-kernel-server
# Required-Start:    $remote_fs nfs-common $portmap hwclock
# Required-Stop:     $remote_fs nfs-common $portmap hwclock
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Kernel NFS server support
# 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.
### END INIT INFO
#
# Startup script for nfs-utils
#
# Source function library.
. /etc/init.d/functions
#
# The environment variable NFS_SERVERS may be set in /etc/default/nfsd
# Other control variables may be overridden here too
test -r /etc/default/nfsd && . /etc/default/nfsd
#
# Location of executables:
test -x "$NFS_MOUNTD" || NFS_MOUNTD=/usr/sbin/rpc.mountd
test -x "$NFS_NFSD" || NFS_NFSD=/usr/sbin/rpc.nfsd
#
# The user mode program must also exist (it just starts the kernel
# threads using the kernel module code).
test -x "$NFS_MOUNTD" || exit 0
test -x "$NFS_NFSD" || exit 0
#
# Default is 8 threads, value is settable between 1 and the truely
# ridiculous 99
test "$NFS_SERVERS" != "" && test "$NFS_SERVERS" -gt 0 && test "$NFS_SERVERS" -lt 100 || NFS_SERVERS=8
#
#----------------------------------------------------------------------
# Startup and shutdown functions.
#  Actual startup/shutdown is at the end of this file.
#mountd
start_mountd(){
	echo -n 'starting mountd: '
	start-stop-daemon --start --exec "$NFS_MOUNTD" -- "$@"
	echo done
}
stop_mountd(){
	echo -n 'stopping mountd: '
	start-stop-daemon --stop --quiet --exec "$NFS_MOUNTD"
	echo done
}
#
#nfsd
start_nfsd(){
        modprobe -q nfsd
	grep -q nfsd /proc/filesystems || {
		echo NFS daemon support not enabled in kernel
		exit 1
        }
	grep -q nfsd /proc/mounts || mount -t nfsd nfsd /proc/fs/nfsd
	grep -q nfsd /proc/mounts || {
		echo nfsd filesystem could not be mounted at /proc/fs/nfsd
		exit 1
        }

	echo -n "starting $1 nfsd kernel threads: "
	start-stop-daemon --start --exec "$NFS_NFSD" -- "$@"
	echo done
}
stop_nfsd(){
	echo -n 'stopping nfsd: '
	$NFS_NFSD 0
	if pidof nfsd
	then
		echo failed
	else
		echo done
	fi
}

#----------------------------------------------------------------------
#
# supported options:
#  start
#  stop
#  reload: reloads the exports file
#  restart: stops and starts mountd
#FIXME: need to create the /var/lib/nfs/... directories
case "$1" in
  start)
	test -r /etc/exports && exportfs -r
	start_nfsd "$NFS_SERVERS"
	start_mountd
	test -r /etc/exports && exportfs -a;;
  stop)	exportfs -ua
	stop_mountd
	stop_nfsd;;
  status)
	status /usr/sbin/rpc.mountd
	RETVAL=$?
	status nfsd
	rval=$?
	[ $RETVAL -eq 0 ] && exit $rval
	exit $RETVAL;;
  reload)	test -r /etc/exports && exportfs -r;;
  restart)
	$0 stop
	$0 start;;
  *)	echo "Usage: $0 {start|stop|status|reload|restart}"
	exit 1;;
esac
