#!/bin/sh
#
# PROVIDE: checkfs
# REQUIRE: SERVERS
# BEFORE: NETWORKING

$_rc_subr_loaded . /etc/rc.subr

name="checkfs"
start_cmd="checkfs_start"
stop_cmd=":"

NETFS="nonfs,nonfs4,nosmbfs,nocifs,nocodafs,noncpfs,nosysfs,noshfs"
NETFS="$NETFS,nofuse,nofuseblk"

checkfs_start()
{
	echo -n "Mounting root read-only... "
	mount -n -o remount,ro /
	show_rval

	FORCEFSCK=
	[ -f /forcefsck ] && FORCEFSCK="-- -f"

	echo "Checking local filesystems... "
	fsck -A -T -C -a -t $NETFS $FORCEFSCK 2>/dev/null
	fsckret=$?
	echo
	if [ ${fsckret} -gt 1 ]; then
		echo "failed!"
		if [ $((${fsckret}&2)) -eq 2 ]; then
			echo
			echo "************** REBOOT REQUIRED *************"
			echo
			echo " The system will be rebooted automatically"
			echo " in 5 seconds."
			echo
			echo "********************************************"
			echo
			sleep 5
		else
			echo
			echo "*********  FILESYSTEM CHECK FAILED  ************"
			echo
			echo " Please repair manually and reboot. Note that"
			echo " the root filesystem is currently mounted"
			echo " read-only. To remount it read-write type:"
			echo
			echo "  mount -n -o remount,rw /"
			echo
			echo " When you exit the maintenance shell the system"
			echo " will reboot automatically."
			echo
			echo "************************************************"
			echo
			sulogin -p
		fi
		echo "Automatic reboot in progress..."
		umount -a
		mount -n -o remount,ro /
		reboot -f
		exit 0
	fi
}

load_rc_config $name
run_rc_command "$1"
