#! /bin/bash
##
## drcom-client PUM v1.0, Python User Mode
##
##               drunlevel
## install init script for drcom kernel module loaded at boot-up
##
## Copyright (c) 2009, drcom-client Team
## Author:		Henry Huang <henry.s.huang@gmail.com>
##
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public
## License as published by the Free Software Foundation; either
## version 2.1 of the License, or (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
## General Public License for more details.
#
## You should have received a copy of the GNU General Public
## License along with this program; if not, write to the
## Free Software Foundation, Inc., 59 Temple Place - Suite 330,
## Boston, MA 02111-1307, USA.

runlevels=35

system=unknown
if [ -f /etc/redhat-release ]; then
    system=redhat
    util=$(type -p /sbin/chkconfig)
elif [ -f /etc/SuSE-release ]; then
    system=suse
    util=$(type -p /sbin/chkconfig)
elif [ -f /etc/debian_version ]; then
    system=debian
    util=$(type -p update-rc.d)
elif [ -f /etc/gentoo-release ]; then
    system=gentoo
    util=$(type -p rc-update)
## FIXME: ArchLinux has to add/del runlevel by hand.
elif [ -f /etc/arch-release ]; then
	system=archlinux
else
    echo "$0: Unknown system" 1>&2
fi

if [ -z $util ]; then
    echo Could not find add/remove init scripts to a runlevel utility 1>&2
    echo This operation can not continue without it 1>&2
    exit 1
fi

fail() {
    echo "$1"
    exit 1
}


addrunlevel() {
    if [ $system == "redhat" ] || [ $system == "suse" ]; then
        $util --del $1 >&/dev/null

        if $util -v &>/dev/null; then
            $util --level $runlevels $1 on || {
                fail "Cannot add $1 to run levels: $runlevels"
            }
        else
            $util $1 $runlevels || {
                fail "Cannot add $1 to run levels: $runlevels"
            }
        fi
    elif [ $system == "debian" ]; then
        # Debian does not support dependencies currently -- use argument $2
        # for start sequence number and argument $3 for stop sequence number
        $util -f $1 remove >&/dev/null
        $util $1 defaults $2 $3 >&/dev/null
    elif [ $system == "gentoo" ]; then
        $util del $1 >&/dev/null
        $util add $1 default >&/dev/null
	## FIXME: ArchLinux has to add runlevel by hand.
	## Edit /etc/rc.conf, and add "drcom" to the MODULES list: 
	## DAEMONS=(... drcom ...)	
    fi
}

delrunlevel() {
    if [ $system == "redhat" ] || [ $system == "suse" ]; then
        if $util --list $1 >& /dev/null; then
            $util --del $1 >& /dev/null || {
                fail "Cannot delete $1 from runlevels"
            }
        fi
    elif [ $system == "debian" ]; then
        $util -f $1 remove >&/dev/null
    elif [ $system == "gentoo" ]; then
        $util del $1 >&/dev/null
    fi
	## FIXME: ArchLinux has to del runlevel by hand.
	## Edit /etc/rc.conf, and del "drcom" to the MODULES list: 
	## DAEMONS=(... drcom ...)	
}

usage() {
    echo "Usage: $0 {add|del} script"
    exit 1
}

# Test for second argument
test -z $2 && {
    usage
}

case "$1" in
add)
    addrunlevel $2 $3 $4
    ;;
del)
    delrunlevel $2
    ;;
*)
    usage
esac

exit
