#! /bin/sh

# $Copyright:	$
# Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
# Sequent Computer Systems, Inc.   All rights reserved.
#  
# This software is furnished under a license and may be used
# only in accordance with the terms of that license and with the
# inclusion of the above copyright notice.   This software may not
# be provided or otherwise made available to, or used by, any
# other person.  No title to or ownership of the software is
# hereby transferred.

#	Copyright (c) 1984 AT&T
#	  All Rights Reserved

#	THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T
#	The copyright notice above does not evidence any
#	actual or intended publication of such source code.

#ident	"$Header: mountall.sh 1.19 90/08/14 $@(#)initpkg:./mountall.sh	1.10"
if u3b2 || i386
then
	cat << 'END_OF_MOUNTALL' > mountall
#! /bin/sh
#	Mount file systems according to file system table /etc/fstab.
#	Note: written to depend on as few commands as possible.

#	file-system-table format: mountdev fs fstype options
#
#	column 1	block special file name of file system
#	column 2	mount-point directory
#	column 3	file system type
#	column 4	options (comma separated, no white space)
#	column 5+	ignored
#
# NOTE:
#	ALL FIELDS ARE REQUIRED.  Options should contain at least "ro" or "rw".
#	White-space separates columns.
#	Lines beginning with "#" are comments.  Empty lines are ignored.
#	A file system type of "ignore" will cause the entry to be ignored.

fstab=/etc/fstab
mountall=$0
mounttype=
rawdevs=
exitval=0

USAGE="usage: $mountall [ -f <fstype> ]"

if [ $# -ne 0 ]
then
	case $# in
	2)
		if [ $1 != "-f" ]
		then
			echo $USAGE
			exit 2
		fi
		mounttype=$2
		mountall="$mountall -f $2"
		;;
	*)
		echo $USAGE
		exit 2
		;;
	esac
fi

#	The 'Extra' line is added after all fstab entries to
#	keep the rawdevs variable active inside the while loop.

{
	#  Ignore comments, empty lines
	sed -e '/^[ 	]*#/D' -e '/^[ 	]*$/D' ${fstab}
	echo 'Extra'
} |
	while  read dev fs fstype fsflags dummy
	do
		case ${fstype} in
		ignore  |  swap ) #  Ignore swap or ignored filesystems
			continue
			;;
		esac 

		case ${fs} in
		/)		#  Ignore root filesystem
			continue
			;;
		esac

		case ${dev} in
		'Extra')
			if [ -n "${rawdevs}" -a "${rlevel}" -ne 1 ]
			then
				/etc/fsck -p ${rawdevs}
				fsck_ret=$?
				rlevel=1 export rlevel
				${mountall}	# shell recursion
				exit ${fsck_ret}
			fi
			exit $exitval
			;;
		esac 

		#	Only mount types specified by -f option
		#	(or everything if -f type not specified)

		if [ -n "${mounttype}" -a "${fstype}" != "${mounttype}" ]
		then
			continue
		fi

		if [ "${fstype}" = "nfs" ]
		then
			/bin/true
		else
			msg=`/etc/fsstat ${dev} 2>&1`
		fi
		case $? in
		0)
			# Only mount file systems in the order specified
			# by fstab.  Otherwise, sub-mounts may not have
			# a mount point.

			if [ -z "${rawdevs}" ]
			then
				/etc/mount ${fs}
				status=$?
				if [ $status -ne 0 ]
				then
					exitval=$status
				fi
			fi
			;;
		1)
			rdev=`basename ${dev}`
			if [ -c /dev/rdsk/${rdev} ]
			then
				rdev=/dev/rdsk/${rdev}
			elif [ -c /dev/r${rdev} ]
			then
				rdev=/dev/r${rdev}
			else
				rdev=${dev}
			fi
			rawdevs="${rawdevs} ${rdev}"

			if [ "${rlevel}" -eq 1 ]
			then
				echo "${msg}\n\t${dev} needs attention."
				exitval=1
				continue
			fi
			echo "${msg}\n\t${rdev} will be checked."

			;;
		2)
			# echo "${dev} already mounted"
			;;
		3)
			/etc/mount ${fs}
			status=$?
			if [ $status -ne 0 ]
			then
				echo "${dev} is not a valid filesystem"
				exitval=$status
			fi
			;;
		esac
	done
END_OF_MOUNTALL
fi
