#!/bin/bash
trap ":" INT QUIT TSTP	
source /etc/init.d/functions.sh
#no /usr yet, very likely
export PATH=/sbin:/bin:/usr/sbin:/usr/bin
umask 022

try() {
	eval $*
	if [ $? -ne 0 ]
	then
		echo -e "$ENDCOL$NORMAL[$BAD oops $NORMAL]"
		echo 
		echo '!!! '"ERROR: the $1 command did not complete successfully."
		echo '!!! '"(\" ${*} \")"
		echo '!!! '"Since this is a critical task, startup cannot continue."
		echo
		/sbin/sulogin $CONSOLE
		reboot -f
	fi
}

mountshm() {
	ebegin "Mounting tmpfs at /dev/shm"
	try mount -n -t tmpfs tmpfs /dev/shm -o rw,mode=0644,size=4m
	eend
}

if [ $$ -ne 1 ]
then
	exec /sbin/init.system $*
fi


echo
echo -e "${GOOD}Gentoo Linux${GENTOO_VERS}; \e[34;01mhttp://www.gentoo.org${NORMAL}\n Copyright 2001 Gentoo Technologies, Inc.; Distributed under the GPL"
echo
	
ebegin "Mounting /proc"
mount -n /proc
eend $?
devfs="yes"
for copt in `cat /proc/cmdline`
do
	if [ "${copt%=*}" = "gentoo" ]
	then
		parms=${copt##*=}
		#parse gentoo option
		if [ "${parms/nodevfs//}" != "${parms}" ]
		then
			devfs="no"
		fi
	fi
done

#with the new way, /dev can be mounted by the kernel ...
wrongmount=0

if [ "$devfs" = "yes" ] && [ ! -e /dev/.devfsd ]
then
	ebegin "Mounting devfs at /dev"; try mount -n -t devfs none /dev; eend
fi

#mount shm and do dep scan	
mountshm 

#in theory /dev/shm/.init should not exist if the system had not
#executed the boot runlevel.  if tmpfs support is not enabled in the
#kernel, things will get pretty broken anyhow, so we wont worry about
#this for now. (we need to check for this before running depscan.sh...)
[ ! -d /dev/shm/.init.d ] && export BOOT="yes"

/etc/init.d/depscan.sh

if [ "$devfs" = "yes" ] && [ -e /dev/.devfsd ]
then
	ebegin "Starting devfsd"; /sbin/devfsd /dev >/dev/null 2>&1; eend $?
fi

#swap needs to be activated *after* devfs has been mounted and *after*
#devfsd has been started, so that the fstab can be properly parsed

ebegin "Activating swap"
swapon -a
eend

#Now that swap has been activated, we can calculate a reasonable size limit
#for /dev/shm so that it doesn't exhaust VM if filled.

#set the console loglevel to 1, as we want a cleaner boot
#the logger should anyhow dump the ring-0 buffer at start to the
#logs, and that with dmesg can be used to check for problems
/bin/dmesg -n 1

exec /sbin/init.system $*
#rename to init.system
