#! /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.75	03-Aug-1995 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 "Rebooting..."
		reboot
	fi
	echo "System boot-up is continuing..."
  fi
}

# Set the path.
PATH=/sbin:/usr/sbin:/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.
clean=NO
[ ! -f /etc/fastboot ] && clean=YES

# Find out type of root file system to see
# if it has a "clean bit".
has_cleanbit=""
rootfs=`grep '^/.*[ 	]\+/[ 	]\+' /etc/fstab`
if [ "$rootfs" != "" ]
then
  set $rootfs
  case "$3" in
	ext2|minix|xiafs)
		has_cleanbit=1
		;;
  esac
fi

# Check root file system.
echo "Checking root file system...."
mount -n -o remount,ro  /
if [ "$has_cleanbit" ]
then
  fsck -V -a -v /
  check_status root
else
  if [ $clean = NO ]
  then
	fsck -V -a -v /
	check_status root
  fi
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 /
  # mount /proc now, may be needed for modules.
  mount /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

# 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).
if [ ! -e /usr/lib/zoneinfo/localtime ]
then
  TZ=:/etc/localtime
  export TZ
fi

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
clock -s

# Initialize serial ports.
if [ -f /etc/rc.d/rc.serial ]
then
	# Source it.
	. /etc/rc.d/rc.serial
fi
if [ -f /etc/rc.d/rc.rocket ]
then
	# Don't source, it does an exit.
	/etc/rc.d/rc.rocket
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.
