#!/bin/bash
#
# shell script hack to accomplish equivalent of 'chkconfig --add'
#

ROOT=$1
SERVICE=$2
RCFILE="$ROOT/etc/init.d/$SERVICE"

[ ! -f "$RCFILE" ] && exit 1

if [ "$ROOT" = "/" ]; then
    exit 1
fi

while read comment cruft LEVELS START STOP ; do
    if [ "$comment" = "#" -a "$cruft" = "chkconfig:" ]; then
	break
    fi
done < $RCFILE

[ -z $STOP ] && exit 1

# this is goofy
for LVL in $(seq 0 6) ; do
    if echo $LEVELS | grep -q $LVL ; then
	PRI=$START
	PREFIX='S'
    else
	PRI=$STOP
	PREFIX='K'
    fi

    LNAME=$ROOT/etc/rc${LVL}.d/$PREFIX$PRI$SERVICE
    [ -e $LNAME ] && rm -f $LNAME 2>/dev/null
    ln -s ../init.d/$SERVICE $LNAME
done


