#! /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
HCID_OPTIONS="-x -s"

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

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

set -e

case "$1" in
  start)
	if [ -e /tmp/first-boot-flag-tmp ]; then
		if [ -x "/usr/bin/osso-product-info" ]; then
			HW=`osso-product-info -qOSSO_PRODUCT_HARDWARE`
		else
			HW="unknown"
		fi

		if [ "$HW" = "RX-48" ]; then
			rm -f /etc/bluetooth/audio.service
		fi
	else
	    echo -n "Starting $DESC:"
	    if [ "$USE_DSMETOOL" = "yes" ]; then
		    dsmetool -n -1 -t "$HCID -n $HCID_OPTIONS" || true
		    echo -n " $HCID_NAME"
	    else
		    start-stop-daemon --start --quiet --exec "$HCID $HCID_OPTIONS" || true
		    echo -n " $HCID_NAME"
	    fi
	fi
	echo "."
	;;
  stop)
	echo -n "Stopping $DESC:"
	if [ "$USE_DSMETOOL" = "yes" ]; then
		dsmetool -k "$HCID -n $HCID_OPTIONS" || true
		echo -n " $HCID_NAME"
	else
		start-stop-daemon --stop --quiet --exec "$HCID $HCID_OPTIONS" || true
		echo -n " $HCID_NAME"
	fi
	echo "."
	;;
  restart|force-reload)
	echo -n "Restarting $DESC:"
	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 $HCID_OPTIONS" || true
		sleep 1
		start-stop-daemon --start --quiet --exec "$HCID $HCID_OPTIONS" || true
	fi
	echo -n " $HCID_NAME"
	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
