#!/bin/sh
# Author: Timo Hoenig <thoenig@suse.de>
#
# /etc/init.d/dbus
#
### BEGIN INIT INFO
# Provides:          dbus
# Required-Start:
# Should-Start:
# Required-Stop:     
# Should-Stop:
# Default-Start:     3 5
# Default-Stop:      
# Short-Description: D-BUS is a message bus system for applications to talk to one another.
# Description:       D-BUS supplies both a system daemon (for events such as "new hardware device added"
#                    or "printer queue changed") and a per-user-login-session daemon (for general IPC needs
#                    among user applications). Also, the message bus is built on top of a general one-to-one
#                    message passing framework, which can be used by any two apps to communicate directly
#                    (without going through the message bus daemon).
### END INIT INFO

# Check for binary
DBUSDAEMON_BIN=/usr/bin/dbus-daemon
test -x $DBUSDAEMON_BIN || exit 5

# Parameters (startup)
DBUSDAEMON_PARA="--system";
DBUSDAEMON_PIDDIR="/var/run/dbus";
DBUSDAEMON_PID=$DBUSDAEMON_PIDDIR/pid;

# Source LSB init functions
# providing start_daemon, killproc, pidofproc, 
# log_success_msg, log_failure_msg and log_warning_msg.
# This is currently not used by UnitedLinux based distributions and
# not needed for init scripts for UnitedLinux only. If it is used,
# the functions from rc.status should not be sourced or used.
#. /lib/lsb/init-functions

. /etc/rc.status

# Reset status of this service
rc_reset

case "$1" in
    start)
	if [ ! -d $DBUSDAEMON_PIDDIR ]; then
		mkdir -p $DBUSDAEMON_PIDDIR;
		chown messagebus:messagebus $DBUSDAEMON_PIDDIR;
	fi
	if [ -e $DBUSDAEMON_PID ]; then
		if [ -d /proc/`cat $DBUSDAEMON_PID` ]; then
			echo "D-BUS already started. Not starting."
			exit 0;
		else
			echo "Removing stale PID file $DBUSDAEMON_PID.";
			rm -f $DBUSDAEMON_PID;
		fi
	fi	
	echo -n "Starting D-BUS daemon";
	startproc -f -p $DBUSDAEMON_PID $DBUSDAEMON_BIN $DBUSDAEMON_PARA
	rc_status -v
	;;
    stop)
	echo -n "Shutting down D-BUS daemon"
	killproc -p $DBUSDAEMON_PID -TERM $DBUSDAEMON_BIN
	rm -f $DBUSDAEMON_PID;
	rc_status -v
	;;
    try-restart)
	$0 status >/dev/null &&  $0 restart
	rc_status
	;;
    restart)
	$0 stop
	$0 start
	rc_status
	;;
    force-reload)
	echo -n "Reload service D-BUS daemon"
	$0 stop  &&  $0 start
	rc_status
	;;
    reload)
	rc_failed 3
	rc_status -v
	;;
    status)
	echo -n "Checking for service D-BUS daemon"
	checkproc $DBUSDAEMON_BIN
	rc_status -v
	;;
    probe)
	## Optional: Probe for the necessity of a reload, print out the
	## argument to this init script which is required for a reload.
	## Note: probe is not (yet) part of LSB (as of 1.2)
	# test /etc/FOO/FOO.conf -nt /var/run/FOO.pid && echo reload
	;;
    *)
	echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|probe}"
	exit 1
	;;
esac
rc_exit

