#!/bin/sh
#
# $Id: bcheckrc,v 4.2 1993/03/23 16:43:19 mike Exp $
#
# System integrity checks.
# This is run before filesystems (other than root) are mounted and
# before any syncs are done either by update or by init.

# Load the library functions and try and find various programs.
. /etc/init.d/LIB
clock=`pathof clock`
ctrlaltdel=`pathof ctrlaltdel`
hostname=`pathof hostname`
rdev=`pathof rdev`
mkswap=`pathof mkswap`
swapon=`pathof swapon`

# Let the user know if we think we are auto booting or not
if [ "$AUTOBOOT" = "YES" ]; then
	echo "NOTICE: System is auto booting"
fi

# If we have swap partitions get them up as early as possible.
echo "Starting swap partitions..." > /dev/console
awk '/^[ \t]*\/dev/' /etc/default/swap | while read i
do
	# If we haven't found mkswap we can't do this.
	if [ -z "$mkswap" ]; then
		echo "WARNING: mkswap not found. Swap partitions are *not* initialised."
		break
	fi
	set $i
	$mkswap $3 $1 $2
	echo "Adding swap partition: $1"
	$swapon $1
done

# If we can see clock out there use it to set the date/time from
# the hardware clock.
if [ -n "$clock" ]; then
	HWCLOCK=`awk '/^[ \t]*HWCLOCK/ { print $2 }' /etc/default/clock`
	if [ "$HWCLOCK" = "GMT" -o "$HWCLOCK" = "UCT" ]; then
		$clock -u -s
	else
		$clock -s
	fi
fi

# Set the system's hostname
hostname=`pathof hostname`
if [ -n "$hostname" ]; then
	myname=`cat /etc/default/hostname 2> /dev/null`
	if [ -n "$myname" ]; then
		$hostname $myname
	else
		echo "WARNING: /etc/default/hostname is missing or empty."
		echo "WARNING: The system name will *not* be set."
	fi
else
	echo "WARNING: can't find hostname. The system name will *not* be set." > /dev/console
fi

# Look in fstab to see what the root filesystem device is. If it isn't
# in fstab we're stuffed.
rootspec=`awk '/^\/dev/ && $2 == "/"' /etc/fstab 2> /dev/null`
set $rootspec
rootdev=$1
roottype=$3
if [ "$rootdev" = "" ]; then
	if [ -n "$rdev" ]; then
		rootdev=`$rdev`
	fi
fi
if [ "$rootdev" = "" ]; then
	: no root in fstab and no rdev
	echo "WARNING: Root filesystem not in /etc/fstab and no rdev." > /dev/console
	echo "WARNING: Output of df will not list the root filesystem!" > /dev/console
	> /etc/mtab
	chmod 644 /etc/mtab
else
	# If no /fastboot file exists then we fsck the root fs first.
	if [ -f /fastboot -o -f /etc/fastboot -o -n "$FASTBOOT" ]; then
		echo "FAST BOOT: root filesystem *not* checked" > /dev/console
	else
		echo "Checking root filesystem on $rootdev..." > /dev/console
		checkfs $rootdev $roottype > /dev/console
	fi

	# remove /etc/mtab* and recreate it
	if [ "$rootspec" = "" ]; then
		echo "$rootdev /" > /etc/mtab
	else
		echo "$rootspec" > /etc/mtab
	fi
	chmod 644 /etc/mtab
fi

# If we have ctrlaltdel out there (from poe's admutils) we'll try and
# use it to tell the kernel to signal init rather than rebooting
# directly. This should be handled by any up to date init.
if [ -n "$ctrlaltdel" ]; then
	if $ctrlaltdel soft; then
		echo "Ctrl-Alt-Del will reboot gracefully"
	else
		echo "WARNING: ctrlaltdel failed? Ctrl-Alt-Del will *NOT* sync before rebooting"
	fi
else
	echo "WARNING: ctrlaltdel not found."
	echo "WARNING: Ctrl-Alt-Del will *NOT* sync before rebooting."
fi
