#!/bin/sh
#
# check file system integrity
#

#locate and source functions script
source `PATH=$INIT_D/:/sbin/init.d:/etc/init.d:/etc/rc.d type -p functions`

case "$1" in
  start)
     if [ ! -r /etc/fstab ]; then
         echo -n 'Cannot read /etc/fstab => skipping filesystem check!'
         print_status skipped
         exit 2
     fi
     
     if [ -f /fastboot ]
     then
        echo -n '/fastboot exists => skipping filesystem check!'
        print_status skipped
        exit 2
     else
        mount -n -o remount,ro /
        if [ $? = 0 ]
        then
           if [ -f /forcefsck ]
           then
              echo '/forcefsck exists => forcing filesystem check!'
              force="-f"
           else
              force=""
           fi

           echo 'Checking file systems...'
           fsck $force -s -A -C -T

           if [ $? -gt 1 ]
           then
              $FAILURE
              echo 
              echo 'Filesystem check failed!'
              echo 'Halting system...'
              echo 
              $NORMAL
              
              #using sulogin here is a security problem because sulogin
              #will grant access without asking for a password if the
              #password database is corrupted. Instead, press reset and
              #boot with init=/bin/sh to repair your filesystem
              halt
           else
              $CURS_UP
              print_status success
           fi
        else #mount returned exit code != 0
           echo -n 'Cannot mount root fs read-only => skipping filesystem check!'
           print_status skipped
           exit 2
        fi
     fi #[ -f /fastboot ]
     ;;
  stop)
     ;;
  *)
     exit 1
     ;;
esac
    
exit 0
