#!/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

# This is a modified version of the rc.messagebus script distributed with the
# dbus sources.  Thanks to Don Tanner of the GWare <http://gware.org> Project
# for most of the work involved      --Robby Workman <rworkman@slackware.com>


PIDFILE=/var/run/dbus/dbus.pid

start() {
  mkdir -p $(dirname $PIDFILE)
  if ! ps -u messagebus -c | grep -wq dbus-daemon; then
    rm -f $(dirname $PIDFILE)/*
    if [ -x /usr/bin/dbus-uuidgen -a -x /usr/bin/dbus-daemon ] ; then
      if [ ! -e /etc/no-machine-id ]; then
        # Ah, the machine-id. DBus won't work right without it, and browsers and
        # other software will make use of this identifier. If you hate that idea,
        # you may create /etc/no-machine-id and then delete /etc/machine-id and
        # /var/lib/dbus/machine-id and we won't try to create these again.
        # You might pay for your "privacy" with bugs, though.
        # It is not recommended to do this, but it's your machine.
        #
        # If /etc/machine-id is a symlink, get rid of it:
        if [ -L /etc/machine-id ]; then
          rm -f /etc/machine-id
        fi
        # If /var/lib/dbus/machine-id is a symlink, get rid of it:
        if [ -L /var/lib/dbus/machine-id ]; then
          rm -f /var/lib/dbus/machine-id
        fi
        # If you have both /etc/machine-id and /var/lib/dbus/machine-id then we will
        # keep /etc/machine-id and back up /var/lib/dbus/machine-id:
        if [ -r /etc/machine-id -a -r /var/lib/dbus/machine-id ]; then
          mv /var/lib/dbus/machine-id /var/lib/dbus/machine-id.backup
        fi
        # If there's a /var/lib/dbus/machine-id file, and no /etc/machine-id, move it:
        if [ -r /var/lib/dbus/machine-id -a ! -e /etc/machine-id ]; then
          mv /var/lib/dbus/machine-id /etc/machine-id
          chmod 444 /etc/machine-id
        fi
        # If there's no /etc/machine-id, fix that:
        if [ ! -r /etc/machine-id ]; then
          /usr/bin/dbus-uuidgen --ensure=/etc/machine-id
          chmod 444 /etc/machine-id
        fi
        # Create the var/lib/dbus/machine-id symlink:
        rm -f /var/lib/dbus/machine-id
        ln -sf /etc/machine-id /var/lib/dbus/machine-id
      fi
      echo "Starting system message bus:  /usr/bin/dbus-uuidgen --ensure ; /usr/bin/dbus-daemon --system"
      /usr/bin/dbus-daemon --system 1> /dev/null
    fi
  fi
}

stop() {
  if [ -e "$PIDFILE" ]; then
    echo "Stopping system message bus..."
    pid=$(cat $PIDFILE)
    kill $pid 1> /dev/null 2> /dev/null
    # Just in case:
    killall dbus-daemon 1> /dev/null 2> /dev/null
    rm -f $PIDFILE
  fi
}

reload() {
  echo "Reloading system message bus configuration..."
  if [ -e "$PIDFILE" ]; then
    pid=$(cat $PIDFILE)
    kill -HUP $pid
  else
    killall -HUP dbus-daemon
  fi
}

status() {
  if ps -u messagebus -c | grep -wq dbus-daemon; then
    echo "System dbus-daemon is running."
  else
    echo "System dbus-daemon is stopped."
  fi
}

# See how we were called.
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    stop
    start
    echo "You may need to restart your Window Manager to reconnect to the system dbus."
    ;;
  reload)
    reload
    ;;
  status)
    status
    ;;
  *)
    echo $"Usage: $0 {start|stop|restart|reload|status}"
    ;;
esac