#!/bin/bash
# Begin $rc_base/init.d/checkfs - File System Check Script

# Based on checkfs script from LFS-3.1 and earlier.
# Rewritten by Gerard Beekmans  - gerard@linuxfromscratch.org
# Patch to handle all fsck variants by A. Luebke - luebke@users.sourceforge.net

source /etc/sysconfig/rc
source $rc_functions

if [ -f /fastboot ]
then
	echo "Fast boot requested, will not perform file system checks"
	exit 0
fi

echo "Mounting root file system in read-only mode..."
mount -n -o remount,ro /
evaluate_retval

if [ $? != 0 ]
then
	$FAILURE
	echo
	echo "Cannot check root file system because it could not"
	echo "be mounted in read-only mode."
	echo
	echo -n "When you press enter, this system will be halted."
	$NORMAL
	echo
	echo "Press enter to continue..."
	read
	echo "gonna run: $rc_base/init.d/halt"
	$rc_base/init.d/halt
fi

if [ -f /forcefsck ]
then
	echo "/forcefsck exists, forcing file system check"
	options="-f"
else
	options=""
fi

echo "Checking file systems..."
#Note: -a option used to be -p; but this fails e.g. on fsck.minix
fsck $options -a -A -C -T
error_value=$?

if [ "$error_value" = 1 ]
then
	$WARNING
	echo "File system errors were found and have been corrected."
	echo "You may want to double-check that everything was fixed"
	echo -n "properly"
	$NORMAL
	print_status warning
fi

if [ "$error_value" = 0 ]
then
	print_status success
fi

if [ "$error_value" = 2 -o "$error_value" = 3 ]
then
	$WARNING
	echo "File system errors were found and have been corrected, but"
	echo "the nature of the errors require this system to be rebooted."
	echo
	echo -n "When you press enter, this system will be rebooted."
	$NORMAL
	print_status warning
	echo
	echo "Please press enter to continue..."
	read
	$rc_base/init.d/reboot
fi

if [ "$error_value" -gt 3 -a "$error_value" -lt 16 ]
then
	$FAILURE
	echo "File system errors were encountered that couldn't be"
	echo "fixed automatically. This system cannot continue to boot"
	echo "and will therefore be halted until those errors fixed manually"
	echo "by a System Administrator."
	echo
	echo -n "When you press enter, this system will be halted."
	$NORMAL
	print_status failure
	echo
	echo "Press enter to continue..."
	read
	$rc_base/init.d/halt
fi

# End $rc_base/init.d/checkfs

