#! /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: mountall,v 1.27.1.1 92/06/24 13:35:46 marker Exp $

#
#	Mount file systems according to file system table /etc/fstab.

#	NFS fstab format
#
#	Field	Meaning
#	-----	-------
#	1	Block special file name of file system
#	2	Mount-point directory
#	3	File system type (ffs, ufs, 4.3, local or nfs)
#	4	Mount options (ro, rw, suid ...) 
#	5	Dump frequency
#	6	Fsck pass number
#
# NOTE: The options passed to the fsck script may or may not be implemented
#	by the binary programs; however, they should be mapped into
#	appropriate file system specific options.  fsck options:
#		a: fsck all non-root file systems as fast as possible
#		d: print debug info
#		C: fsck	only dirty file systems.

error=0

main() {
	case $# in
	0 )	;;
	*)	echo "usage: mountall"
	esac

	#
	#  Do a newfs.ffs on tmp file systems, if any.
	#
	for name in `grep -v "^#" /etc/fstab | grep ",tmp" | sed "s/ .*//" | sed "s/	.*//"`
	do
		echo "newfs.ffs $name"
		newfs.ffs $name
	done

	#
	# Let user skip fsck of clean file systems if (s)he wants.
	# On truly diskless don't ask silly questions!
	#

	## The following two lines are replaced by the one "if grep"
	##numffs=`grep ffs /etc/fstab | wc -l`
	##if [ $numffs -gt 0 ]

	if grep ffs /etc/fstab >/dev/null 2>&1
	then
            # If you want to have this work the "oldway", just redefine
            # Oldway to be "true" on the next line.
            Oldway=false
            if [ "$Oldway" = "true" ]
            then
                # This is the way mountall worked in 4.52 and before.
                # It fsck's all file systems unless "y" is typed.
  		echo ""
  		echo "***** Normally all file systems are fscked."
  		echo "***** To fsck only dirty ones, type 'yes' within 5 seconds: \c"
  	
  		if	expr _"`timed_read 8 2>/dev/null`" : _"y.*" >/dev/null 2>&1
  		then
  			echo "***** Only dirty file systems will be fscked."
  			fsck -a -d -C
  		else
  			echo "\n***** All file systems will be fscked."
  			fsck -a -d
  		fi
            else
                # This is the new way mountall works in 5.00 and later.
                # It fsck's dirty file systems unless "n" is typed.
  		echo ""
  		echo "***** Normally only dirty file systems are fscked."
		echo "To fsck ALL file systems, type 'no' within 5 seconds: \c"

  	
  		if	expr _"`timed_read 8 2>/dev/null`" : _"n.*" >/dev/null 2>&1
  		then
  			echo "***** All file systems will be fscked."
  			fsck -a -d
  		else
  			echo "\n***** Only dirty file systems will be fscked."
  			fsck -a -d -C
  		fi
	    fi
		case $? in
		0)	;;
		2)	error=1
			;;
		8)	echo "Reboot fsck failed... help!"
			error=1
			;;
		12)	echo "Reboot fsck interrupted"
			error=1
			;;
		*)	echo "Unknown error in reboot fsck"
			error=1
			;;
		esac
	fi
	
	# Go to single user?
	single_user $error

	# mount all "local" filesystems except root & proc.  Be verbose about
	#  the output and fsck all filesystems which aren't clean.
	mount -a -c -v -t local -D PROC -D fd

	# Go to single user?
	single_user $?
}

# If an error has occurred while booting, shutdown to single-
# user.  The sysadmin should then deal with the situation before
# going multi-user.  When this script is executed from /etc/init,
# `who am i` will return null.  If run from a user shell, 'root'
# will be returned.
single_user()
{
	if [ $1 -ne 0 ]; then
		set _foo `who am i` _bar
		if [ "_$2" != "__bar" ]; then
			echo "\nRemaining in current state\n"
		else
			set `who -r`
			if [ -x /etc/bad_boot.conf ] &&
			   ([ "_$9" = "_S" ] || [ "_$9" = "_1" ]); then
				/etc/bad_boot.conf
				_exit=$?
				if [ $_exit -ne 0 ]; then
					exit $_exit
				fi
			else
				echo "\nWARNING: Shutting down to single user"
				init 1
				exit 2
			fi
		fi
	fi
}

timed_read() {
	TTY=`tty`
	(read i; echo $i; ) < $TTY &
	sleep $1
	kill -9 $! > /dev/null 2>&1 &
}

main ${1+"$@"}
