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

source ${INIT_D:-/sbin/init.d}/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
              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
