#!/bin/sh
#
# nfsfs		Mount NFS filesystems.
#
# chkconfig:	345 16 80
# description:	Mounts and unmounts all Network File System (NFS) \
#		mount points.

# Source networking configuration.
if [ ! -f /etc/sysconfig/network ]; then
	exit 0
fi

# Source function library.
. /etc/rc.d/init.d/functions

# Get network config
. /etc/sysconfig/network

# Get service config
[ -f /etc/sysconfig/nfsfs ] && . /etc/sysconfig/nfsfs
[ -f /etc/sysconfig/nfslock ] && . /etc/sysconfig/nfslock

# Check that networking is up.
if is_yes "${NETWORKING}"; then
	if [ ! -f /var/lock/subsys/network -a "$1" != stop -a "$1" != status ]; then
		msg_network_down nfsfs
		exit 1
	fi
else
	exit 0
fi

if [ "$1" != "stop" ]; then
	check_portmapper || { nls "Error: portmap isn't running" && exit 0; }
fi

if [ -z "$NFSUMOUNT_IGNORE" ]; then
	NFSUMOUNT_IGNORE="/ /usr"
fi

start() {
  	if [ ! -f /var/lock/subsys/nfsfs ]; then
		# Set the ports lockd should listen on
		if [ -n "$LOCKD_TCPPORT" ]; then
			/sbin/sysctl -w fs.nfs.nlm_tcpport=$LOCKD_TCPPORT >/dev/null 2>&1
		fi
		if [ -n "$LOCKD_UDPPORT" ]; then
			/sbin/sysctl -w fs.nfs.nlm_udpport=$LOCKD_UDPPORT >/dev/null 2>&1
		fi

		# Special case /usr first
		if ! awk '{ if ($2 == "/usr" && $3 ~ /^nfs(4$|$)/ && $4 !~ /noauto/) exit 1}' /etc/fstab ; then
			run_cmd "Mounting /usr filesystem" mount /usr
			# In case of /usr over NFS idmapd, gssd and lockd may not start
			# check if they're running and try to start them if not
			/sbin/service idmapd status >/dev/null || /sbin/service idmapd start
			/sbin/service gssd status >/dev/null || /sbin/service gssd start
			/sbin/service nfslock status >/dev/null || /sbin/service nfslock start
		fi
		run_cmd "Mounting NFS filesystems" mount -a -t nfs,nfs4
		touch /var/lock/subsys/nfsfs

	else
	    msg_already_running "NFSFS"
	fi
}

stop() {
	if [ -f /proc/mounts ]; then
		fsfile="/proc/mounts"
	else
		fsfile="/etc/mtab"
	fi

	show "Unmounting NFS filesystems"
	busy
	retry=3
	remaining=$(awk -v ig="^($NFSUMOUNT_IGNORE)$" 'BEGIN { gsub(/[\t ]+/, "|", ig); } \
			$3 ~ /^nfs(4$|$)/ && $2 !~ ig {print $2}' $fsfile)
	while [ -n "$remaining" -a $retry -gt 0 ]; do
		fuser -msk -TERM `awk -v ig="^($NFSUMOUNT_IGNORE)$" 'BEGIN { gsub(/[\t ]+/, "|", ig); } \
					$3 ~ /^nfs(4$|$)/ && $2 !~ ig {print $2}' $fsfile`
		sleep 2
		fuser -msk -KILL `awk -v ig="^($NFSUMOUNT_IGNORE)$" 'BEGIN { gsub(/[\t ]+/, "|", ig); } \
					$3 ~ /^nfs(4$|$)/ && $2 !~ ig {print $2}' $fsfile`
		# Sort filesystems to unmount in reverse order
		rem=""
		for r in $(awk -v ig="^($NFSUMOUNT_IGNORE)$" 'BEGIN { gsub(/[\t ]+/, "|", ig); } \
				$3 ~ /^nfs(4$|$)/ && $2 !~ ig {print $2}' $fsfile) ; do
			rem="$r $rem"
		done
		for fs in $rem ; do
			umount $fs
		done
		remaining=$(awk -v ig="^($NFSUMOUNT_IGNORE)$" 'BEGIN { gsub(/[\t ]+/, "|", ig); } \
				$3 ~ /^nfs(4$|$)/ && $2 !~ ig {print $2}' $fsfile)
		retry=$(($retry-1))
	done
	ok
	rm -f /var/lock/subsys/nfsfs
}

# See how we were called.
case "$1" in
  start)
  	start
	;;
  stop)
  	stop
	;;
  status)
	if [ -f /proc/mounts ]; then
		echo "Configured NFS mountpoints:"
		grep -v '^#' /etc/fstab | \
		  awk '{ if ($3 ~ /^nfs(4$|$)/ && $4 !~ /noauto/) print $2}'
		echo "Active NFS mountpoints:"
		grep -v '^#' /proc/mounts | \
		  awk '{ if ($3 ~ /^nfs(4$|$)/ && $4 !~ /noauto/) print $2}'
	else
		echo "/proc filesystem unavailable"
	fi
	;;
  restart)
	stop
	start
	;;
  reload|force-reload)
	mount -a -t nfs,nfs4
	;;
  *)
	msg_usage "$0 {start|stop|restart|reload|force-reload|status}"
	exit 3
esac

exit 0
