#!/bin/sh

FSCK="/sbin/fsck"
HWCLOCK="/sbin/hwclock"

if [ -x ${HWCLOCK} ]; then
	echo -n "set system date from hardware clock:" 
	${HWCLOCK} -s --noadjfile --localtime
	echo
fi

echo -n "mounting proc filesystem. "
mount -n -t proc none /proc
echo

echo -n "checking root filesystem device and type: "
ROOTFSSTAT=`mount | grep " / " /proc/mounts | tail -n 1`
ROOTDEVICE=`echo $ROOTFSSTAT | cut -d " " -f 1`
if [ "X$ROOTDEVICE" = X ]; then
	echo -n "couldn't determine root file system device. "
else
	echo -n "$ROOTDEVICE "
fi
ROOTFSTYPE=`echo $ROOTFSSTAT | cut -d " " -f 3`
if [ "X$ROOTFSTYPE" = X ]; then
	echo -n "couldn't determine root file system type. "
else
	echo -n "$ROOTFSTYPE "
fi
echo


case $ROOTFSTYPE in
jffs*)
	ROMMODE="true"
	MOUNT="mount -n"
	echo -n "remounting root filesystem read-only. "
	$MOUNT -t $ROOTFSTYPE -oremount,ro $ROOTDEVICE /
	echo
	;;
*)
	if [ "X$ROOTFSTYPE" != X"nfs" -a -x $FSCK ]; then
		FSTAB_ROOT=`grep -v "^#" /etc/fstab | while read d p x ; do [ "X$p" = X"/" ] && echo $d; done`
		if [ "X$FSTAB_ROOT" != "X" ]; then
			ROOTDEVICE=$FSTAB_ROOT
			echo "checking root filesystem consistency."
			$FSCK -Ta /
		fi
	fi
	ROMMODE="false"
	MOUNT="mount"
	echo -n "remounting root filesystem writable. "
	if [ "X$ROOTFSTYPE" != X"nfs" ]; then
		mount -n -t $ROOTFSTYPE -oremount,rw $ROOTDEVICE /
	else
		mount -n -oremount,rw $ROOTDEVICE /
	fi
	echo
	echo -n "updating mtab. "
	>/etc/mtab
	rm -f /etc/mtab~*
	$MOUNT -f -t $ROOTFSTYPE $ROOTDEVICE /
	$MOUNT -f -t proc none /proc
	if [ ! -f /etc/fstab ]; then
		>/etc/fstab
	fi
	echo
	;;
esac

if [ -f /etc/fstab -a -x $FSCK ]; then
        echo "checking filesystems."
	$FSCK -TRAa
fi

if grep usbdevfs /proc/filesystems > /dev/null 2>&1; then
	echo -n "mounting usbdevfs. "
	$MOUNT -t usbdevfs usbdevfs /proc/bus/usb
	echo
fi

if grep devpts /proc/filesystems > /dev/null 2>&1; then
	echo -n "mounting devpts. "
	$MOUNT -t devpts none /dev/pts
	echo
fi

while read device point type opt; do
	if grep " $point " /proc/mounts > /dev/null 2>& 1; then
		continue;
	fi
	case $point in
	/var)
		VARDEV=$device
		VARTYPE=$type
		;;
	/tmp)
		TMPDEV=$device
		TMPTYPE=$type
		;;
	esac
	case $device in
	/dev/ram*)
		echo initializing $device
		case $type in
		minix)
			mkfs.minix $device
		;;
		*)
			mkfs -t $type $device
		;;
		esac
	;;
	esac
done < /etc/fstab


if [ X$VARDEV != X ]; then
	if [ X$TMPDEV != X ]; then
		echo -n "mounting /tmp. "
		$MOUNT -t $TMPTYPE $TMPDEV /tmp
		echo
	fi
	echo -n "mounting /var "
	if [ X$TMPDEV != X -o $ROMMODE = "false" ]; then
		# since writable /tmp is exists, 
		# we can save original containts of /var
		echo -n "picking up original contents. "
		tar -C / -cf /tmp/var.tar var
		$MOUNT -t $VARTYPE $VARDEV /var
		tar -C / -xf /tmp/var.tar
		rm /tmp/var.tar
	else
		# pick up list of directories in /var and re-create them
		echo -n "re-creating subdirectories. "
		var_directories=`find /var -type d`
		$MOUNT -t $VARTYPE $VARDEV /var
		for d in $var_directories; do
			mkdir $d
		done
	fi
	echo
fi

$MOUNT -a

if [ -f /etc/nologin ]; then
	rm -f /etc/nologin
fi

swapon -a

/sbin/ifconfig lo 127.0.0.1

if [ -d /etc/rc.d ]; then
	for i in /etc/rc.d/*; do
		if [ -x "$i" ]; then
                    $i start
		fi
	done
fi 

if [ -x /etc/rc.local ]; then
	. /etc/rc.local
fi

exit 0

