#!/bin/sh
#/etc/rc
#
# These commands are executed at boot time by init(8).
# User customization should go in /etc/init.d/local.

PATH=/bin:/usr/bin:/sbin
. /etc/system_config

check_mount() {
   while read device directory rest
   do
      if [ "#" = "${device%%'[^#]*'}" ]; then
         continue
      fi
      if [ "$directory" = "$1" ]; then
         /sbin/fsck -a $device < /dev/tty
         if [ $? -gt 1 ]; then
            return $?
         fi
         echo Mounting $directory
         /sbin/mount $directory < /dev/tty
         break
      fi
   done < /etc/fstab
   return 0
}

echo ++++++++++++++++++++++++++++++++++++++++++++++++

# Check the integrity of root filesystem
echo Checking root
/sbin/fsck -a $ROOTDEV

# If there was a failure, drop into single-user mode.
if [ $? -gt 1 ] ; then
	echo fsck failed for /.  Please reboot.
	sh
fi

# Remount the root filesystem in read-write mode
echo Remounting root read-write
/sbin/mount -n -o remount $ROOTDEV /

# remove /etc/mtab* so that mount will create it with a root entry
/bin/rm -f /etc/mtab* /etc/nologin 

# create root mtab entry (normally would let mount do this,
# but the bootutil mount has a bug on remount)
echo "$ROOTDEV	/	ext2	defaults" >/etc/mtab

# Set machine name
echo Hostname: $MYNAME
/bin/hostname $MYNAME

# Mount /proc
echo Mounting /proc
/sbin/mount /proc

# Configure network as we may need it to mount /usr
echo Configuring network
LOCALHOST=127.0.0.1

if [ $DONET = "yes" ]
then
  # Set up the Ethernet connection(s).
  /sbin/ifconfig eth0 ${MYADDR} netmask ${NETMASK} broadcast ${BROADCAST}
  /sbin/route add -net ${MYNET} netmask ${NETMASK}
  /sbin/route add default gw ${GATEWAY} 
fi

# Attach the loopback device.
/sbin/ifconfig lo ${LOCALHOST}
/sbin/route add ${LOCALHOST}

# show the routes
/sbin/route -n

# Network configuring all done.

# Mount /usr and /var
check_mount /usr
if [ $? -gt 1 ]; then
	echo fsck failed for /usr.  Please run \"fsck -r\" by hand.
	sh
fi
check_mount /var
if [ $? -gt 1 ]; then
	echo fsck failed for /var.  Please run \"fsck -r\" by hand.
	sh
fi

# update ld.so
echo running ldconfig
/sbin/ldconfig

# Enable swapping
echo Enabling swap
/sbin/swapon -a

# start the update(8) sync demon
echo Starting update
/sbin/update &

# remove stale locks (must be done after mount -a!)
/bin/rm -f /usr/spool/locks/* /usr/spool/uucp/LCK..* /tmp/.X*lock

# clean utmp
cat /dev/null >/etc/utmp

exit 0
