#! /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.
if [ ! -f /etc/fastboot ]
then
	# We FORCE file system checks.
	check_forced="-f"
	echo "No fastboot, file system checks forced."
fi

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

# Check root file system - other fs's version (without clean bit)
if [ $check_forced ]
then
	fsck -TM -tnoext2 -a -v /
	check_status root
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/utmp
> /etc/mtab
mount -f /
[ $proc_mounted ] && mount -f /proc

# Check all other local file systems.
# NOTE: root fs is mounted rw now AND is checked again. This should
# give us no problems since it's been checked before.
echo "Checking local file systems...."

# First the syntax for ext2 file systems.
fsck -AR -text2 -a -v $check_forced

# And this is for all other file systems.
if [ $check_forced ]
then
	fsck -TRA -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 ]
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 -d ${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`"
  
#
# Save and clear boot information, from kernel buffer
#
[ -z "$LASTBOOT" ] && LASTBOOT="/var/adm/lastboot"
echo -n "Saving boot information to : $LASTBOOT"
  
# add, date to front and dump messages
echo "-- `date` --"   >$LASTBOOT	
dmesg -c | sed 's/^<.*>//' >>$LASTBOOT

# for syslogd, that it will not record these in firstplace
/sbin/klogd -o -s -f /dev/null		
echo ", done."
# All done.
