#! /bin/sh
### BEGIN INIT INFO
# Provides:          mountnfs
# Required-Start:    $local_fs
# Required-Stop:
# Should-Start:      $network $portmap nfs-common  udev-mtab
# Default-Start:     S
# Default-Stop:
# Short-Description: Wait for network file systems to be mounted
# Description:       Network file systems are mounted by
#                    /etc/network/if-up.d/mountnfs in the background
#                    when interfaces are brought up; this script waits
#                    for them to be mounted before carrying on.
### END INIT INFO

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

proc do_wait_async_mount {
	# Read through fstab line by line. If it is NFS, set the flag
	# for mounting NFS file systems. If any NFS partition is found
	# then wait around for it.

	setvar waitnfs = ''
	for file in "$(eval ls $(fstab_files))" {
		if test -f $file {
			while read DEV MTPT FSTYPE OPTS REST {
				case (DEV) {
				  ""|\#* {
					continue
					}
				}
				case (OPTS) {
				  noauto|*,noauto|noauto,*|*,noauto,* {
					continue
					}
				}
				case (FSTYPE) {
				  nfs|nfs4|smbfs|cifs|coda|ncp|ncpfs|ocfs2|gfs|ceph {
					}
				  * {
					continue
					}
				}
				case (MTPT) {
				  /usr/local|/usr/local/* {
					}
				  /usr|/usr/* {
					setvar waitnfs = ""$waitnfs $MTPT""
					}
				  /var|/var/* {
					setvar waitnfs = ""$waitnfs $MTPT""
					}
				}
			} < "$file"
		}
	}

	# Wait for each path, the timeout is for all of them as that's
	# really the maximum time we have to wait anyway
	setvar TIMEOUT = '900'
	for mountpt in $waitnfs {
		log_action_begin_msg "Waiting for $mountpt"

		while ! mountpoint -q $mountpt {
			sleep 0.1

			setvar TIMEOUT = $(( $TIMEOUT - 1 ))
			if test $TIMEOUT -le 0 {
				log_action_end_msg 1
				break
			}
		}

		if test $TIMEOUT -gt 0 {
			log_action_end_msg 0
		}
	}
}

case (1) {
    start {
        # Using 'no !=' instead of 'yes =' to make sure async nfs
        # mounting is the default even without a value in
        # /etc/default/rcS
        if test no != $ASYNCMOUNTNFS  {
                do_wait_async_mount
        } else {'
                FROMINITD=yes' /etc/network/if-up.d/mountnfs
        }
        }
    restart|reload|force-reload {
        echo "Error: argument '$1' not supported" >&2
        exit 3
        }
    stop|status {
        # No-op
        }
    * {
        echo "Usage: $0 start|stop" >&2
        exit 3
        }
}

: