#! /bin/sh
### BEGIN INIT INFO
# Provides:          bemused-server
# Should-Start:      mpd bluetooth $syslog
# Required-Start:    $bluetooth
# Required-Stop:     $bluetooth
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start a bemused server
### END INIT INFO

#
# python bemused mpd client bridge by Dub Spencer
#
# to install: update-rc.d bemused-mpc defaults 90 10
#

DAEMON=/usr/bin/pbmpcd
NAME=pbmpcd
DESC=bemused-mpc
PIDFILE=/var/run/pbmpcd.pid

. /lib/lsb/init-functions

test -x $DAEMON || exit 0

set -e

# Read configuration variable file if it is present
[ -r /etc/default/$DESC ] && . /etc/default/$DESC

case "$1" in
    start)
	if [ "$START_PBMPCD" != "true" ]; then
	    log_failure_msg "Not starting $DESC: disabled by /etc/default/$DESC."
	    log_end_msg 0
	    exit 0
	fi

	log_begin_msg "Starting bemused-mpc..."
	if [ -s $PIDFILE ] && kill -0 $(cat $PIDFILE) >/dev/null 2>&1; then
	    log_warning_msg "apparently already running"
	    exit 0
	fi
 
	start-stop-daemon --start --quiet --chuid $USERID:$GROUPID \
		--pidfile $PIDFILE --make-pidfile \
		--background --exec $DAEMON -- $FLAGS
	echo "$NAME."
	;;
    stop)
	if [ ! -f $PIDFILE ]; then
	    log_warning_msg "Not stopping bemused-mpc: no pidfile found."
	    exit 0
	fi
	
	log_daemon_msg "Stopping" "bemused-mpc"
	log_end_msg 0
	start-stop-daemon --stop --oknodo --quiet \
		--pidfile $PIDFILE
	rm -f $PIDFILE
	;;
    restart|force-reload)
	log_daemon_msg "Restarting" "bemused-mpc"
	log_end_msg 0
	start-stop-daemon --stop --oknodo --quiet \
		--pidfile $PIDFILE
	rm -f $PIDFILE
	start-stop-daemon --start --quiet --chuid $USERID:$GROUPID \
		--pidfile $PIDFILE --make-pidfile \
		--background --exec $DAEMON -- $FLAGS
	;;
    *)
	echo "Usage: $0 {start|stop|restart}" >&2
	exit 1
	;;
esac

exit 0
