#!/bin/sh
#
# Start/stop the Bluetooth daemons
#

set -e

PATH=/sbin:/bin:/usr/sbin:/usr/bin
NAME=bluetooth
DESC="Bluetooth subsystem"

DAEMON_NAME=bluetoothd
HID2HCI_NAME=hid2hci

# The register-passkeys script was originally written by Debian:
REGISTER_PASSKEYS=/usr/lib/bluetooth/register-passkeys

DAEMON_EXEC="`which $DAEMON_NAME || true`"
HID2HCI_EXEC="`which $HID2HCI_NAME || true`"

DAEMON_ENABLE=true
HID2HCI_ENABLE=false

[ -e /etc/default/bluetooth ] && . /etc/default/bluetooth

bluetooth_start() {
        echo -n "Starting $DESC:"
        if $DAEMON_ENABLE && [ -x "$DAEMON_EXEC" ]; then
                $DAEMON_EXEC
                echo -n " $DAEMON_NAME"
        fi
        if $HID2HCI_ENABLE && [ -x "$HID2HCI_EXEC" ] ; then
                $HID2HCI_EXEC --tohci > /dev/null 2>&1 || true
                echo -n " $HID2HCI_NAME"
        fi
        if [ -x $REGISTER_PASSKEYS ]; then
                $REGISTER_PASSKEYS
                echo -n " passkeys"
        fi
        echo "."
}

bluetooth_stop() {
	echo -n "Stopping $DESC:"
	killall $DAEMON_NAME > /dev/null 2>&1 || true
	echo -n " $DAEMON_NAME"
	echo "."
}

case "$1" in
  start)
	bluetooth_start ;
	;;
  stop)
	bluetooth_stop ;
	;;
  restart)
        bluetooth_stop ;
        sleep 1
        bluetooth_start ;
        ;;
  *)
	echo "Usage: $N start|stop|restart" >&2
	exit 1
	;;
esac

exit 0
