#!/bin/sh

### BEGIN INIT INFO
# Provides:          mt-daapd
# Required-Start:    $local_fs $remote_fs $network $time avahi
# Required-Stop:     $local_fs $remote_fs $network $time
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Multithreaded DAAP music server 
# Description:       mt-daapd, a.k.a. Firefly Media Server, is what
#                    most people will understand to be an iTunes share
#                    server. It uses the DAAP protocol, as iTunes does,
#                    and supports streaming MP3 and AAC natively. It can
#                    make use of a number of conversion methods to expose
#                    Ogg and FLAC files too.
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/mt-daapd
NAME=mt-daapd
PIDFILE=/var/run/$NAME.pid
DESC=mt-daapd
SCRIPTNAME=/etc/init.d/$NAME
DAEMON_ARGS=""
[ -x "$DAEMON" ] || exit 0

# Include mt-daapd defaults if available
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions

do_stop() {
		start-stop-daemon --stop --quiet --pidfile $PIDFILE --name $NAME --signal 2 --exec $DAEMON  --retry=TERM/30/KILL/5
		RETVAL="$?"
		[ "$RETVAL" = 2 ] && return 2
		start-stop-daemon --stop --quiet --pidfile $PIDFILE --name $NAME --signal 2 --exec $DAEMON --oknodo --retry=0/30/KILL/5
		[ "$?" = 2 ] && return 2
		rm -f $PIDFILE
		return "$RETVAL"
}

do_start() {
		start-stop-daemon --start --quiet -m --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
		|| return 1
		start-stop-daemon --start --quiet -m --pidfile $PIDFILE --exec $DAEMON -- \
		$DAEMON_ARGS \
		|| return 2
}

#
# Function that sends a SIGHUP to the daemon/service
#
do_reload() {
		#
		# If the daemon can reload its configuration without
		# restarting (for example, when it is sent a SIGHUP),
		# then implement that here.
		#
		start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME
		return 0
}
case "$1" in
		start)
		[ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
		do_start
		case "$?" in
				0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
				2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
		esac
		;;
		stop)
		[ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
		do_stop
		case "$?" in
				0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
				2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
		esac
		;;
		reload|force-reload)

		# If do_reload() is not implemented then leave this commented out
		# and leave 'force-reload' as an alias for 'restart'.

		log_daemon_msg "Reloading $DESC" "$NAME"
		do_reload
		log_end_msg $?
		;;
		restart)
		log_daemon_msg "Restarting $DESC" "$NAME"
		do_stop
		case "$?" in
				0|1)
				do_start
				case "$?" in
						0) log_end_msg 0 ;;
						1) log_end_msg 1 ;;
						*) log_end_msg 1 ;;
				esac
				;;
				*)
				log_end_msg 1
				;;
		esac
		;;
		*)
		echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
		exit 3
		;;
esac

:
