# System startup script run by init (with /bin/sh) on multi-user boot, 
# or after # the single-user shell exits.  The console is the controlling tty,
# stdin, stdout, and stderr.

# Ignore interrupts during rc, but not for the programs we run.
trap : INT QUIT

export HOME=/
export PATH=/bin

intr ()
{
  echo 'Reboot interrupted.'
  exit 1
}

if [ -r /fastboot ];
  echo 'Fast boot.  Skipping filesystem boot programs.'
else
  echo 'Automatic reboot.'

  # Run the filesystem boot programs (fsck et al).
  fsboot

  # These are the error codes 4.4 fsck returns (or, at least,
  # 4.4 rc checks for it returning).
  case $? in
  0)
    # Winning.
    ;;
  2)
    # User hit QUIT char.  We should return to single-user.
    exit 1
    ;;
  4)
    # Want to reboot and try again.
    reboot
    echo "reboot failed... help!"
    exit 1
    ;;
  8|1)				# BSD does not use $?==1.
    echo 'Filesystem boot programs failed.  Help!'
    exit 1
    ;;
  12)
    # Interrupted.
    intr
    ;;
  130)
    # Interrupt before the handler was installed.
    exit 1
    ;;
  *)
    echo "Unknown error $? from fsboot.  Help!"
    exit 1
    ;;
  esac
fi

# Return to single-user on interrupt.  Since we are still ignoring QUIT,
# the user can hit C-\ to abort a particular program without aborting rc.
trap intr INT

# Clean up left-over files.
rm -f /fastboot
rm -f /etc/nologin
rm -f /var/spool/uucp/LCK.*
rm -f /var/spool/uucp/STST/*
# XXX Will GNU have /var/run/utmp?
(cd /var/run && { rm -rf -- *; cp /dev/null utmp; chmod 644 utmp; })

echo starting system logger
rm -f /dev/log
syslogd

# Check for the password temporary/lock file.
if [ -f /etc/ptmp ];  then
  logger -s -p auth.err \
  'Password file may be incorrect -- /etc/ptmp exists.'
fi

echo -n standard daemons:
echo -n ' update';		update
echo -n ' cron';		cron
echo '.'

if [ -r /etc/rc.local ]; then
  echo Local boot-time procedure:
  sh /etc/rc.local "$@"
  status=$?
else
  status=0
fi

echo 'Reboot finished.'
date

exit $status
