#!/bin/sh
#
# nfs-kernel-server
#		This shell script takes care of starting and stopping
#               the kernel-mode NFS server.
#
# chkconfig: 2345 20 80
PATH=/bin:/usr/bin:/sbin:/usr/sbin:/var/swap/bin:/mnt/bin
# 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.
if [ `which rpc.nfsd | wc -l` -eq 0 ]; then
	echo "ERROR: rpc.nfsd not found" 
	exit 0
fi
if [ `which rpc.mountd | wc -l` -eq 0 ]; then
	echo "ERROR: rpc.mountd not found" 
	exit 0
fi
if [ `which exportfs | wc -l` -eq 0 ]; then
	echo "ERROR: exportfs not found" 
	exit 0
fi

# Read config
DEFAULTFILE=/etc/default/nfs-kernel-server
RPCNFSDCOUNT=8
RPCMOUNTDOPTS=
if [ -f $DEFAULTFILE ]; then
    . $DEFAULTFILE
fi

# The default state directory is /var/lib/nfs
test -n "$NFS_STATEDIR" || NFS_STATEDIR=/var/lib/nfs
#

#directories
create_directories(){
	echo -n '[nfs-kernel-server] creating NFS state directory: '
	mkdir -p "$NFS_STATEDIR"
	(	cd "$NFS_STATEDIR"
		#umask 077
		umask 000
		mkdir -p sm sm.bak
		test -w sm/state || {
			rm -f sm/state
			:>sm/state
		}
		#umask 022
		umask 000
		for file in xtab etab smtab rmtab state
		do
			test -w "$file" || {
				rm -f "$file"
				:>"$file"
			}
		done
	)
	echo done
}


# See how we were called.
case "$1" in
  start)
  # The nfsd kernel module must exist along with its dependencies
  if [ `lsmod | grep "nfsd" | wc -l` -lt 1 ]; then
    if [ -e /lib/modules/exportfs.ko ]; then
		 	insmod /lib/modules/exportfs.ko 
    	insmod /lib/modules/nfsd.ko1
   	else
   		echo "Module wurden nicht geladen, sind aber eventuell im Kernel schon enthalten"
   	fi
  fi
  create_directories
	cd /	# daemons should have root dir as cwd
	if grep -q '^/' /etc/exports
	then
		printf "[nfs-kernel-server] Exporting directories for $DESC..."
		exportfs -r
		echo "done."

		printf "[nfs-kernel-server] Starting $DESC:"
		printf " nfsd"
		start-stop-daemon --start --quiet \
		    --exec 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
		    grep -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

		rpcinfo -u localhost nfs 3 >/dev/null 2>&1 ||
		    RPCMOUNTDOPTS="$RPCMOUNTDOPTS"

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

		start-stop-daemon --start --quiet \
		    --exec rpc.mountd -- $RPCMOUNTDOPTS
		echo "."
	else
		echo "[nfs-kernel-server] Not starting $DESC: No exports."
	fi
	;;

  stop)
	printf "[nfs-kernel-server] Stopping $DESC: mountd"
	start-stop-daemon --stop --oknodo --quiet \
	    --name rpc.mountd --user 0
	printf " nfsd"
	start-stop-daemon --stop --oknodo --quiet \
	    --name nfsd --user 0 --signal 2
	echo "."

	printf "[nfs-kernel-server] Unexporting directories for $DESC..."
	exportfs -au
	echo "done."
	;;

  reload | force-reload)
	printf "[nfs-kernel-server] Re-exporting directories for $DESC..."
	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
