#!/bin/sh
#
# (C) Copyright 1993, 1994 Mike Jagdis (jaggy@purplet.demon.co.uk)
#
#
# 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
ctrlaltdel=`pathof ctrlaltdel`
hostname=`pathof hostname`
domainname=`pathof domainname`
mount=`pathof mount`
rdev=`pathof rdev`
reboot=`pathof reboot`
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 $use_escapes "Starting swap partitions:\c" > /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
	echo $use_escapes " $1\c"

	# Only do an mkswap if the block count is not "-", "*" or "0"
	if [ "$2" != "-" -a "$2" != "*" -a "$2" != "0" ]; then
		$mkswap $3 $1 $2
	fi
	$swapon $1
done
echo

# 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 [ -n "$rootdev" ]; then
	# If no /fastboot file exists then we fsck the root fs first.
	if [ "$checkfs" != "YES" -a "$checkfs" != "yes" ] && [ -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
		(dofscheck $rootdev $roottype > $CTTY 2>&1 < $CTTY)
	fi
fi

# If we get this far it is safe for us to mount the root file system
# read/write - if we know what it is...
if [ -n "$rootdev" ]; then
	$mount -n -o remount $rootdev /
fi

# remove /etc/mtab* and recreate it
rm -f /etc/mtab~
if [ -n "$rootspec" ]; then
	echo "$rootspec" > /etc/mtab
else
	if [ -n "$rootdev" ]; then
		# Lie. libc 4.3.3 and later *requires* more than we know.
		# For the root fs this shouldn't be important since we don't
		# use the extra information except to mount (and unmount?)
		# the fs. This *is* the root fs...
		# (Could be dodgy for remounting? Do we need the type then?)
		echo "$rootdev / ext2 defaults 0 0" > /etc/mtab
	else
		: 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
	fi
fi
chmod 644 /etc/mtab

# Get the proc filesystem up early.
if [ ! -d /proc ]; then
	rm -f /proc
	mkdir /proc
	chmod 555 /proc
fi
$mount /proc 2>/dev/null || $mount -t proc /proc /proc

# Set the system's hostname and domainname. Watch out! Hostname may be
# an FQDN or we may have a separate domain name. A domain name in the
# hostname is always favoured over a separate domain name however we
# always split them and use sethostname() and setdomainname() on each
# part. Software should be adapted where necessary to read both parts.
# If it doesn't do that it's broken...

# This is where the Purple Distribution puts it.
if [ -f "/etc/default/hostname" ]; then
	HOSTNAME=`cat /etc/default/hostname 2> /dev/null`
fi

# I've seen this. SLS? MCC? Tamu?
if [ -z "$HOSTNAME" -a -f "/etc/default/host" ]; then
	HOSTNAME=`cat /etc/default/host 2> /dev/null`
fi

# Possible?
if [ -z "$HOSTNAME" -a -f "/etc/hostname" ]; then
	HOSTNAME=`cat /etc/hostname 2> /dev/null`
fi

# This seems to be use by the Net-2 install script.
if [ -z "$HOSTNAME" -a -f "/etc/HOSTNAME" ]; then
	HOSTNAME=`cat /etc/HOSTNAME 2> /dev/null`
fi

# Fer gawd's sake...
if [ -z "$HOSTNAME" -a -f "/etc/systemid" ]; then
	HOSTNAME=`cat /etc/systemid 2> /dev/null`
fi

# Split into hostname and domainname parts
DOMAINNAME=`echo $HOSTNAME | awk '{ i = index($0, "."); if (i) print substr($0, i+1) }'`
HOSTNAME=`  echo $HOSTNAME | awk '{ i = index($0, "."); if (i) print substr($0, 1, i-1); else print $0 }'`

# Right, now have we got a domainname yet?
# This is where the Purple Distribution puts it.
if [ -z "$DOMAINNAME" -a -f "/etc/default/domainname" ]; then
	DOMAINNAME=`cat /etc/default/domainname 2> /dev/null`
fi

# I've seen this. SLS? MCC? Tamu?
if [ -z "$DOMAINNAME" -a -f "/etc/default/domain" ]; then
	DOMAINNAME=`cat /etc/default/domain 2> /dev/null`
fi

# Possible?
if [ -z "$DOMAINNAME" -a -f "/etc/domainname" ]; then
	DOMAINNAME=`cat /etc/domainname 2> /dev/null`
fi

# This seems to be use by the Net-2 install script.
if [ -z "$DOMAINNAME" -a -f "/etc/DOMAINNAME" ]; then
	DOMAINNAME=`cat /etc/DOMAINNAME 2> /dev/null`
fi

# Or is it this...
if [ -z "$DOMAINNAME" -a -f "/etc/DOMAIN" ]; then
	DOMAINNAME=`cat /etc/DOMAIN 2> /dev/null`
fi

# If we haven't got any way to set the domain name we'll fold it back
# in to the hostname. Not good, but it is reasonable to expect this
# to work...
if [ -z "$domainname" -a -n "$DOMAINNAME" -a -n "$HOSTNAME" ]; then
	HOSTNAME=$HOSTNAME.$DOMAINNAME
	DOMAINNAME=
fi

if [ -n "$hostname" ]; then
	if [ -n "$HOSTNAME" ]; then
		$hostname $HOSTNAME 2>/dev/null || $hostname -S $HOSTNAME
		if [ -n "$domainname" -a -n "$DOMAINNAME" ]; then
			$domainname $DOMAINNAME
		fi
	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

# Run any other configuration scripts we may have.
if [ -d /etc/bcheckrc.d ]; then
	for i in /etc/bcheckrc.d/*
	do
		if [ -s $i ]; then
			/bin/sh $i
		fi
	done
fi
