#!/bin/bash

SYS_BTCONF="/etc/bluetooth.conf"
BOOT_BTCONF="/boot/bluetooth.conf"
BTCONF="/data/etc/bluetooth.conf"
CMCONF="/etc/connman/main.conf"

PROG="/usr/bin/dbus-daemon"
PROG_UG="/usr/bin/dbus-uuidgen"
PROG_UA="/usr/bin/udevadm"


test -x ${PROG} || exit 0

# dbus is currently only used by bluez and connman
test -s ${BTCONF} || test -s ${BOOT_BTCONF} || test -s ${SYS_BTCONF} || test -s ${CMCONF} || exit 0

test -n "${OS_VERSION}" || source /etc/init.d/base


start() {
    mkdir -p /tmp/dbus
    mkdir -p /var/lib/dbus

    msg_begin "Starting dbus"

    ${PROG_UG} --ensure
    ${PROG} --system
    
    test $? == 0 && msg_done || msg_fail
}

stop() {
    msg_begin "Stopping dbus"
    ${PROG_UA} control --stop-exec-queue
    killall -q $(basename ${PROG})
    test $? == 0 && msg_done || msg_fail

    rm -f /var/run/messagebus.pid
}

case "$1" in
    start)
        start
        ;;

    stop)
        stop
        ;;
    
    restart)
        stop
        start
        ;;

    *)
        echo "Usage: $0 {start|stop|restart}"
        exit 1
        ;;
esac

exit $?

