# /etc/rc - System startup script run by init before going multiuser.

opts=$1		# Boot options:	-s: single user
		#		-a: ask for /usr
		#		-f: force a full fsck
		#		-n: don't check

umask 022
TERM=minix
PATH=/sbin:/usr/sbin:/usr/local/sbin:/bin:/usr/bin:/usr/local/bin
export TERM PATH

# Load national keyboard map.
test -f /etc/keymap && keymap -l /etc/keymap

# Set timezone and time.
. /etc/timeinfo
if readclock; then date; else intr date -q; fi

# Initialize virtual disk devices.
test -f /etc/vdisk.conf && vdisk_ctrl -c /etc/vdisk.conf

case $opts in
*n*)	: Skip check.
	;;
*)	echo;echo "Checking File Systems."
	case $opts in
	*f*)	intr fsck -pf
		;;
	*)	intr fsck -p
	esac
esac

case $? in
0)	# All clean or fixed.
	;;
2)	# Quit typed.
	why="Quit typed"
	opts=s
	;;
4)	# Minor errors have been corrected, but on a mounted FS.
	echo "Mounted device fixed, rebooting"
	reboot -f
	;;
*)	# Serious errors, need manual intervention.
	echo "Please run fsck manually, reboot if / is repaired."
	echo "Fsck failed - Single user."
	intr sh
esac

# The first mount call makes "/" read-write, clears /etc/mtab, and checks
# if the root device is the same as mentioned in /etc/fstab.

echo; mount -vo remount,fstab /

case $? in
0)	# / is now as it should be, mount all other devices.
	case $opts in
	*a*)	;;
	*)	mount -av || opts=s why="Mount failed"
	esac
	;;
2)	# Root device is not as expected, ask for /usr (if necessary.)
	test -d /usr/bin || opts=a$opts
	;;
*)	# Remount of / failed.
	opts=s why="Mount failed"
esac

case $opts in
*a*)	intr sh -c '
		echo -n "Finish the name of device to mount as /usr: /dev/"
		read usr
		mount /dev/$usr /usr' \
	|| opts=s$opts why="Mount failed"
esac

case $opts in
*s*)	echo "${why+"$why - "}Single user."
	intr sh
esac

# Things should be alright now.
echo;echo "Multiuser startup in progress."

read rootdev rootdir rest </etc/mtab
case $rootdev:$rootdir in
/dev/ram:/)
	# Remove boot-only things to make space.
	rm -fr /boot /minix
esac

# Further initialization.
test -f /usr/etc/rc && sh /usr/etc/rc
if [ -f /usr/lib/packages ]
then
	IFS=/
	while read package rest <&9
	do
		rc="/opt/$package/etc/rc"
		test -f "$rc" && sh "$rc" start 9<&-
	done 9</usr/lib/packages
fi
test -f /usr/local/etc/rc && sh /usr/local/etc/rc
test -f /var/etc/rc && sh /var/etc/rc
exit 0
