#! /bin/sh
#
# bluez-pan    Bluetooth DUN and PAN daemons starting and stopping
#
# Edd Dumbill <edd@usefulinc.com>

# IMPORTANT NOTE
#
# Don't edit this file, use /etc/default/bluez-pan
# to enable and configure how dund and pand get run

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

DUND_DAEMON=/usr/bin/dund
DUND_NAME=dund
PAND_DAEMON=/usr/bin/pand
PAND_NAME=pand

DUND_ENABLED=0
PAND_ENABLED=0
DUND_OPTIONS=""
PAND_OPTIONS=""

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

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 "$PAND_ENABLED" = "0" && \
	test "$DUND_ENABLED" = "0"; then
	exit
fi

set -e

start_daemons()
{
	echo -n "Starting $DESC:"
	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
	echo "."
}

stop_daemons()
{
	echo -n "Stopping $DESC: "
	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
	echo "."
}

case "$1" in
  start)
		start_daemons
	;;
  stop)
		stop_daemons
	;;
  restart|force-reload)
		stop_daemons
		sleep 1
		start_daemons
	;;
  *)
	N=/etc/init.d/bluez-pan
	echo "Usage: $N {start|stop|restart|force-reload}" >&2
	exit 1
	;;
esac

exit 0
