#!/bin/sh
#
# /etc/rc.d/rc.sysinit - first init script
#

# Set a basic path
PATH=/sbin:/usr/sbin:/bin:/usr/bin

# Load in bootup config if available
if [ -f /etc/rc.d/rc.conf ]; then
  . /etc/rc.d/rc.conf
fi

# Load pseudo-filesystems
/bin/mount -n -t proc procfs /proc
/bin/mount -n -t sysfs sysfs /sys

# Start the udev deamon
/sbin/start_udev

# Activate all swap devices
/sbin/swapon -a

# Check local filesystems
/sbin/fsck -A -T -C -a
if [ $? -gt 1 ] ; then
	echo "Error in file system check; starting emergency shell..."
	PS1="(Emergency shell) \#"; export PS1
	/sbin/sulogin -p; echo "Rebooting system..."
	/bin/umount -a -r; /bin/mount -n -o remount,ro /
	/sbin/reboot -f; exit 0
fi

# Mount the root filesystem read-write
echo "Mounting local file systems:"
/bin/mount -v -n -o remount,rw /
/bin/umount /sys /proc
/bin/rm -f /etc/mtab*

# Mount local filesystems
#echo "Mounting local filesystems."
/bin/mount -a -v -O no_netdev

# Remove old pid-files
/bin/rm -rf /forcefsck /fastboot /etc/nologin /etc/shutdownpid
(cd /var/run && /usr/bin/find . -name "*.pid" -delete)
(cd /var/lock && /usr/bin/find . ! -type d -delete)
(cd /tmp && /usr/bin/find . ! -name . -delete)
/bin/mkdir -m 1777 /tmp/.ICE-unix

# Set kernel variables
#echo "Setting kernel variables."
/sbin/sysctl -p > /dev/null

# Clear the current-users record
/bin/echo >/var/run/utmp

# Make sure paths have correct permissions
/bin/chmod 755 /
/bin/chmod 1777 /tmp /var/tmp

# Load keymap if specificed
if [ ! -z "$KEYMAP" ]; then
  if [ "$UNICODE" = "no" ]; then
    /bin/loadkeys $KEYMAP
   else
    /usr/bin/kbd_mode -u
    /bin/loadkeys --unicode $KEYMAP
    /usr/bin/unicode_start
  fi
fi

# Set numlock mode
if [ ! -z "$NUMLOCK" ]; then
  if [ "$NUMLOCK" != "no" ]; then
    for tty in /dev/tty[1-6]; do
      setleds -D +num < $tty
    done
  fi
fi

# Set the system clock
echo -n "Setting time: "
if [ "$TIMEZONE" ]; then
        /bin/ln -sf /usr/share/zoneinfo/$TIMEZONE /etc/localtime
fi
if [ -x /sbin/hwclock ]; then
	if [ "$HWCLOCK" = "UTC" ]; then
		/sbin/hwclock --utc --hctosys
	else
		/sbin/hwclock --localtime --hctosys
	fi
	echo "`date`"
fi

# Initialize /dev/urandom
if [ -f /var/lib/urandom/seed ]; then
#	echo "Initializing /dev/urandom."
	cat /var/lib/urandom/seed >/dev/urandom
fi

# Resolv module dependencies
#echo "Resolving module dependencies (`uname -r`)."
/sbin/depmod -A

# Run a script on first boot
if [ -x /.newinstall ]; then
	/.newinstall >/tmp/newinstall.log 2>&1
fi

# Load kernel modules
#echo "Loading `uname -r` kernel modules."
for module in $MODULES; do
	/sbin/modprobe $module
done

# Save boot messages
/bin/dmesg > /var/log/boot

# EOF
