#!/bin/sh
#
# rc	Run startup commands depending on the runlevel

exec 2>/dev/null

RCDIR=/etc/rc.d
PATH=/sbin:/bin:/usr/sbin/:/usr/bin:/usr/local/bin:$RCDIR/functions
export PATH

[ "$RUNLEVEL" ] || RUNLEVEL=m

if [ "$LASTRUNLEVEL" -a -d $RCDIR/rc.$LASTRUNLEVEL ]; then
    # kill all SYSV processes in last run level
    #
    SYSV=`cd $RCDIR/rc.$LASTRUNLEVEL; echo K[0-9][0-9]*`
    if [ "$SYSV" != "K[0-9][0-9]*" ]; then
	for service in $SYSV; do
	    $RCDIR/rc.$LASTRUNLEVEL/$service stop
	done
    fi
fi

# kill all registered services from previous runlevel
#
showreg | while read service; do
    if [ -x $RCDIR/init.d/$service ]; then
	$RCDIR/init.d/$service stop
    elif [ "$LASTRUNLEVEL" -a -x $RCDIR/rc.$LASTRUNLEVEL/$service ]; then
	$RCDIR/rc.$LASTRUNLEVEL/$service stop
    fi
done

# reset the registry, and...
initregistry


# ... away we go
#
if [ -d $RCDIR/rc.$RUNLEVEL ]; then
    SYSV=
    for service in `cd $RCDIR/rc.$RUNLEVEL;ls`; do
	case $service in
	K[0-9][0-9]*) ;;
	S[0-9][0-9]*) SYSV="$SYSV $service" ;;
	'*') ;;
	*)  need $service
	    ;;
	esac
    done

    # start SYSV compatable services
    for service in $SYSV; do
	$RCDIR/rc.$RUNLEVEL/$service start
    done
fi
