#!/bin/sh
###############################################################################
#                                                                             #
# IPFire.org - A linux based firewall                                         #
# Copyright (C) 2007-2022  IPFire Team  <info@ipfire.org>                     #
#                                                                             #
# This program is free software: you can redistribute it and/or modify        #
# it under the terms of the GNU General Public License as published by        #
# the Free Software Foundation, either version 3 of the License, or           #
# (at your option) any later version.                                         #
#                                                                             #
# This program is distributed in the hope that it will be useful,             #
# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
# GNU General Public License for more details.                                #
#                                                                             #
# You should have received a copy of the GNU General Public License           #
# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
#                                                                             #
###############################################################################

. /etc/sysconfig/rc
. $rc_functions
. /var/ipfire/nfs/nfs-server

case "$1" in
	start)
		boot_mesg "Mounting nfsd virtual filesystem..."
		/bin/mount -t nfsd none /proc/fs/nfsd 2>&1 > /dev/null
		evaluate_retval

		boot_mesg "Starting NFS mountd..."
		loadproc /usr/sbin/rpc.mountd

		boot_mesg "Starting NFS nfsd..."
		loadproc /usr/sbin/rpc.nfsd -p $PORT $PROCESSES

		boot_mesg "Starting NFS statd..."
		loadproc /usr/sbin/rpc.statd

		if [ "$QUOTAS" = "yes" ]; then
			boot_mesg "Starting NFS rquotad..."
			loadproc /usr/sbin/rpc.rquotad
		fi

		# Make ceratin that the list is refreshed on
		# a restart.
		boot_mesg "Exporting NFS Filesystems..."
		/usr/sbin/exportfs -ra 2>&1 > /dev/null
		evaluate_retval
		;;

	stop)
		boot_mesg "Stopping NFS statd..."
		killproc /usr/sbin/rpc.statd

		boot_mesg "Stopping NFS nfsd..."
		/usr/sbin/rpc.nfsd 0
		evaluate_retval

		boot_mesg "Stopping NFS mountd..."
		killproc /usr/sbin/rpc.mountd

		if [ "$QUOTAS" = "yes" ]; then
			boot_mesg "Stopping NFS rquotad..."
			killproc /usr/sbin/rpc.rquotad
		fi

		boot_mesg "Refreshing NFS Exported Filesystems..."
		/usr/sbin/exportfs -au 2>&1 > /dev/null
		evaluate_retval

		boot_mesg "Unmounting NFS Virtual Filesystem..."
		/bin/umount /proc/fs/nfsd 2>&1 > /dev/null
		evaluate_retval

		# Remove a pid file that isn't done automatically
		boot_mesg "Removing the rpc.statd pid file if it exists"
		if [ -f /var/run/rpc.statd.pid ]; then
		    rm -f /var/run/rpc.statd.pid
		fi
		;;

	reload)
		boot_mesg "Reloading NFS Server..."
		/usr/sbin/exportfs -ra
		evaluate_retval
		;;

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

	status)
		statusproc /usr/sbin/rpc.mountd
		## Special case for nfsd with no full path
		statusproc nfsd
		statusproc /usr/sbin/rpc.statd
		if [ "$QUOTA" = "yes" ]; then
			statusproc rpc.rquotad
		fi
		;;

	*)
		echo "Usage: $0 {start|stop|reload|restart|status}"
		exit 1
		;;
esac
