#!/sbin/runscript
# Copyright 1999-2003 Gentoo Technologies, Inc.
# Distributed under the terms of the GNU General Public License v2
# $Header: /home/cvsroot/gentoo-src/rc-scripts/init.d/checkroot,v 1.29 2003/03/09 21:45:22 azarah Exp $


depend() {
	before *
}

start() {
	local retval=0
	
	if [ ! -f /fastboot ]
	then
		ebegin "Remounting root filesystem read-only (if necessary)"
		mount / -n -o remount,ro &>/dev/null
		eend $?

		ebegin "Checking root filesystem"
		if [ -f /forcefsck ]
		then
			ewarn "A full fsck has been forced"
			fsck -C -f -a /
			# /forcefsck isn't deleted because checkfs needs it.
			# it'll be deleted in that script.
		else
			fsck -C -a /
		fi
		retval=$?
		if [ "${retval}" -eq 0 ]
		then
			eend 0
		elif [ "${retval}" -eq 1 ]
		then
			ewend 1 "Filesystem repaired"
		else
			eend 2 "Filesystem couldn't be fixed :("
			/sbin/sulogin ${CONSOLE}
			einfo "Unmounting filesystems"
			/bin/mount -a -o remount,ro &>/dev/null
			einfo "Rebooting"
			/sbin/reboot -f
		fi
	fi

	ebegin "Remounting root filesystem read/write"
	mount / -n -o remount,rw &>/dev/null
	if [ "$?" -ne 0 ]
	then
		eend 2 "Root filesystem could not be mounted R/W :("
		/sbin/sulogin ${CONSOLE}
	else
		eend 0
	fi

	if [ "${BOOT}" = "yes" ]
	then
		# Create /etc/mtab
		grep -v 'rootfs' /proc/mounts > /etc/mtab

		# Remove stale backups
		rm -f /etc/mtab~ /etc/mtab~~
	fi
}


# vim:ts=4
