#!/bin/bash
#
# script: rc.messagebus
#
# 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>
#
# LimeTech - modified for Unraid OS
# Bergware - modified for Unraid OS, October 2023

DAEMON="System Message Bus"
PIDFILE="/var/run/dbus/dbus.pid"

# run & log functions
. /etc/rc.d/rc.runlog

dbus_running(){
  sleep 0.1
  ps -u messagebus -c | grep -wq dbus-daemon
}

dbus_start(){
  log "Starting $DAEMON..."
  local REPLY
  if dbus_running; then
    REPLY="Already started"
  else
    mkdir -p $(dirname $PIDFILE)
    rm -f $(dirname $PIDFILE)/*
    if [[ -x /usr/bin/dbus-uuidgen && -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
      run /usr/bin/dbus-daemon --system
    fi
    if dbus_running; then REPLY="Started"; else REPLY="Failed"; fi
  fi
  log "$DAEMON...  $REPLY."
}

dbus_stop(){
  log "Stopping $DAEMON..."
  local REPLY
  if ! dbus_running; then
    REPLY="Already stopped"
  else
    run kill $(cat $PIDFILE)
    # Just in case:
    run killall --ns $$ dbus-daemon
    rm -f $PIDFILE
    if ! dbus_running; then REPLY="Stopped"; else REPLY="Failed"; fi
  fi
  log "$DAEMON...  $REPLY."
}

dbus_reload(){
  log "Reloading $DAEMON..."
  if [[ -e $PIDFILE ]]; then
    pid=$(cat $PIDFILE)
    run kill -HUP $pid
  else
    run killall --ns $$ -HUP dbus-daemon
  fi
  log "$DAEMON...  Reloaded."
}

dbus_status(){
  if dbus_running; then
    echo "$DAEMON is currently running."
  else
    echo "$DAEMON is not running."
    exit 1
  fi
}

# See how we were called.
case "$1" in
'start')
  dbus_start
  ;;
'stop')
  dbus_stop
  ;;
'restart')
  dbus_stop
  sleep 1
  dbus_start
  log "You may need to restart your Window Manager to reconnect to the system dbus."
  ;;
'reload')
  dbus_reload
  ;;
'status')
  dbus_status
  ;;
*)
  echo "Usage: $BASENAME start|stop|restart|reload|status"
  exit 1
  ;;
esac
exit 0
