#!/bin/sh

help() {
    echo "Syntax: `basename $0` --add|--remove <service>"
    echo "      Where service is a executable script in /etc/init.d."
    echo "      The service has to have a comment with \"# chkconfig: <run_levels> <start_priority> <stop_priority>\"."
    echo "      The <run_levels> are ignored with busybox as it is a SysV init functionnality."
    echo ""
    exit 1
}

[ $# -ne 2 -o ! -x "/etc/init.d/${2:-none}" ] && help

eval "`sed -e '/^#[[:space:]]*chkconfig:/{s/^.*:[[:space:]]*[0-9]\+[[:space:]]\+\([0-9]\+\)[[:space:]]\+\([-0-9]\+\)/start_prio=\1;kill_prio=\2;/;p};d' /etc/init.d/$2`"
case "$1" in
    --add)
        echo "start_prio: $start_prio	kill_prio: $kill_prio" > /etc/rc.d/LOG$2
        ln -s ../init.d/$2 /etc/rc.d/S${start_prio}$2
        [ "$kill_prio" != "--" ] && ln -s ../init.d/$2 /etc/rc.d/K${kill_prio}$2
        ;;
    --remove|--del)
        rm /etc/rc.d/S${start_prio}$2
        [ "$kill_prio" != "--" ] && rm /etc/rc.d/K${kill_prio}$2
        ;;
    *)
        help
esac
