#! /bin/sh
#
# boot		boot-time system configuration.
#
# Version:	@(#)boot  2.2  1996-05-22  miquels@cistron.nl
#
#		Copyright (C) 1994, 1995, 1996 Debian Association, Inc.
#		Written by Ian Murdock <imurdock@gnu.ai.mit.edu>
#		       and Miquel van Smoorenburg <miquels@cistron.nl>
#

#
# This script desperately needs to parse a config file
# (like /etc/boot.conf) instead of having to edit it directly!
#

# Time files in /tmp are kept.
TMPTIME=0
# Set to 1 if you want sulogin to be spawned on bootup
SULOGIN=0

PATH="/sbin:/bin:/usr/sbin:/usr/bin"
umask 022

if [ -x /sbin/unconfigured.sh ]
then
  /sbin/unconfigured.sh
fi

#
# Uncomment this stuff if you want a sulogin to be spawned from
# this script *before anything else* with a timeout, like on SCO.
#
[ "$SULOGIN" = 1 ] && sulogin -t 30 /dev/console

echo
echo "Running /etc/init.d/boot..."
echo

# Enable multiple devices.  We do this here because we may be
# swapping to them, or something.  If /etc/mdtab doesn't exist
# or is empty we don't bother; likewise if mdadd isn't installed.
if [ -s /etc/mdtab -a -f /sbin/mdadd ]
then
  mdadd -ar
fi

# Activate the swap device(s) in /etc/fstab. This needs to be done
# before fsck, since fsck can be quite memory-hungry.
echo "Activating swap..."
swapon -a 2>/dev/null

# Ensure that bdflush (update) is running before any major I/O is
# performed (the following is a good example of such activity :).
update

# Check the integrity of all file systems (if not a fastboot).
if [ -f /fastboot ]
then
  echo "*** WARNING!  File systems are NOT being checked!"
  echo
else
  # Ensure that root is quiescent and read-only before fsck'ing.
  mount -n -o remount,ro /
  if [ $? = 0 ]
  then
    echo "Checking file systems..."
    fsck -A -a
    # If there was a failure, drop into single-user mode.
    #
    # NOTE: "failure" is defined as exiting with a return code of
    # 2 or larger.  A return code of 1 indicates that file system
    # errors were corrected but that the boot may proceed.
    if [ $? -gt 1 ]
    then
      # Surprise! Re-directing from a HERE document (as in
      # "cat << EOF") won't work, because the root is read-only.
      echo
      echo "fsck failed.  Please repair manually and reboot.  Please note"
      echo "that the root file system is currently mounted read-only.  To"
      echo "remount it read-write:"
      echo
      echo "   bash# mount -n -o remount,rw /"
      echo
      echo "CONTROL-D will reboot the system."
      echo
      # Start a single user shell on the console
      /sbin/sulogin /dev/console
      sync
      reboot
    fi
    echo
  else
    echo "*** ERROR!  Cannot fsck because root is not read-only!"
    echo
  fi
fi

# Remount the root file system in read-write mode,
# then cleanup and ensure /etc/mtab is up-to-date.
mount -n -o remount,rw /
rm -f /etc/mtab /etc/mtab~ /etc/nologin /fastboot
mount -o remount,rw /

# Load the appropriate modules.
if [ -x /etc/init.d/modules ]
then
  /etc/init.d/modules
fi

# Mount local file systems in /etc/fstab.
echo "Mounting local file systems..."
mount -avt nonfs

# Execute swapon command again, in case we want to swap to
# a file on a now mounted filesystem.
swapon -a 2>/dev/null

# Setup the network interfaces. Note that /var/run and /var/lock
# are cleaned up after this, so don't put anything in the "network"
# script that leave a pidfile or a lockfile.
if [ -x /etc/init.d/network ]
then
  /etc/init.d/network
fi

# Set hostname.
hostname --file /etc/hostname

# Now that TCP/IP is configured, mount the NFS file systems in /etc/fstab.
echo "Mounting remote file systems..."
mount -a -t nfs

# Set GMT="-u" if your system clock is set to GMT, and GMT=""
# if not.
GMT="-u"
# Set and adjust the CMOS clock.
clock -s $GMT
if [ ! -f /etc/adjtime ]
then
  echo "0.0 0 0.0" > /etc/adjtime
fi
clock -a $GMT

# Now that /usr/lib/zoneinfo should be available, announce the local time.
echo
echo "Local time: `date`"
echo

# Wipe /tmp (and don't erase `lost+found', `quota.user' or `quota.group')!
# Note that files _in_ lost+found _are_ deleted.
echo -n "Cleaning up /tmp, /var/run and /var/lock... "
( cd /tmp && \
  find . -xdev \
  ! -ctime -$TMPTIME \
  ! -name . \
  ! \( -name lost+found -uid 0 \) \
  ! \( -name quota.user -uid 0 \) \
  ! \( -name quota.group -uid 0 \) \
    -depth -exec rm -rf -- {} \; )
# Clean up any stale locks.
( cd /var/lock && find . -type f -exec rm -f -- {} \; )
# Clean up /var/run and create /var/run/utmp so that we can login.
( cd /var/run && find . ! -type d -exec rm -f -- {} \; )
: > /var/run/utmp
echo "done."

# Run the package-specific boot scripts in /etc/rc.boot.
run-parts /etc/rc.boot

# Set pseudo-terminal access permissions.
chmod 666 /dev/tty[pqrs]*
chown root.tty /dev/tty[pqrstuvwxyzabcde][0123456789abcdef]

# If /var/log/wtmp does not exist, create it.
# if [ ! -f /var/log/wtmp ]
# then
#   : > /var/log/wtmp
# fi

# Update /etc/motd.
uname -a > /tmp/motd
sed 1d /etc/motd >> /tmp/motd
mv /tmp/motd /etc/motd

if [ -x /sbin/setup.sh ]
then
  /sbin/setup.sh
fi
