#	START NEW ARIX SCCS HEADER
#
#	@(#) mountfsys: version 25.1 created on 12/2/91 at 16:59:42
#
#	Copyright (c) 1990 by Arix Corporation
#	All Rights Reserved
#
#	@(#)mountfsys	25.1	12/2/91 Copyright (c) 1990 by Arix Corporation
#
#	END NEW ARIX SCCS HEADER
#

#	ATT: ident	"sadmin:admin/mountfsys	2.9"

#ident	"@(#)sadmin/admin:mountfsys	1.1"

#	Mount a file system.
#help# 
#help#	Mountfsys mounts a file system found on a built-in disk, making
#help#	it available to users.  The file system is unmounted with the
#help#	"umountfsys" command.

#!	chmod +x ${file}
PATH=/bin:/usr/bin:/usr/lbin:/etc

flags="-qq -k$$"	# flags for checkyn to implement [q] (quit)
trap 'exit 0' 1 2 15

unset makedir mountit readonly patterns

while getopts p:ry c 
do
	case "$c" in
	p )
		patterns=$OPTARG
		;;
	y )
		mountit=yes
		;;
	r )
		readonly=-r
		;;
	\? )
		echo >&2 "Usage:  `basename $0` [ -p pattern ] [ -r ] [ -y ]
-r	mount the file system read-only
-y	mount the file system without asking any questions"
		exit 1
		;;
	esac
done

shift `expr $OPTIND - 1`
if [ $# != 0 ]
then
	echo >&2 "Usage:  `basename $0` [ -p pattern ] [ -r ] [ -y ]
-r	mount the file system read-only
-y	mount the file system without asking any questions"
	exit 1
fi

dir=${0}.d
if [ -z "${patterns}"  -a  -d ${dir} ]
then
	patterns=`ls ${dir} 2>/dev/null`
fi

cddrive=`seldevice -c $$ /dev/rdsk ${patterns}`
bddrive=/dev/`expr ${cddrive} : '/dev/r\(.*\)'`

if [ -n "${patterns}" ]
then
	. ${dir}/`selpattern ${bddrive}`
fi

l=`disklabel $$ ${cddrive}`
trap "	trap '' 1 2
	echo drive not mounted." 0
eval `labelfsname "${l}"`

if [ -z "${fsname}" ]
then
	echo '	This drive was not properly initialized as a file system.
	It will not be mounted.
	Suggestions:  1)  This drive may have been used for backups or some
		          other purpose.
		      2)  Use "makefsys" to create a file system on this drive.'
	exit 0
fi
if [ ! -d /${fsname} ]
then
	makedir="
(There is not currently a /${fsname} directory,
but this procedure will make one.)
"
fi

if  test ${mountit}  ||  checkyn ${flags} -f \
	"Disk '${label}', file system '/${fsname}'.${makedir}  Mount it${readonly:+ read-only}?"
then
	trap "" 0 1 2 15
	if [ ! -d /${fsname} ]
	then
		mkdir /${fsname}
		madedir=yes
	fi
	chmod 777 /${fsname}
	if  mountmsg=`/etc/mount ${bddrive} /${fsname} ${readonly} 2>&1`
	then
		echo "/${fsname} mounted."
		exit 0
	fi
	if [ ${madedir} ]
	then
		rmdir /${fsname}
	else
		chmod 0 /${fsname}
	fi
	echo '\tThe mount failed.'
	case "${mountmsg}" in
	*'needs checking'* )
		echo '\tThe file system on this drive needs to be checked.
	Suggestion:  use "checkfsys" to look for and repair file system damage.'
		;;
	* )
		echo "\\t${mountmsg}"
	esac
fi
exit 0
