#! /bin/sh
#
# |-----------------------------------------------------------|
# | Copyright (c) 1991, 1990 MIPS Computer Systems, Inc.      |
# | All Rights Reserved                                       |
# |-----------------------------------------------------------|
# |          Restricted Rights Legend                         |
# | Use, duplication, or disclosure by the Government is      |
# | subject to restrictions as set forth in                   |
# | subparagraph (c)(1)(ii) of the Rights in Technical        |
# | Data and Computer Software Clause of DFARS 252.227-7013.  |
# |         MIPS Computer Systems, Inc.                       |
# |         950 DeGuigne Avenue                               |
# |         Sunnyvale, California 94088-3650, USA             |
# |-----------------------------------------------------------|
#
# $Header: nfs,v 1.31.2.1 92/06/24 13:35:02 marker Exp $
#
# Initialize the nfs software.
#

USAGE="usage: /etc/init.d/nfs {start|stop}" ;

not() {
	eval ${1+"$@"} && return 1 || return 0
}

main() {
	case $1 in
	start)	nfs_start ${1+"$@"} ;;
	stop)	nfs_stop ${1+"$@"} ;;
	*)	echo "$USAGE"; exit 1 ;;
	esac
}

nfs_start() {
	PATH=/net:/bin:/etc:/usr/bin:/usr/etc:/usr/ucb:/usr/etc/yp
	nfs=/etc
	
	#
	# Have they set the hostname yet.  If not, don't start the network.
	#
	if [ "`hostname`" = "no_hostname_set" ] ; then
		exit 0
	fi
	#
	#   Check for existance of tcp first.
	#
    	if [ ! -x /etc/havetcp ] || not /etc/havetcp
	then
		exit 0
	fi
	#
	# Fire up NFS daemons if they are present and executable.
	# The portmapper is the sine qua non, since RPC doesn't work without
	# him.  Now that inetd supports RPC, init.d/tcp must start portmap.
	# Therefore init.d/tcp must be invoked before this script.
	#
	if [ -x /etc/havenfs ] && /etc/havenfs
	then
		# Truncate /etc/xtab & run exportfs
		if [ -x $nfs/exportfs ]; then
			> /etc/xtab
			if $nfs/exportfs -a; then
				echo "Export file systems"
			else
				echo "\nWARNING: /etc/exportfs failed \c"
				echo "(Can't mount to this system)\n"
			fi
		else
			echo "\n\nWARNING: unexecutable file: /etc/exportfs \c"
			echo "(Can't mount to this system)\n"
		fi

		echo "NFS daemons:\c"
	
		# NFS server daemons
		if [ -x $nfs/nfsd ]; then
			$nfs/nfsd 4;			echo " nfsd\c"
		fi
	
		# NFS client bio daemons
		if [ -x $nfs/biod ]; then
			$nfs/biod 4;			echo " biod\c"
		fi
	
		# NFS Lock manager daemon
		if [ -r /etc/lockd.conf ]; then
			$nfs/rpc.lockd `sed -n 1p /etc/lockd.conf`
		else
			$nfs/rpc.lockd
		fi
		kopt set enable_lockd 1 > /dev/null 2>&1
		echo " lockd\c"

		# NFS status monitoring daemon
		$nfs/rpc.statd;			echo " statd\c"

		echo "."

		if [ -r /etc/automount.conf ]; then
		        $nfs/automount `sed -n 1p /etc/automount.conf`
			echo "Starting automounter."
		fi

		# You can't NFS mount yourself.
		mount -a -v -t nfs
	fi
}


nfs_stop() {
	if mount | grep "on \/ type nfs "  >/dev/null 2>&1
	then
	  : # we are mounted over nfs, don't unmount yet
	else
	  umount -v -k -t nfs
	fi
	pidlist=""
	# For ever string we search for, we need to add another space to test
	# statement below.
	for proc in nfsd biod rpc.lock rpc.stat automoun
	do
		pidlist="$pidlist `ps -e | grep ' '$proc'$' |
		    sed -e 's/^  *//' -e 's/ .*//'`"
	done
	if test "$pidlist" != "     "
	then
		kill $pidlist
	fi

}

main ${1+"$@"}
exit 0
