#!/bin/sh
#
# TiMidity      /etc/init.d/ initscript for TiMidity++
#               $Id: timidity.init,v 1.6 2004/09/30 01:04:04 hmh Exp $
#
#               Copyright (c) 2004 by Henrique M. Holschuh <hmh@debian.org>
#               Distributed under the GPL version 2
#

PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/bin/timidity
DESC="TiMidity++ ALSA midi emulation"
PMIDI=/usr/bin/pmidi
PIDFILE=/var/run/timidity.pid

set -e

test -x ${DAEMON} || exit 0
test -x ${PMIDI} && pmidi_enabled="true" || pmidi_enabled="false";

TIM_ALSASEQ=
TIM_ALSASEQPARAMS="-B2,8"
[ -r /etc/default/timidity ] && . /etc/default/timidity
[ "${TIM_ALSASEQ}" != "true" ] && exit 0
PARAMS="${TIM_ALSASEQPARAMS} -iAD"

START="--start --quiet --exec ${DAEMON} --pidfile ${PIDFILE} -- ${PARAMS}"

case "$1" in
  start)
	echo -n "Starting ${DESC}: "
	[ -d /proc/asound ] || {
  		echo "(ALSA is not active, cannot start)"
		exit 0
	}
	if start-stop-daemon ${START} >/dev/null; then
		echo "timidity."
  		if [ $pmidi_enabled = "true" ] ; then
  			sleep 1
  			echo -n "Emulating midi on ports: ";
			pmidi -l | grep "TiMidity" | cut -f 1 -d ' ' | xargs
		fi
	else
		if start-stop-daemon --test ${START}  >/dev/null 2>&1; then
			echo "(failed)."
			exit 1
		else
			echo "already running."
			exit 0
		fi
	fi
	;;
  stop)
  	echo -n "Stopping $DESC: "
	if start-stop-daemon --stop --quiet --pidfile ${PIDFILE} \
	   --exec ${DAEMON} --retry 10 ; then
		echo "timidity."
	else
		echo "(failed)."
		exit 1
	fi
	;;
  restart|force-reload)
  	$0 stop
	exec $0 start
  	;;
  *)
    echo "Usage: $0 {start|stop|restart|force-reload}" >&2
    exit 1
esac
 
exit 0						      
