#! /bin/sh

. "`dirname $0`/rcdefs"

BOOTLOG= Print -n "Checking filesystems... "
# Test to see if the root partition is read-only, like it ought to be.
READWRITE=no
if echo -n >> "Testing filesystem status"; then
 ${BIN_RM} -f "Testing filesystem status"
 READWRITE=yes
fi

# Check the integrity of all filesystems
if [ ! $READWRITE = yes ]; then
 ${BIN_FSCK} -A -a
 # If there was a failure, drop into single-user mode.
 if [ $? -gt 1 ] ; then
  ${BIN_FSCK} -A -v
  if [ $? -gt 1 ] ; then
  echo
  echo
  echo "**************************************"
  echo "fsck returned error code - REBOOT NOW!"
  echo "**************************************"
  echo
  echo
  fi
 fi
 # Remount the root filesystem in read-write mode
 echo "Remounting root device with read-write enabled."
 ${BIN_MOUNT} -w -n -o remount /
 if [ $? -gt 0 ] ; then
  echo
  echo "Attempt to remount root device as read-write failed!  This is going to"
  echo "cause serious problems...  "
  echo 
  echo "If you're using the UMSDOS filesystem, you **MUST** mount the root partition"
  echo "read-write!  You can make sure the root filesystem is getting mounted "
  echo "read-write with the 'rw' flag to Loadlin:"
  echo
  echo "loadlin vmlinuz root=/dev/hda1 rw   (replace /dev/hda1 with your root device)"
  echo
  echo "Normal bootdisks can be made to mount a system read-write with the rdev command:"
  echo
  echo "rdev -R /dev/fd0 0"
  echo
  echo "You can also get into your system by using a bootkernel disk with a command"
  echo "like this on the LILO prompt line:  (change the root partition name as needed)"
  echo 
  echo "LILO: mount root=/dev/hda1 rw"
  echo
  echo "Please press ENTER to continue, then reboot and use one of the above methods to"
  echo -n "get into your machine and start looking for the problem. " 
  read junk; 
 fi
else
 echo "Testing filesystem status: read-write filesystem"
 if [ -d /DOS/linux/etc -a -d /DOS/linux/dev ]; then # no warn for UMSDOS
  ${BIN_CAT} << EOF

*** ERROR: Root partition has already been mounted read-write. Cannot check!

For filesystem checking to work properly, your system must initially mount
the root partition as read only. Please modify your kernel with 'rdev' so that
it does this. If you're booting with LILO, add a line:

   read-only

to the Linux section in your /etc/lilo.conf and type 'lilo' to reinstall it.

If you boot from a kernel on a floppy disk, put it in the drive and type:
  rdev -R /dev/fd0 1

If you boot from a bootkernel disk, or with Loadlin, you can add the 'ro' flag.

This will fix the problem *AND* eliminate this annoying message. :^)

EOF
  echo -n "Press ENTER to continue. "
  read junk;
 fi
fi
BOOTLOG= Print "done."

# remove /etc/mtab* so that mount will create it with a root entry
${BIN_RM} -f /etc/mtab* /etc/nologin

# mount file systems in fstab (and create an entry for /)
# but not NFS because TCP/IP is not yet configured
# and not ISO9660 because the CD-ROM drivers are not yet loaded
BOOTLOG= Print "Mounting... "
${BIN_MOUNT} -avt nonfs,noiso9660
#BOOTLOG= Print "done."

${BIN_RM} -f /var/run/utmp

# Looks like we have to create this.
${BIN_CAT} /dev/null > /var/run/utmp

# Configure the system clock.
Print -n "Configuring clock... "
${BIN_CLOCK} -su
Print "done."

Print -n "Finding module dependencies... "
${BIN_DEPMOD} -a -e -s
Print "done."
