#!/bin/bash
#
# chkconfig: 2345 97 02
# description: dhcdbd provides D-BUS control of the ISC DHCP client, \
#              dhclient, and D-BUS access to the DHCP options obtained by \
#              dhclient for each IPv4 interface.  dhcdbd shuts down \
#              automatically when the messagebus is shut down.
# processname: dhcdbd

# Source function library.
. /etc/init.d/functions

RETVAL=0

prog=dhcdbd
dhcdbd=/sbin/dhcdbd
lockfile=/var/lock/subsys/dhcdbd
busfile=/var/run/dbus/system_bus_socket

start() {
    echo -n $"Starting $prog:"
    daemon $dhcdbd --system
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && touch $lockfile
    return $RETVAL
}

stop () {
    echo -n $"Stopping $prog:"
    if [ -e $lockfile ] && [ -e $busfile ]; then
        /bin/dbus-send --system \
                        --dest=com.redhat.dhcp \
                        --type=method_call \
                        --print-reply \
                        --reply-timeout=20000 \
                        /com/redhat/dhcp \
                        com.redhat.dhcp.quit >/dev/null 2>&1
        RETVAL=$?
    else
        killproc $prog -TERM
        RETVAL=$?
    fi

    if [ $RETVAL = 0 ]; then
        success
        echo
    else
        failure
        echo
    fi

    rm -f $lockfile
    return $RETVAL
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    condrestart)
        if [ -e $lockfile ]; then
            stop
            start
        fi
        ;;
    restart)
        stop
        start
        ;;
    reload)
        RETVAL=3
        ;;
    status)
        status $dhcdbd
        RETVAL=$?
        ;;
    *)
        echo $"Usage: $0 {start|stop|restart|condrestart|status}"
        RETVAL=3
        ;;
esac;

exit $RETVAL
