--- /System/Library/StartupItems/NFS/NFS.orig	2006-12-25 15:16:59.000000000 -0500
+++ /System/Library/StartupItems/NFS/NFS	2007-01-10 23:58:09.000000000 -0500
@@ -6,23 +6,26 @@
 
 . /etc/rc.common
 
-AUTOMOUNTDIR=/private/var/automount
+AUTOMOUNTDIR="/private/var/automount"
+AUTOMOUNT_HINTFILE="/Network/.localized"
+AUTOMOUNT_MARKER="/var/run/automount.initialized"
+LOCKFILE="/var/run/NFS.StartupItem"
 
 StartService ()
 {
     CheckForNetwork 
     if [ "${NETWORKUP}" = "-NO-" ]; then exit; fi
-    lockfile -r 0 /var/run/NFS.StartupItem || exit 0
+    lockfile -r 0 $LOCKFILE || exit 0
 
     ##
     # Set up NFS client.
     ##
     echo "Starting network file system"
 
-	if [ -d ${AUTOMOUNTDIR} ]; then
-		chflags -R nouchg ${AUTOMOUNTDIR}
-		rm -rf ${AUTOMOUNTDIR}
-	fi
+    if [ -d ${AUTOMOUNTDIR} ]; then
+        chflags -R nouchg ${AUTOMOUNTDIR}
+        rm -rf ${AUTOMOUNTDIR}
+    fi
 
     # nsfiod is the NFS asynchronous block I/O daemon, which implements
     # NFS read-ahead and write-behind caching on NFS clients.
@@ -38,33 +41,45 @@
     ##
 
     ##
-    # gather list of NFS exports
+    # gather list of NFS exports from NetInfo and generate /etc/exports from it.
+    # This is a stopgap measure until mountd itself learns to consult NetInfo.
     ##
-    exports_ni=$(niutil -list . /exports 2> /dev/null | wc -w)
+
+    echo "# GENERATED FILE - DO NOT EDIT!" > /etc/exports
+    echo "# Make changes under /exports in NetInfo Manager" >> /etc/exports
+
+    for i in `niutil -list . /exports | awk '{print $2}' | sort`
+    do
+        path=`niutil -readprop . /exports/$i path`
+        export_opts=`niutil -readprop . /exports/$i export_opts`
+
+        echo "$path    $export_opts" >> /etc/exports
+    done
+
     # Look for exports in /etc/exports, ignoring comments and blank lines.
     exports_etc=$(grep -v '^[[:space:]]*\(#\|$\)' /etc/exports 2> /dev/null | wc -l)
-    exports=$(($exports_ni + $exports_etc))
+    exports=$exports_etc
 
     # if we are an NFS server, turn on NFS locking by default:
     if [ "${exports}" -gt 0 ]; then
-	if [ "${NFSLOCKS:=-AUTOMATIC-}" = "-AUTOMATIC-" ]; then
-	    NFSLOCKS=-YES-;
-	fi
+        if [ "${NFSLOCKS:=-AUTOMATIC-}" = "-AUTOMATIC-" ]; then
+            NFSLOCKS=-YES-;
+        fi
     fi
 
     if [ "${NFSLOCKS:=-AUTOMATIC-}" = "-YES-" ]; then
-	# we definitely want locks on, so turn them on now
-	rpc.statd
-	rpc.lockd
+        # we definitely want locks on, so turn them on now
+        rpc.statd
+        rpc.lockd
     fi
     if [ "${NFSLOCKS:=-AUTOMATIC-}" = "-AUTOMATIC-" ]; then
-	# delay starting daemons until we know we need them
+        # delay starting daemons until we know we need them
 
-	# invoke rpc.statd to send any SM_NOTIFY messages and quit.
-	rpc.statd -n
+        # invoke rpc.statd to send any SM_NOTIFY messages and quit.
+        rpc.statd -n
 
-	# -w says to wait for signal from kernel, then start daemons
-	rpc.lockd -w
+        # -w says to wait for signal from kernel, then start daemons
+        rpc.lockd -w
     fi
 
     ##
@@ -79,47 +94,79 @@
     rm -f /var/db/mountdtab
 
     if [ "${exports}" -gt 0 ]; then
-	echo "Starting Network File System server"
-	mountd
+        echo "Starting Network File System server"
+        mountd
 
-	# If the NetInfo config/nfsd directory contains startup args for nfsd, use those.
-	arguments=`niutil -readprop . /config/nfsd arguments`
-	if [ "${arguments}" = "" ]; then
-		arguments="-t -u -n 6"
-	fi
-	nfsd ${arguments}
+        # If the NetInfo config/nfsd directory contains startup args for nfsd,
+        # use those.
+        arguments=`niutil -readprop . /config/nfsd arguments`
+        if [ "${arguments}" = "" ]; then
+            arguments="-t -u -n 6"
+        fi
+        nfsd ${arguments}
     fi
 
     ##
     # Start the automounter
     ##
 
+    NETWORK_PROTOCOL=`niutil -readprop . /mounts default_protocol 2>/dev/null`
+    NETWORK_PROTOCOL=${NETWORK_PROTOCOL:-udp}
+
     if [ "${AUTOMOUNT:=-YES-}" = "-YES-" ]; then
-	automount -m /Network -nsl -mnt ${AUTOMOUNTDIR}
-	ln -s /automount/Library /Network/Library
-	automount -m /automount/Servers -fstab -mnt /private/Network/Servers \
-		-m /automount/static -static -mnt ${AUTOMOUNTDIR}
-	ln -s /automount/Servers /Network/Servers
-	
-	#
-	# Hint that the name /Network should be localized:
-	#
-	ln -s . /Network/.localized
-    fi
-	
-	#
-	# Leave a mark upon completion of the automounter startup:
-	#
-	touch /var/run/automount.initialized
+        automount -$NETWORK_PROTOCOL -m /Network -nsl -mnt ${AUTOMOUNTDIR}
+        ln -s /automount/Library /Network/Library
+
+        automount -$NETWORK_PROTOCOL \
+            -m /automount/Servers -fstab -mnt /private/Network/Servers \
+            -m /automount/static -static -mnt ${AUTOMOUNTDIR}
+        ln -s /automount/Servers /Network/Servers
+        
+        #
+        # Hint that the name /Network should be localized:
+        #
+        ln -s . $AUTOMOUNT_HINTFILE
+    fi
+
+    #
+    # Leave a mark upon completion of the automounter startup:
+    #
+    touch $AUTOMOUNT_MARKER
 }
 
 StopService ()
 {
+    echo "Stopping NFS"
+
+    #
+    # Forcibly umount all automounted file systems.
+    #
+    mount | grep automounted | grep '\:' | \
+    while read DYNMOUNT
+    do
+        MOUNTPOINT=`echo $DYNMOUNT | awk '{ print $3 }'`
+        echo $DYNMOUNT
+        umount -f $MOUNTPOINT
+    done
+
+    killall automount
+    killall -9 mountd
+    killall -9 nfsiod
+    killall -9 rpc.statd
+    killall -9 rpc.lockd
+    killall -9 nfsd-server
+    killall -9 nfsd-master
+
+    rm -f $LOCKFILE
+    rm -f $AUTOMOUNT_HINTFILE
+    rm -f $AUTOMOUNT_MARKER
     return 0
 }
 
 RestartService ()
 {
+    StopService
+    StartService
     return 0
 }
 
