#! /sbin/sh
#Tag 0x00000f00

# (u)mount file systems
# "$Revision: 1.27 $"

cd /

case $1 in
'start')
	# The /usr filesystem MAY HAVE BEEN mounted earlier earlier so
	# that unbundled products would have a chance to execute any
	# initialization code after mounting /usr but before mounting
	# the rest of /etc/fstab.
	#
	# Mount any filesystems listed in /etc/fstab that we haven't
	# yet mounted.  Note that this may include /usr if we haven't
	# already mounted it.
	#
	if /sbin/chkconfig verbose
	then
		echo "Mounting filesystems:"
	fi

	# Mount file systems according to file system table /etc/fstab.
	# check all filesystems if necessary, in parallel.
	/sbin/fsck -m -c -y | grep -v 'mounted file system, ignored'

	if /sbin/mount -at efs
	then :
	else
	    echo '\07Some filesystems failed to mount; may be unable to reach multiuser state'
	    sleep 5
	fi

	# mv /tmp to /.oldtmp and create a new /tmp unless the existing
	# /tmp or any of its subdirectories is a mount point, or unless /tmp
	# is a symbolic link.  In these cases, just leave /tmp alone.

	chkconfig nocleantmp
	if test $? = 1 -a -d /tmp -a ! -l /tmp ; then
	    # 2 tests because we don't want to run the command part, and
	    # the shell does the command substitution stuff before running
	    # test, and we don't want to get "/tmp: No such file or directory"
	    # when /tmp doesn't exist (as is the case in the miniroot.
	    if test -z "`mount | grep ' on /tmp[/ ]'`" \
		-a "`ls -la /tmp | wc -l`" -gt 3; then

		# Any existing /.oldtmp means we somehow didn't remove
		# it in rmtmpfiles or perhaps never even ran rmtmpfiles
		# last time we booted.  It might contain good stuff.

		if test -d /.oldtmp; then
		    ls -a1 /tmp | sed -e '/^\.$/d' -e '/^\.\.$/d' | xargs -i rm -rf /.oldtmp/{}
		    ls -a1 /tmp | sed -e '/^\.$/d' -e '/^\.\.$/d' | xargs -i mv /tmp/{} /.oldtmp
		else
		    rm -rf /.oldtmp
		    mv /tmp /.oldtmp
		    mkdir /tmp
		    chmod +rwx /tmp
		fi
	    fi
	fi
	;;

'stop')
	if /sbin/chkconfig verbose
	then
		echo "Unmounting filesystems:"
	fi
	sync
	_DLMNTS=""
	if [ "`/sbin/nvram diskless 2> /dev/null`" -eq 1 ] ; then
		_DLMNTS=",/swap,/sbin"
	fi
	/sbin/umount -ak -b /var,/usr,/debug,/proc,/dev/fd${_DLMNTS}
	sync
	;;

*)
	echo "usage: /etc/init.d/filesystems {start|stop}"
	;;
esac
