#!/bin/sh
export PATH=/bin:/sbin:/usr/bin:/usr/sbin
# disable kernel messages
if [ -f /proc/sys/kernel/printk ];then
	echo 0 > /proc/sys/kernel/printk
fi
ln -s /proc/self/fd/0 /dev/stdin
ln -s /proc/self/fd/1 /dev/stdout
ln -s /proc/self/fd/2 /dev/stderr
: ${rcquiet=0}
if [ $rcquiet -ne 1 ];then
	echo "System initialization ..."
fi

# create some useful directories in tmpfs
mkdir -p /var/log
mkdir -p /var/run
mkdir -p /var/tmp
touch /var/log/lastlog
touch /var/log/wtmp

# start dynamic device node management
[ -f /etc/init.d/rcdev ] && . /etc/init.d/rcdev

# seed some random
cat /etc/.rnd >/dev/urandom 2>&1

# setup cfgfs
[ -x /sbin/cfgfs ] && {
  cfgfs setup
  mount -o remount,ro /
}

# remount /tmp with smaller size
size=$(cat /etc/tmpfs 2>/dev/null)
[ -z $size ] && size=2048
mount -o remount,nosuid,nodev,mode=1777,size=${size}k -t tmpfs tmpfs /tmp

HOSTNAME=
[[ -s /etc/hostname ]] && HOSTNAME=$(cat /etc/hostname)
HOSTNAME=${HOSTNAME%%.*}
echo ${HOSTNAME:=openadk} >/proc/sys/kernel/hostname

chown 0:0 /tmp; chmod 1777 /tmp

if test x"$rcquiet" = x"0" && sh -n /etc/rc.conf >/dev/null 2>&1; then
	. /etc/rc.conf
	unset rcquiet
	rcquiet=0
else
	unset rcquiet
	rcquiet=1
fi
{
	grep '^#INIT ' /etc/init.d/* | \
	    sort -nk2 | \
	    while read line; do
		rcenabled=NO
		rcname=${line%%:*}
		rcname=${rcname##*/}
		test x"$rcquiet" = x"0" && eval 'rcenabled=${'"${rcname}"':-NO}'
		test x"$rcenabled" = x"NO" || echo -n "Starting ${rcname}... "
		sh ${line%%:*} autostart
		rc=$?
		case $rcenabled:$rc in
		(NO:*)
			;;
		(*:0)
			echo done.
			;;
		(*:*)
			echo "failed ($rc)."
			;;
		esac
	    done
} |tee /dev/stderr |logger -p 6 -t ''

if [ $rcquiet -ne 1 ]; then
  if [ -f /etc/rc.netinfo ]; then
    . /etc/rc.netinfo
  fi
fi
