#! /bin/sh
#
# bluez-utils    Bluetooth subsystem starting and stopping
#
# startup control over dund and pand can be changed by 
# editing /etc/default/bluez-utils

PATH=/sbin:/bin:/usr/sbin:/usr/bin
DESC=bluez-utils

HCID=/usr/sbin/hcid
HCID_NAME=hcid

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

DUND_DAEMON=/usr/bin/dund
DUND_NAME=dund
PAND_DAEMON=/usr/bin/pand
PAND_NAME=pand
HIDD_DAEMON=/usr/bin/hidd
HIDD_NAME=hidd

DUND_ENABLED=0
PAND_ENABLED=0
HIDD_ENABLED=0
DUND_OPTIONS=""
PAND_OPTIONS=""
HIDD_OPTIONS="--master --server --encrypt"

test -f /etc/default/bluez-utils && . /etc/default/bluez-utils

test -f /home/user/.start_hidd && HIDD_ENABLED=1

# test for essential daemons
test -x $HCID || exit 0
test -x $RFCOMM || exit 0

# disable nonessential daemons if not present
if test "$DUND_ENABLED" != "0"; then
	if ! test -f $DUND_DAEMON; then
		DUND_ENABLED=0
	fi
fi

if test "$PAND_ENABLED" != "0"; then
	if ! test -f $PAND_DAEMON; then
		PAND_ENABLED=0
	fi
fi

if test "$HIDD_ENABLED" != "0"; then
	if ! test -f $HIDD_DAEMON; then
		HIDD_ENABLED=0
	fi
fi

set -e

start_pan()
{
	if test "$DUND_ENABLED" != "0"; then
		start-stop-daemon --start --quiet --exec $DUND_DAEMON -- $DUND_OPTIONS
		echo -n " $DUND_NAME"
	fi
	if test "$PAND_ENABLED" != "0"; then
		start-stop-daemon --start --quiet --exec $PAND_DAEMON -- $PAND_OPTIONS
		echo -n " $PAND_NAME"
	fi
}


stop_pan()
{
	if test "$DUND_ENABLED" != "0"; then
		start-stop-daemon --stop --quiet --exec $DUND_DAEMON || true
		echo -n " $DUND_NAME"
	fi
	if test "$PAND_ENABLED" != "0"; then
		start-stop-daemon --stop --quiet --exec $PAND_DAEMON || true
		echo -n " $PAND_NAME"
	fi
}

start_hid()
{
	if test "$HIDD_ENABLED" != "0"; then
		start-stop-daemon --start --quiet --exec $HIDD_DAEMON -- $HIDD_OPTIONS
		echo -n " $HIDD_NAME"
	fi
}

stop_hid()
{
	if test "$HIDD_ENABLED" != "0"; then
		$HIDD_DAEMON --killall
		start-stop-daemon --stop --quiet --exec $HIDD_DAEMON || true
		echo -n " $HIDD_NAME"
	fi
}

start_rfcomm()
{
	if [ -x $RFCOMM ] && [ -f $RFCOMM_CONF ] ; then
		# rfcomm must always succeed for now: users
		# may not yet have an rfcomm-enabled kernel
		$RFCOMM -f $RFCOMM_CONF bind all || true
		echo -n " $RFCOMM_NAME"
	fi
}

stop_rfcomm()
{
	if [ -x $RFCOMM ] ; then
		echo -n " $RFCOMM_NAME"
		$RFCOMM unbind all || true
	fi
}

restart_rfcomm()
{
	if [ -x $RFCOMM ] && [ -f $RFCOMM_CONF ] ; then
		$RFCOMM unbind all || true
		$RFCOMM -f $RFCOMM_CONF bind all || true
		echo -n " $RFCOMM_NAME"
	fi
}

case "$1" in
  start)
	echo -n "Starting $DESC:"
	if [ "$USE_DSMETOOL" = "yes" ]; then
		dsmetool -n -1 -t "$HCID -n -x -s" || true
		echo -n " $HCID_NAME"
	else
		start-stop-daemon --start --quiet --exec "$HCID -x -s" || true
		echo -n " $HCID_NAME"
	fi
	start_hid || true
	start_rfcomm || true
	start_pan || true
	echo "."
	;;
  stop)
	echo -n "Stopping $DESC:"
	stop_pan || true
	stop_rfcomm || true
	stop_hid || true
	if [ "$USE_DSMETOOL" = "yes" ]; then
		dsmetool -k "$HCID -n -x -s" || true
		echo -n " $HCID_NAME"
	else
		start-stop-daemon --stop --quiet --exec "$HCID -x -s" || true
		echo -n " $HCID_NAME"
	fi
	echo "."
	;;
  restart|force-reload)
	echo -n "Restarting $DESC:"
	stop_hid || true
	stop_pan || true
	if [ "$USE_DSMETOOL" = "yes" ]; then
		dsmetool -k "$HCID -n -x -s" || true
		sleep 1
		dsmetool -n -1 -t "$HCID -n -x -s" || true
	else
		start-stop-daemon --stop --quiet --exec "$HCID -x -s" || true
		sleep 1
		start-stop-daemon --start --quiet --exec "$HCID -x -s" || true
	fi
	echo -n " $HCID_NAME"
	start_pan || true
	start_hid || true
	restart_rfcomm
	echo "."
	;;
  *)
	N=/etc/init.d/bluez-utils
	# echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
	echo "Usage: $N {start|stop|restart|force-reload}" >&2
	exit 1
	;;
esac

exit 0
