#!/bin/sh
# Early fs check
#
. /boot.conf
test "$LOG_START" && echo "<*> Executing '$0 $*'"

exec </dev/console

test "$LOG_STAGE" && echo "* Testing root device read-only status"
if (echo -n >>"/Testing filesystem status") >/dev/null 2>&1; then
    rm -f "/Testing filesystem status"
    echo    "*** ERROR: Root device has been mounted read-write. Cannot fs check!"
    echo -n "    Press ENTER to continue:"; read junk
else
    test "$LOG_STAGE" && echo "* Running fs check"
    fsck -A -a
    # If there was a failure, drop into single-user mode.
    if test $? -gt 1; then
        echo "*** An error occurred during the file system check. ***"
        echo "*** Dropping you into a shell for repairs.          ***"
        echo "*** Running 'e2fsck -v -y <partition>' might help.  ***"
        echo
        echo "Once you exit the single-user shell, the system will reboot"
        echo
        #sulogin
	sh
        echo "* Unmounting file systems"
        umount -r -n -a
        mount -r -n -o remount,ro /
        echo "* Rebooting system"
        sleep 2
        reboot
    fi
    if test "$ROOT_FS_STATE" != "RO" -a "$ROOT_FS_STATE" != "RW" ; then
        echo -n "Press <Y> to remount root dev or <Enter> to leave it RO:"
        read reply
        if test "$reply" = "Y" -o "$reply" = "y"; then
	    ROOT_FS_STATE="RW"
        fi
    fi
    if test "$ROOT_FS_STATE" = "RW"; then
        test "$LOG_STAGE" && echo "* Remounting root device read-write"
        mount -n -o remount,rw /
        if test $? -gt 0; then
    	    echo    "*** Attempt to remount root device as read-write failed!"
    	    echo    "    This is going to cause serious problems!"
    	    echo -n "    Press <Enter> to continue:"; read junk
        fi
    fi    
fi

exit 0
