#!/bin/sh

start() {
        if [ ! -f /var/run/dbus/bluez_flag ]; then
                /usr/local/etc/init.d/dbus restart
                touch /var/run/dbus/bluez_flag
        fi
        if ! pidof bluetoothd >/dev/null; then
                start-stop-daemon --start --exec /usr/local/lib/bluetooth/bluetoothd -- --experimental & 2>/dev/null
        fi
}

stop() {
        start-stop-daemon --stop --exec /usr/local/lib/bluetooth/bluetoothd 2>/dev/null
        sleep 2
}

status() {
        if pidof bluetoothd >/dev/null; then
                echo -e "\nbluetoothd is running.\n"
                exit 0
        else
                echo -e "\nbluetoothd is not running.\n"
                exit 1
        fi
}

case $1 in
        start) start
                ;;
        stop) stop
                ;;
        status) status
                ;;
        restart) stop; start
                ;;
        *) echo -e "\n$0 [start|stop|restart|status]\n"
                ;;
esac

