#! /bin/sh
#
# bcheckrc	This script is run when the system comes up for
#		the first time. It takes care of checking file
#		systems, setting the system clock etc.
#
# Version:	@(#) /etc/bcheckrc 1.50 1994-01-18
#
# Author:	Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
#

  # Set the path.
  PATH=/sbin:/etc:/bin:/usr/bin

  # Get swap up as fast as possible.
  echo "Turning on swap (for swapping to partitions)"
  swapon -a

  # See if filesystems need checking.
  if [ ! -f /etc/fastboot ]
  then
	echo "No fastboot, checking all file systems"
	fsck -AV -a
	# Exit status of 4 or greater means something went wrong.
	if [ $? -gt 3 ]
	then
		# Oh-oh. Repair manually, and reboot.
		echo "*** File system check failed, please repair by hand"
		echo "*** Dropping you in a shell. Please reboot after"
		echo "*** repairing the file system."
		echo "*** Remember the root file system can still be readonly!"

		# Set a suitable prompt.
		PS1="(Repair filesystem) #"; export PS1

		# Now try to execute a shell, in preferred order.
		if [ -x /etc/su-shell ]; then /etc/sushell
		elif [ -x /sbin/sulogin ]; then /sbin/sulogin
		else /bin/sh; fi

		# And reboot the system when done.
		echo "Automatic reboot in progress."
		reboot
	fi
  fi

  # Remount the root fileystem read-write.
  echo "Re-mounting root file system read-write"
  mount -n -o remount,rw /

  # Be sure /etc/mtab and /etc/utmp are up to date.
  rm -f /etc/mtab~ /etc/fastboot /etc/nologin
  > /etc/utmp
  > /etc/mtab
  mount -f /

  # Mount all other file systems. This should be done only in
  # multi user mode, but we need /usr/lib/zoneinfo for the next
  # action: setting the date from CMOS clock.
  echo "Mounting local file systems"
  mount -a -t nonfs

  # Right, now turn on swap in case we swap to files.
  echo "Turning on swap (for swapping to files)"
  swapon -a 2>&1 | grep -v "busy"

  # Set the system clock, and adjust the CMOS clock (which is in local time).
  echo "Adjusting CMOS clock"
  if [ ! -f /etc/adjtime ]
  then
	echo "0.0 0 0.0" > /etc/adjtime
	chmod 644 /etc/adjtime
	chown bin.bin /etc/adjtime
  fi
  clock -a

  # Initialize serial ports.
  if [ -f /etc/rc.d/rc.serial ]
  then
	. /etc/rc.d/rc.serial
  fi

  # Set the hostname.
  if [ -f /etc/HOSTNAME ]
  then
	# Find out if there's a full hostname here.
	full=`cat /etc/HOSTNAME`
	if [ "${full#*.}" != "" ]
	then
		# Yes, set both host and domainname from it (new style).
  		hostname ${full%%.*}
  		domainname ${full#*.}
	else
		# No, so stuff whole FQDN into hostname (old style).
  		hostname -f "$full"
	fi
  else
	hostname linux
	domainname nodomain.org
  fi
  echo "The name of this system is `hostname`"

  # All done.
