#!/bin/sh
#
# messagebus:	The D-BUS systemwide message bus
#
# chkconfig:	345 10 92
# description:	This is a daemon which broadcasts notifications of system events
#		and other messages. See http://www.freedesktop.org/software/dbus/
#
# processname:	dbus-daemon
# config:	/etc/dbus-1
# pidfile:	/run/messagebus.pid
#
### BEGIN INIT INFO
# Provides: messagebus
# Required-Start: $local_fs
# Required-Stop: $local_fs
# Default-Start: 3 4 5
# Default-Stop: 0 1 6
# Short-Description: 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
### END INIT INFO

# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

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

DAEMON=/bin/dbus-daemon
LOCKFILE=/var/lock/subsys/messagebus
PIDFILE=/run/messagebus.pid
USER=messagebus

RETVAL=0

start() {
    if [ ! -d /run/dbus ]; then
    	mkdir -p /run/dbus/users
    	chmod 1777 /run/dbus/users
    fi
    if [ -x /bin/dbus-uuidgen ] ; then
        /bin/dbus-uuidgen --ensure
    fi

    msg_starting $"system message bus"
    start_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --no-announce -- $DAEMON --system
    RETVAL=$?
    return $RETVAL
}

stop() {
    msg_stopping $"system message bus"
    stop_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --no-announce -- $DAEMON -TERM
    RETVAL=$?
    rm -f "$PIDFILE" ||:
    return $RETVAL
}

reload() {
    msg_reloading $"system message bus"
    [ -S /run/dbus/system_bus_socket ] || return 0
    dbus-send --print-reply --system --type=method_call --dest=org.freedesktop.DBus / org.freedesktop.DBus.ReloadConfig > /dev/null
    RETVAL=$?
    return $RETVAL
}

# See how we were called.
case "$1" in
    start)
	start
	;;
    stop)
	stop
	;;
    reload)
	reload
	;;
    restart)
	stop
	start
	;;
    condstop)
	if [ -e "$LOCKFILE" ]; then
		stop
	fi
	;;
    condrestart)
	if [ -e "$LOCKFILE" ]; then
		stop
		start
	fi
	;;
    condreload)
	if [ -e "$LOCKFILE" ]; then
		reload
	fi
	;;
    status)
	status --pidfile "$PIDFILE" --expect-user "$USER" -- $DAEMON
	RETVAL=$?
	;;
    *)
        msg_usage "${0##*/} {start|stop|reload|restart|condstop|condrestart|condreload|status}"
        ;;
esac

exit $RETVAL
