#! /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/rc.d/bcheckrc 1.70	19-Oct-1994 MvS

# First some supporting shell functions.

check_status()
{
  if [ $? -gt 1 ]
  then
	# Exit status of greater 1 means something went wrong.
	# Oh-oh. Repair manually, and reboot.
	echo "*** File system check failed, please repair by hand"
	if [ $1 = root ]
	then
		echo "*** Dropping you in a shell. System is rebooted after"
		echo "*** repairing the file system and leaving the shell"
		echo "*** Remember the root file system is still readonly!"
	fi

	echo
	echo "=== If you got an error that looked like \`/: is a directory'"
	echo "=== then possibly your /etc/mtab file is corrupt."
	echo "=== Remove /etc/mtab* and use mount -f to correct this."
	echo
	# Set a suitable prompt.
	PS1="(Repair filesystem) #"; export PS1

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

	# And reboot the system when done.
	if [ $1 = root ]
	then
		echo "Automatic reboot in progress."
		reboot
	fi
	echo "System boot-up is continuing..."
  fi
}

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

# See if this is a virtual console.
if [ ! /dev/systty -ef /dev/console ]
then
	# No, so cat kernel messages to the console.
	# This can be taken out when the kernel supports
	# serial line consoles.
	mount -n /proc
	cat -u /proc/kmsg > /dev/console &
	sleep 2
	proc_mounted=noproc
fi

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

# See if filesystems need checking.
clean=NO
[ ! -f /etc/fastboot ] && clean=YES

echo "Checking root file system...."
# Check root file system - ext2 version (has clean bit)
mount -n -o remount,ro  /
fsck -VM -text2 -a -v /
check_status root

# Check root file system - other fs's version (without clean bit)
if [ $clean = NO ]
then
	fsck -VTM -tnoext2 -a -v /
	check_status root
fi

# Remount the root fileystem read-write.
echo -n "Attempting to remount root file system read-write.. "
mount -n -o remount,rw /
if [ $? != 0 ]
then
  echo "Failed."
  echo "Ignore this message if you are running from a CD root file system"
  rootro=YES
else
  # Be sure /etc/mtab is up to date. Em, how does this work from
  # a CD? After /var is mounted?
  echo "OK."
  rootro=NO
  rm -f /etc/mtab~ /etc/fastboot
  > /etc/mtab
  mount -f /
  [ $proc_mounted ] && mount -f /proc
fi

# Check all other local file systems.
echo "Checking local file systems...."

# First the syntax for ext2 file systems.
fsck -VAR -text2 -a -v

# And this is for all other file systems.
if [ $clean = NO ]
then
	fsck -VTRA -tnoext2 -a -v
fi

# 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 -avt nonfs $proc_mounted

# Right, now turn on swap in case we swap to files.
echo "Turning on swap (for swapping to files)"
swapon -a >/dev/null 2>&1

# Set the system clock, and adjust the CMOS clock (which is in local time).
echo "Adjusting CMOS clock"
if [ ! -f /etc/adjtime ] && [ $rootro = NO ]
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

# First step in setting up the net subsystem.
if [ -f /etc/rc.d/rc.net ]
then
	. /etc/rc.d/rc.net setup
elif [ -f /etc/rc.d/rc.inet1 ]
then
	. /etc/rc.d/rc.inet1
fi

# All done.
