#! /bin/sh

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/bin/btcond
NAME=btcond
DESC="BT Connection Daemon"

test -x $DAEMON || exit 0

# Include btcond defaults if available
if [ -f /etc/default/btcond ] ; then
	. /etc/default/btcond
	if [ ! -z "$RUN_AS_USER" ]; then
		if [ "$USE_DSMETOOL" = "yes" ]; then
			USER_FLAGS="-U $RUN_AS_USER"
		else
			USER_FLAGS="--chuid $RUN_AS_USER"
		fi
	fi
fi

#set -e

case "$1" in
  start)
	echo -n "Starting $DESC: "
	if [ "$USE_DSMETOOL" = "yes" ]; then
		dsmetool -n -1 $USER_FLAGS -r "$DAEMON -l $DAEMON_OPTS"
	else
		start-stop-daemon --start --quiet $USER_FLAGS \
			--exec $DAEMON -- -d $DAEMON_OPTS
	fi
	echo "$NAME."
	;;
  stop)
	echo -n "Stopping $DESC: "
	if [ "$USE_DSMETOOL" = "yes" ]; then
		dsmetool -k "$DAEMON -l $DAEMON_OPTS"
	else
		start-stop-daemon --stop --quiet \
			$USER_FLAGS --exec $DAEMON
	fi
	echo "$NAME."
	;;
  restart|force-reload)
	$0 stop
	$0 start
	;;
  *)
	N=/etc/init.d/$NAME
	# echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
	echo "Usage: $N {start|stop|restart|force-reload}" >&2
	exit 1
	;;
esac

exit 0
