#!/bin/sh
#
# messagebus:   The D-BUS systemwide message bus
# description:  This is a daemon which broadcasts notifications of system events \
#               and other messages. See http://www.freedesktop.org/software/dbus/
#
# processname: dbus-daemon
# pidfile: /var/run/messagebus.pid

# Sanity checks.
[ -x /usr/bin/dbus-daemon ] || exit 0
chmod 4755 /usr/libexec/dbus-daemon-launch-helper
# Create needed directories.
[ -d /var/run/dbus ] || mkdir -p /var/run/dbus
[ -d /var/lock/subsys ] || mkdir -p /var/lock/subsys
[ -d /tmp/dbus ] || mkdir -p /tmp/dbus

RETVAL=0

service_start() {
    printf "Starting system message bus: "
    local has_postfix=$(getent passwd dbus)
    [ "X${has_postfix}" = "X" ] && {
        adduser -H -D dbus
    }
    dbus-uuidgen --ensure
    dbus-daemon --system
    RETVAL=$?
    echo "done"
    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/dbus-daemon
}

service_stop() {
    printf "Stopping system message bus: "

    ## we don't want to kill all the per-user $processname, we want
    ## to use the pid file *only*; because we use the fake nonexistent
    ## program name "$servicename" that should be safe-ish
    killall dbus-daemon
    RETVAL=$?
    echo "done"
    if [ $RETVAL -eq 0 ]; then
        rm -f /var/lock/subsys/dbus-daemon
        rm -f /var/run/messagebus.pid
    fi
}

# See how we were called.
case "$1" in
start)
    service_start
    ;;
stop)
    service_stop
    ;;
status)
    status $processname
    RETVAL=$?
    ;;
restart)
    service_stop
    service_start
    ;;
condrestart)
    if [ -f /var/lock/subsys/$servicename ]; then
        service_stop
        service_start
    fi
    ;;
reload)
    echo "Message bus can't reload its configuration, you have to restart it"
    RETVAL=$?
    ;;
*)
    echo "Usage: $0 {start|stop|restart|condrestart|reload}"
    ;;
esac
exit $RETVAL
