#!/bin/sh
#
# messagebus:   The D-BUS systemwide message bus
#
# chkconfig: 345 22 85
# 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
#
### BEGIN INIT INFO
# Provides: messagebus
# Required-Start: $syslog $local_fs
# Required-Stop: $syslog $local_fs
# Default-Start: 2 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

# Sanity checks.
[ -x /sred/usr/bin/dbus-daemon ] || exit 0

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

# so we can rearrange this easily
processname=dbus-daemon
servicename=messagebus

RETVAL=0

SESSION_BUS=false

start_session_bus() {
    echo -n $"Starting session message bus: "
    if test -z "$DBUS_SESSION_BUS_ADDRESS"; then 
        DBUS_SESSION_BUS_ADDRESS="unix:abstract=/tmp/dbus-asession"
        echo "Dbus address per session is: $DBUS_SESSION_BUS_ADDRESS"   
        export DBUS_SESSION_BUS_ADDRESS
    fi
    addr=$(eval echo \$\{DBUS_SESSION_BUS_ADDRESS\} )
    /usr/bin/$processname --session --fork --address=$addr
    RETVAL=$?
}


start() {
    echo -n $"Starting system message bus: "
    if [ ! -d /var/lock/subsys ]; then
        mkdir /var/lock/subsys
    fi
    if [ -x /usr/bin/dbus-uuidgen ] ; then
        /usr/bin/dbus-uuidgen --ensure
    fi
    if [ -f /var/run/messagebus.pid ] ; then
        rm -f /var/run/messagebus.pid
    fi
    
    $processname --system
    RETVAL=$?
    echo
    if [ "$SESSION_BUS" == true ]; then
       if [ $RETVAL -eq 0 ]; then
          touch /var/lock/subsys/$servicename
          start_session_bus
       fi
    fi
}

status() {
    echo -n $"Status system message bus:"
    ppid=`pgrep $processname`
    if test -z "$ppid"; then
    	echo " stopped"
    else
        echo " "    
        echo -n $"System message bus:" 
        for pid in $ppid ; do
            res=`grep system /proc/$pid/cmdline`
            [ "$res" != "" ] && echo " running"
        done
        echo -n $"Session message bus:"
        for pid in $ppid ; do
            ress=`grep session /proc/$pid/cmdline`
            [ "$ress" != "" ] && echo " running"
            done
    fi
}

stop() {
    echo -n $"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 -TERM $processname
    RETVAL=$?
    echo
    if [ $RETVAL -eq 0 ]; then
        rm -f /var/lock/subsys/$servicename
        rm -f /var/run/messagebus.pid
    fi
}

reload() {
        echo -n $"Reloading $processname: "                     
        pkill -HUP $processname                           
        RETVAL=$?                                        
        echo                                             
        return $RETVAL 
}

# See how we were called.
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        status $servicename
        RETVAL=$?
        ;;
    restart)
        stop
        start
        ;;
    condrestart)
        if [ -f /var/lock/subsys/$servicename ]; then
            stop
            start
        fi
        ;;
    reload)
        reload
        RETVAL=$?
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|reload}"
        ;;
esac
exit $RETVAL
