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

#ident	"$Header: checkfsys 1.4 89/10/11 $@(#)sadmin:admin/checkfsys	2.10"
#	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.  If there are
#help#	are errors, this procedure attempts to repair them.

#!	chmod +x ${file}

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
}

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=`ls ${dir} 2>/dev/null`
fi

ddrive=`selectdevice -b $$ /dev/dsk ${patterns}`
ndrive=`drivename ${ddrive}`

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

l=`/etc/labelit ${ddrive}`
eval `labelfsname "${l}"`

if [ -z "${fsname}" ]
then
	echo ${fsname} ${ddrive}
	echo '	This medium was not properly initialized as a file system.
	It will not be checked.'
	exit 0
fi
ERR=0
if [ -f /${fsname} ]
	then
		ERR=2
	elif [ -d /${fsname} ]
	then
	mounted=`/etc/mount | sed 's; .*;;'`
	if echo "$mounted" | grep "/${fsname}\$"
	then
		ERR=3
	fi
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;  it is 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 ${ddrive}
	;;
interactive )
	/etc/fsck ${ddrive}
	;;
automatic )
	/etc/fsck -y ${ddrive}
	;;
* )
	admerr $0 Invalid checktype "'${checktype}'"
	exit 1
esac
exit=$?

trap 'exit 0' 2

if [ ${exit} -ne 0 ]
then
	echo "
	File system check did not finish successfully.
	Either the file system 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
	if [ $ERR -ne 0 ]
	then
		echo "\tUnable to mount file system '${fsname}' to complete checking."
		case "$ERR" in
		"2") echo "\tA file by that name already exists in the root directory."
			;;
		"3") echo "\tA file system with that name is already mounted."
			;;
		*) echo "\tUnknown error $ERR."
			;;
		esac
	  exit 1
	fi

	trap "	trap '' 1 2
		diskumount -n '${ndrive}' ${ddrive}" 0
	/etc/mount ${ddrive} /${fsname} -r
	if [ ! -d /${fsname}/lost+found ]
	then
		echo "There is no lost+found directory on file system '${fsname}'"
		exit 0
	fi
	cd /${fsname}/lost+found
	if [ -n "`ls`" ]
	then
		echo "
	There some files in the /${fsname}/lost+found directory.
	These are files that belong in the file system somewhere, but whose
	names have been lost, but whose contents is still intact.
	You should mount this file system, look at each file and if you
	recognize it, move it to the proper directory and name."
	fi
	cd /
fi
