### BEGIN INIT INFO
# Provides:				lepresenced
# Required-Start:		networking
# Required-Stop:		networking
# Default-Start:		2 3 4 5
# Default-Stop:			0 1 6
# Short-Description:	lepresenced - presenced for Bluetooth LE devices.
# Description:			lepresenced
#						checks for one or multiple bluetooth *low energy*
#						devices for their presence state and reports it
#						to the 73_PRESENCE.pm module.
### END INIT INFO

BLUETOOTH_DEVICE="hci0"
LISTEN_ADDRESS="0.0.0.0"
LISTEN_PORT="5333"
SYSLOG_LEVEL="LOG_WARNING"

if test -f /etc/default/lepresenced; then
    . /etc/default/lepresenced
fi

. /lib/lsb/init-functions

# Process name ( For display )
NAME=lepresenced
DAEMON=/usr/sbin/lepresenced
PIDFILE=/var/run/lepresenced.pid
LEPRESENCED_OPTS="--daemon --device $BLUETOOTH_DEVICE --listenaddress $LISTEN_ADDRESS --listenport $LISTEN_PORT --loglevel $SYSLOG_LEVEL"

test -x $DAEMON || exit 5

case $1 in
	start)
		if [ -e $PIDFILE ]; then
			status_of_proc -p $PIDFILE $DAEMON "$NAME process" && status="0" || status="$?"
			if [ $status = "0" ]; then
				exit # Exit
			fi
		fi
		log_daemon_msg "Starting the process" "$NAME"
		if start-stop-daemon --start --quiet --oknodo --pidfile $PIDFILE --exec $DAEMON -- $LEPRESENCED_OPTS; then
			log_end_msg 0
		else
			log_end_msg 1
		fi
	;;
	stop)
		if [ -e $PIDFILE ]; then
			status_of_proc -p $PIDFILE $DAEMON "Stoppping the $NAME process" && status="0" || status="$?"
			if [ "$status" = 0 ]; then
				start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE
				/bin/rm -rf $PIDFILE
			fi
		else
			log_daemon_msg "$NAME process is not running"
			log_end_msg 0
		fi
	;;
	restart)
		$0 stop && sleep 2 && $0 start
	;;
esac