#! /bin/sh

trap "" 1
pgm=`/usr/bin/basename $0`

for j in /etc/rc0.d/*
do
    if [ -f $j ]
    then
	dtou $j
	sh $j
    fi 
done

rm -f /bin/login
cd /

for j in /var/run/*.pid
do
    if [ -f $j ]
    then
	kill `cat $j` 2>/dev/null
    fi 
done

for j in /proc/[0-9]?*
do
    j=`/usr/bin/basename $j`
    if [ $j != $$ ]
    then
	kill $j 2>/dev/null
    fi
done

sleep 2

for j in /var/run/*.pid
do
    if [ -f $j ]
    then
	kill -9 `cat $j` 2>/dev/null
    fi 
done

fs=''

while read line
do
    set -- $line

    if [ "$2" != "/proc" ]
    then
	fs="$fs $2"
    fi
done </proc/mounts

for j in $fs
do
    /bin/umount $j
    /bin/mount -o remount,ro $j
done 2>/dev/null

/bin/umount -a 2>/dev/null
/bin/sync
sleep 2

echo -e '\007'

if [ "$pgm" = halt ]
then
    echo "System halted." >/dev/console
    while [ 1 ]
    do
	sleep 1000000
    done
fi

if [ "$pgm" = poweroff ]
then
    exec /usr/local/bin/poweroff -f
fi

exec /usr/local/sbin/busybox reboot $*
