#! /bin/sh
### BEGIN INIT INFO
# Provides: bluetooth
# Required-Start:    $local_fs $syslog $remote_fs dbus
# Required-Stop:     $local_fs $syslog $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start bluetooth daemons
### END INIT INFO
#
# bluez    Bluetooth subsystem starting and stopping
#
# originally from bluez's scripts/bluetooth.init
#
# Edd Dumbill <ejad@debian.org>
# LSB 3.0 compilance and enhancements by Filippo Giunchedi <filippo@debian.org>
#
# Updated for bluez 4.7 by Mario Limonciello <mario_limonciello@dell.com>
#
# startup control over dund and pand can be changed by editing
# /etc/default/bluetooth

PATH=/sbin:/bin:/usr/sbin:/usr/bin
DESC=bluetooth

DAEMON=/usr/sbin/bluetoothd
HCIATTACH=/usr/sbin/hciattach

HID2HCI=/usr/sbin/hid2hci
HID2HCI_ENABLED=1
HID2HCI_UNDO=1

UART_CONF=/etc/bluetooth/uart

RFCOMM=/usr/bin/rfcomm
RFCOMM_NAME=rfcomm
RFCOMM_CONF=/etc/bluetooth/rfcomm.conf
SDPTOOL=/usr/bin/sdptool

test -f /etc/default/bluetooth && . /etc/default/bluetooth
test -f /etc/default/rcS && . /etc/default/rcS

. /lib/lsb/init-functions

set -e

run_sdptool()
{
	test -x $SDPTOOL || return 1

	if ! test -z "$SDPTOOL_OPTIONS" ; then
		oldifs="$IFS"
		IFS=";"
		for o in $SDPTOOL_OPTIONS ; do
			#echo "execing $SDPTOOL $o"
			IFS=" "
			if [ "$VERBOSE" != "no" ]; then
				$SDPTOOL $o
			else
				$SDPTOOL $o >/dev/null 2>&1
			fi
		done
		IFS="$oldifs"
	fi

}

enable_hci_input()
{
       if [ "$VERBOSE" != no ]; then
               log_progress_msg "hid_devices"
               $HID2HCI --tohci
       else
               $HID2HCI --tohci >/dev/null 2>&1
       fi
}

disable_hci_input()
{
       if [ "$VERBOSE" != no ]; then
               log_progress_msg "hid_devices"
               $HID2HCI --tohid
       else
               $HID2HCI --tohid >/dev/null 2>&1
       fi
}

start_uarts()
{
	[ -f $HCIATTACH ] && [ -f $UART_CONF ] || return
	grep -v '^#' $UART_CONF | while read i; do
               if [ "$VERBOSE" != no ]; then
                       $HCIATTACH $i
               else
                       $HCIATTACH $i >/dev/null 2>&1
               fi
	done
}

stop_uarts()
{
	killall hciattach > /dev/null 2>&1 || true
}

start_rfcomm()
{
	if [ -x $RFCOMM ] && [ -f $RFCOMM_CONF ] ; then
		# rfcomm must always succeed for now: users
		# may not yet have an rfcomm-enabled kernel
                if [ "$VERBOSE" != no ]; then
                       log_progress_msg "rfcomm"
                       $RFCOMM -f $RFCOMM_CONF bind all || true
                else
                       $RFCOMM -f $RFCOMM_CONF bind all >/dev/null 2>&1 || true
                fi
	fi
}

stop_rfcomm()
{
	if [ -x $RFCOMM ] ; then
               if [ "$VERBOSE" != no ]; then
                       log_progress_msg "rfcomm"
                       $RFCOMM unbind all || true
               else
                       $RFCOMM unbind all >/dev/null 2>&1 || true
               fi
	fi
}

restart_rfcomm()
{
	if [ -x $RFCOMM ] && [ -f $RFCOMM_CONF ] ; then
               if [ "$VERBOSE" != no ]; then
                       log_progress_msg  "rfcomm"
                       $RFCOMM unbind all || true
                       $RFCOMM -f $RFCOMM_CONF bind all || true
               else
                       $RFCOMM unbind all >/dev/null 2>&1|| true
                       $RFCOMM -f $RFCOMM_CONF bind all >/dev/null 2>&1 || true
               fi
	fi
}

case "$1" in
  start)
	log_daemon_msg "Starting $DESC"

	if test "$BLUETOOTH_ENABLED" = "0"; then
		log_progress_msg "disabled. see /etc/default/bluetooth"
		log_end_msg 0
		exit 0
	fi

	start-stop-daemon --start --quiet --exec $DAEMON || true
	log_progress_msg "bluetoothd"

	run_sdptool || true

	start_uarts || true

	if test "$HID2HCI_ENABLED" = "1"; then
		enable_hci_input || true
	fi
	start_rfcomm || true
	log_end_msg 0
    ;;
  stop)
	log_daemon_msg "Stopping $DESC"
	if test "$BLUETOOTH_ENABLED" = "0"; then
		log_progress_msg "disabled."
		log_end_msg 0
		exit 0
	fi
	stop_rfcomm || true
	if test "$HID2HCI_UNDO" = "1"; then
		disable_hci_input || true
	fi
	start-stop-daemon --stop --quiet --exec $DAEMON || true
	log_progress_msg "bluetoothd"
	stop_uarts || true
	log_end_msg 0
	;;
  restart|force-reload)
	$0 stop
	$0 start
	;;
  status)
	status_of_proc "$DAEMON" "$DESC" && exit 0 || exit $?
	;;
  *)
	N=/etc/init.d/bluetooth
	# echo "Usage: $N {start|stop|restart|reload|force-reload|status}" >&2
	echo "Usage: $N {start|stop|restart|force-reload|status}" >&2
	exit 1
	;;
esac

exit 0

# vim:noet
