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

#	ATT: ident	"sadmin:admin/checkfsys	2.7"

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

#	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.

#	Check a file system for errors.
#help# 
#help#	Checkfsys checks a file system for errors or inconsistencies.
#help#	If there are errors, this procedure attempts to repair them.

#!	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

usage () {
	echo >&2 "Usage:  `basename $0` [ -p pattern ]"
	exit 1
}

cleanup () {
	if [ -n "${unmounted}" ]
	then
		echo >&2 "\nRemounting ${bddrive} on ${filesys}."
		/etc/mount ${bddrive} ${filesys}
	fi
}

patterns=
case $# in
0 )
	;;
2 )
	if [ $1 = -p ]
	then
		patterns=$2
		shift 2
	else
		usage
	fi
	;;
* )
	usage
esac

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

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

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

rootdrive=`/etc/devnm /|sed -e 's/ .*//'`
if [ "${rootdrive}" = "${bddrive}" ]
then
	echo >&2 "\n$0: This utility will not check the root file system.
To check the root file system, run, \"/etc/fsck ${rootdrive}\"
with the machine in single user mode, and with root being the
only file system mounted."
	exit 1
fi

/etc/fsstat ${bddrive} > /dev/null
exit=$?

if [ ${exit} -eq 3 ]
then
	echo >&2 "\n/etc/fsstat: unexpected error. Contact your System Administrator."
	exit 1
fi

if [ ${exit} -eq 2 ]
then
	filesys=`/etc/mount  |
		sed -n "\\; on ${bddrive} ;s;^\\([^ ]*\\) .*;\\1;p"`
	if [ -z "${filesys}" ]
	then
		echo >&2 "${bddrive} is mounted, but is not in the mount table.
It needs to be unmounted before it can be checked."
		exit 1
	fi

	echo >&2 "\n${bddrive} is already mounted on \"${filesys}\".
This command cannot run with it mounted.\n"
	if  checkyn ${flags} -H"
	${bddrive} is already in use; it is mounted on ${filesys}.
	Before you can use the drive for some other purpose, you
	must unmount it." \
		-f "Do you want to unmount it?"
	then
		if  diskumount ${bddrive} ${filesys}
		then
			unmounted=true
		else
			echo >&2 "\nThis command cannot complete because \"${filesys}\" could not be unmounted.
Unmount it and try again."
			exit 0
		fi
	else
		exit 0
	fi
fi

l=`disklabel $$ ${cddrive}`
eval `labelfsname "${l}"`

if [ -z "${fsname}" ]
then
	echo >&2 "\n${cddrive} has not been properly initialized as a file system
with /etc/labelit.  It will not be checked."
	cleanup
	exit 0
fi

checktype=`checklist ${flags} -fep -H '
	The "check" looks for file system damage but does not attempt to
	fix it.  The "interactive repair" asks you to decide what should be
	done for each error.  The "automatic repair" makes standard repairs
	when it finds errors.  "automatic" generally safe, although there are
	some problems it cannot handle.  In those cases you need a file system
	repair expert to fix things.
	     Most users will be satisfied using "automatic".  For particularly
	sensitive or unreproducible data we recommend that you use "check"
	first and then use either "interactive" or "automatic" repair.' \
	"Disk '${label}', file system '/${fsname}'.
Select:
	check (no repairs attempted)
	interactive repair
	automatic repair
[c, i, a, ?, q]:" check interactive automatic`

#	Interrupting an fsck can be dangerous and cause strange messages,
#	therefore, they are ignored.
trap '' 2
case "${checktype}" in
check )
	/etc/fsck -n -D ${cddrive}
	;;
interactive )
	/etc/fsck -D ${cddrive}
	;;
automatic )
	/etc/fsck -y -D ${cddrive}
	;;
* )
	admerr $0 Invalid checktype "'${checktype}'"
	cleanup
	exit 1
esac
exit=$?

trap 'cleanup;exit 0' 2

if [ ${exit} -ne 0 ]
then
	echo "
	File system check did not finish successfully.  Either the
	disk is damaged beyond this procedure's ability to repair it
	or there is some other problem.  Please call your service
	representative if you think there is a machine problem."
else
	cleanup
	if [ -n "${unmounted}" ]
	then
		if [ ! -d /${fsname}/lost+found ]
		then
			echo >&2 "Warning: ${filesys} does not have a lost+found directory.
Use the \"mklost+found\" utility to create this directory."
			exit 0
		fi
		cd /${fsname}/lost+found
		if [ -n "`/bin/ls`" ]
		then
			echo >&2 "
There are files in the /${fsname}/lost+found directory. These
are files that belong in the file system somewhere, but whose
names have been lost (the contents of the file is still intact).
You should look at each file and if you recognize it, move it
to the proper directory and name."
		fi
	fi
fi
