#!/bin/sh
###############################################################################
#                                                                             #
# IPFire.org - A linux based firewall                                         #
# Copyright (C) 2007-2022  IPFire Team  <info@ipfire.org>                     #
#                                                                             #
# This program is free software: you can redistribute it and/or modify        #
# it under the terms of the GNU General Public License as published by        #
# the Free Software Foundation, either version 3 of the License, or           #
# (at your option) any later version.                                         #
#                                                                             #
# This program is distributed in the hope that it will be useful,             #
# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
# GNU General Public License for more details.                                #
#                                                                             #
# You should have received a copy of the GNU General Public License           #
# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
#                                                                             #
###############################################################################

. /etc/sysconfig/rc
. ${rc_functions}

case "${1}" in
	start)
		if [ -f /fastboot ]; then
			boot_mesg -n "/fastboot found, will not perform" ${INFO}
			boot_mesg " file system checks as requested."
			echo_ok
			exit 0
		fi

		boot_mesg "Mounting root file system in read-only mode..."
		mount -n -o remount,ro / >/dev/null
		evaluate_retval

		if [ ${?} != 0 ]; then
			echo_failure
			boot_mesg -n "FAILURE:\n\nCannot check root" ${FAILURE}
			boot_mesg -n " filesystem because it could not be mounted"
			boot_mesg -n " in read-only mode.\n\nAfter you"
			boot_mesg -n " press Enter, this system will be"
			boot_mesg -n " halted and powered off."
			boot_mesg -n "\n\nPress enter to continue or wait a minute..." ${INFO}
			boot_mesg "" ${NORMAL}
			read -t 60 ENTER
			${rc_base}/init.d/halt stop
		fi

		if [ -f /forcefsck ]; then
			boot_mesg -n "/forcefsck found, forcing file" ${INFO}
			boot_mesg " system checks as requested."
			echo_ok
			options="-f"
		else
			options=""
		fi

		boot_mesg "Checking file systems..."
		# Note: -a option used to be -p; but this fails e.g.
		# on fsck.minix
		fsck ${options} -a -A -C -T 2>/dev/null
		error_value=${?}

		if [ "${error_value}" = 0 ]; then
			echo_ok
		fi

		if [ "${error_value}" = 1 ]; then
			echo_warning
			boot_mesg -n "WARNING:\n\nFile system errors" ${WARNING}
			boot_mesg -n " were found and have been corrected."
			boot_mesg -n "  You may want to double-check that"
			boot_mesg -n " everything was fixed properly."
			boot_mesg "" ${NORMAL}
		fi

		if [ "${error_value}" = 2 -o "${error_value}" = 3 ]; then
			echo_warning
			boot_mesg -n "WARNING:\n\nFile system errors" ${WARNING}
			boot_mesg -n " were found and have been been"
 			boot_mesg -n " corrected, but the nature of the"
			boot_mesg -n " errors require this system to be"
			boot_mesg -n " rebooted.\n\nAfter you press enter,"
			boot_mesg -n " this system will be rebooted"
			boot_mesg -n "\n\nPress Enter to continue or wait a minute..." ${INFO}
			boot_mesg "" ${NORMAL}
			read -t 60 ENTER
			reboot -f
		fi

		if [ "${error_value}" -gt 3 -a "${error_value}" -lt 16 ]; then
			echo_failure
			sleep 2
			boot_mesg -n "FAILURE:\n\nFile system errors" ${FAILURE}
			boot_mesg -n " were encountered that could not be"
			boot_mesg -n " fixed automatically.  This system"
			boot_mesg -n " cannot continue to boot and will"
			boot_mesg -n " therefore be halted until those"
			boot_mesg -n " errors are fixed manually by a"
			boot_mesg -n " System Administrator.\n\n"
			boot_mesg "" ${NORMAL}
			sulogin
			reboot -f
		fi

		if [ "${error_value}" -ge 16 ]; then
			echo_failure
			boot_mesg -n "FAILURE:\n\nUnexpected Failure" ${FAILURE}
			boot_mesg -n " running fsck.  Exited with error"
			boot_mesg -n " code: ${error_value}."
			boot_mesg "" ${NORMAL}
			exit ${error_value}
		fi
		;;
	*)
		echo "Usage: ${0} {start}"
		exit 1
		;;
esac
