#! /bin/sh
# Description:       Now that TCP/IP is configured, mount the NFS file
#                    systems in /etc/fstab if needed. If possible,
#                    start the portmapper before mounting (this is needed for
#                    Linux 2.1.x and up).
#
#                    Also mounts SMB filesystems now, so the name of
#                    this script is getting increasingly inaccurate.

PATH=/sbin:/bin
. /lib/init/vars.sh

. /lib/lsb/init-functions
. /lib/init/mount-functions.sh

do_start() {
	[ -f /etc/fstab ] || return
	#
	# Read through fstab line by line. If it is NFS, set the flag
	# for mounting NFS file systems. If any NFS partition is found and it
	# not mounted with the nolock option, we start the portmapper.
	# 
	# If any sec={krb5,krb5i,krb5p} option is given, or any of the file
	# systems are nfs4, we'll need to start rpc.gssd and/or rpc.idmapd too;
	# we'll leave that to nfs-common.
	#

	exec 9<&0 </etc/fstab

	start_nfs=no
	while read DEV MTPT FSTYPE OPTS REST
	do
		case "$DEV" in
		  ""|\#*)
			continue
			;;
		esac
		case "$OPTS" in
		  noauto|*,noauto|noauto,*|*,noauto,*)
			continue
			;;
		esac
		case "$FSTYPE" in
		  nfs)
		  	# NFS filsystems normally require statd and portmap. However,
			# if nolock is set, portmap and statd are not required for this
			# file system.
			case "$OPTS" in
			  nolock|*,nolock|nolock,*|*,nolock,*)
			  	# no action
				;;
			  *)
				start_nfs=yes
				;;
			esac

			# However, Kerberos requires gssd, so start nfs-common anyway.
			case "$OPTS" in
			  sec=krb5|*,sec=krb5|sec=krb5,*|*,sec=krb5i,*|sec=krb5i|*,sec=krb5i|sec=krb5i,*|*,sec=krb5i,*|sec=krb5p|*,sec=krb5p|sec=krb5p,*|*,sec=krb5p,*)
			  	start_nfs=yes
				;;
			esac
			;;
		  nfs4)
			# NFSv4 requires idmapd, so start nfs-common no matter what the options are.
			start_nfs=yes
			;;
		  smbfs|cifs|coda|ncp|ncpfs|ocfs2|gfs)
			;;
		  *)
			FSTYPE=
			;;
		esac
		if [ "$FSTYPE" ]
		then
			case "$NETFS" in
			  $FSTYPE|*,$FSTYPE|$FSTYPE,*|*,$FSTYPE,*)
				;;
			  *)
				NETFS="$NETFS${NETFS:+,}$FSTYPE"
				;;
			esac
		fi
	done

	exec 0<&9 9<&-

	#
	# Initialize nfs-common (which starts rpc.statd, rpc.gssd
	# and/or rpc.idmapd, and loads the right kernel modules if
 	# applicable) if we use Kerberos and/or NFSv4 mounts.
	#
	if [ "$start_nfs" = yes ] && [ -x /etc/init.d/portmap ] && [ -x /etc/init.d/nfs-common ]
	then
		/etc/init.d/portmap start
		/etc/init.d/nfs-common start
	fi

	if [ "$NETFS" ]
	then
		mount_all_nfs() { mount -a -t$NETFS ; }
		
		# Accept 64 or 96 (32/mount failure + 64/some mount succeeded)
		# as valid return statuses from mount.  This partly works around
		# the problem of mount reporting an error status when
		# some filesystems are already mounted.

		pre_mountall
		mount_all_nfs
		post_mountall
	fi
}


# Not for loopback!
[ "$IFACE" != "lo" ] || exit 0

# Lock around this otherwise insanity may occur
mkdir /var/run/network/mountnfs 2>/dev/null || exit 0

do_start

rmdir /var/run/network/mountnfs 2>/dev/null || exit 0
