#! /bin/sh
#
# rcS		Call all S??* scripts in /etc/rcS.d in
#		numerical/alphabetical order.
#
# Version:	@(#)/etc/init.d/rcS  2.76  19-Apr-1999  miquels@cistron.nl
#

# Set the path
PATH=/sbin:/bin:/usr/sbin:/usr/bin
runlevel=S
prevlevel=N
umask 022
export PATH runlevel prevlevel

[ -e /dev/.devfsd ] && mount -v -f -t devfs devfs /dev

# If we're using devfs, start devfsd now - we need the old device names
[ -e /dev/.devfsd -a -x /sbin/devfsd ] && /sbin/devfsd /dev

# Mount /proc (done here so volume labels can work with fsck)
#echo "Mounting proc filesystem"

mount -v -n -t proc /proc /proc

# recover the boot status from the ramdisk.
# it may be signalling us to perform a certain action.
STATUS2=`boot_status 1`

# kernel_check must set the boot status 
# so that we will return here if the power fails. 
      
#echo "Checking fail-safe kernel area"
# kernel check
/sbin/kernel_check

# Flag boot status rcS start
/sbin/boot_status 0 10 

#
#	Source defaults.
#
. /etc/default/rcS
export VERBOSE

#
#	Trap CTRL-C &c only in this shell so we can interrupt subprocesses.
#       The shell invoked by busybox doesn't properly trap control-C
#
trap ":" INT QUIT TSTP

# NOTE: At this point the script needs to check whether root is compact flash.
# if so, we might be booting directly from CF, and need to fsck the root file
# system. Otherwise, if we previously booted from ramdisk or are booting NFS,
# root has already been checked and mounted, and does not need to be checked.
# In any case, a conditional is necessary here.

/sbin/boot_status 0 20 
ROOTFSTYPE=`grep "/dev/root" /proc/mounts | /bin/awk '{ print $3 }'`
if [ $ROOTFSTYPE == "ext3" ] 
then
       echo "Checking root filesystem"
#       initlog is used by RedHat to bring syslog online before
#       the file system is mounted.
#       this is for future support.
#       initlog -c "fsck -T -a $fsckoptions /"
        fsck.ext3 -n $fsckoptions /dev/discs/disc0/part2
        rc=$?
        if [ "$rc" = "0" ]; then
                /sbin/boot_status 0 21 
        elif [ "$rc" = "1" ]; then
		echo "Passed"
                /sbin/boot_status 0 22 
        fi

        # A return of 2 or higher means there were serious problems.
        if [ $rc -gt 1 ]; then
#                failure "$STRING"
                echo
                echo $"*** An error occurred during the file system check."
                echo $"*** The system will reboot"
                /sbin/boot_status 0 246 
                /sbin/init 6
        fi
fi

/sbin/boot_status 0 25 

# Enter root, /proc and devfs into mtab.
mount -v -f /
mount -v -f /proc
[ -e /dev/.devfsd ] && mount -v -f -t devfs devfs /dev
 
/sbin/boot_status 0 30 
# Check filesystems
        echo "Checking filesystems"
#        initlog -c "fsck -T -R -A -a $fsckoptions"
        fsck -R -A -a $fsckoptions
        rc=$?
        if [ "$rc" = "0" ]; then
                /sbin/boot_status 0 31 
        elif [ "$rc" = "1" ]; then
		echo "Passed"
                /sbin/boot_status 0 32 
        fi

        # A return of 2 or higher means there were serious problems.
        if [ $rc -gt 1 ]; then
	     echo "Failure: $rc"
             echo
             echo $"*** An error occurred during the file system check."
		
	     if [ $rc -ne 8 ]; then
                /sbin/boot_status 0 246 
                /sbin/init 6
	     fi
fi

#
#	Call all parts in order.
#
for i in /etc/rc.d/rcS.d/S??*
do
	# Ignore dangling symlinks for now.
	[ ! -f "$i" ] && continue
	case "$i" in
		*.sh)
			# Source shell script for speed.
			(
				trap - INT QUIT TSTP
				set start
				. $i
			)
			;;
		*)
			# No sh extension, so fork subprocess.
			$i start
			;;
	esac
done


