#! /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: bcheckrc,v 1.15.1.1 92/06/24 13:35:43 marker Exp $

# ***** This file has those commands necessary to check the file
# system, date, and anything else that should be done before mounting
# the file systems.

PATH=/etc:/bin
ROOTFS=/dev/root

SYNC() { sync; sleep 1; sync; sleep 1; sync; }

main() {
	#
	# The bcheckrc_X functions do not return, so failure means
	# no function exists.
	#
        if mount | grep "on \/ type nfs "  >/dev/null 2>&1
        then
          # root is mounted over nfs, don't check root
          exit 0
        fi

        if mount | grep "on \/ type ffs "  >/dev/null 2>&1
        then
          # root is mounted on a ffs, fsck root below
          :
        else
          # Can't tell how root is mounted, don't try fsck
          exit 0
        fi

	echo "\nChecking root file system (${ROOTFS}) if necessary."
	FSTYPE=`fstyp $ROOTFS`
	SYNC;
	bcheckrc_$FSTYPE ${1+"$@"}
	echo "Bad fs type for $ROOTFS: $FSTYPE"; 
	exit 1;
}

bcheckrc_S51K() {
	#
	# Only fsck dirty file systems.
	# Fsck handles remount or reboot itself.
	#
	fsstat $ROOTFS  >/dev/null 2>&1 ||
		/etc/fsck.S51K -y -D -b ${rootfs}
	exit
}

bcheckrc_ffs() {
	fsstat $ROOTFS  >/dev/null 2>&1 && exit 0; 

	fsck.ffs -d -y $ROOTFS 
	case $? in
	0)	echo "fsck.ffs: finished normally"
		;;
	2)	#
		# Berkeley fsck semantics say enter single user now.
		# Wish I could.
		#
		echo "fsck.ffs: received SIGQUIT -- ignored."
		;;
	4)	echo "\nAutomatically Rebooting UNIX."
		uadmin 1 1;
		echo "Auto-reboot failed.  Reboot UNIX manally."
		;;
	8)	echo "fsck.ffs: abnormal exit."
		;;
	12)	echo "fsck.ffs: received SIGINTR -- disk checks terminated."
		;;
	*)	echo "fsck.ffs: unknown exit status."
		;;
	esac

	exit 0
}

main ${1+"$@"}
