#! /bin/sh
# /etc/init.d/festival
#
# Init script for starting Festival as a system-wide server process.
# 
# ALT Linux verion of Igor Vlasenko <viy@altlinux.org>
#
# This file and all modifications and additions to the pristine
# package are under the same license as the package itself.
# 
# used pieces of suse init script 
# Copyright (c) 2006 Andreas Schneider <mail@cynapses.org>.
#
# note: as festival creates waveforms too, should it require(d)-start: sound?
### BEGIN INIT INFO
# Provides:          festival
# Required-Start:    $syslog $remote_fs sound
# Should-Start:      $time
# Required-Stop:     $syslog $remote_fs sound
# Should-Stop:       $time
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: festival daemon providing full text-to-speech system
# Description:       Start festival to allow applications to access a
#	text-to-speech system with various APIs, as well as an environment
#	for development and research of speech synthesis techniques. It is
#	written in C++ and has a Scheme-based command interpreter for
#	general control. 
### END INIT INFO
# compatible chkconfig entry (to allow backports)
#
# chkconfig: 35 64 36
# description: festival daemon providing full text-to-speech system
#
# processname: festival
# config: /etc/festival/server.scm
# pidfile: /var/run/festival.pid

WITHOUT_RC_COMPAT=1

# Source function library.
. /etc/init.d/functions

NAME=festival
PIDFILE=/var/run/festival.pid
LOCKFILE=/var/lock/subsys/festival
DAEMON=/usr/bin/festival
USER=_festival
CONFIG=/etc/festival/server.scm
OPTIONS=" --server -b $CONFIG"

# Source festival configuration.
SourceIfNotEmpty /etc/sysconfig/festival

test -x $DAEMON || exit 0

start_nopid()
{
    start-stop-daemon --start --background --startas /bin/su --exec $DAEMON --name $NAME -- -s /bin/sh -l $USER -c "exec /usr/bin/festival $OPTIONS"
#    start-stop-daemon --start --background --startas /bin/su --name $NAME -- -s /bin/sh -l $USER -c "exec /usr/bin/festival $OPTIONS"
#    start-stop-daemon --start --quiet $USER --background --make-pidfile --pidfile /var/run/festival.pid --exec $DAEMON -- --server -b /etc/festival/server.scm
	RETVAL=$?
	return $RETVAL
}

stop_nopid()
{
    echo -n "Stopping "
    action $NAME start-stop-daemon --stop --oknodo --exec $DAEMON
    RETVAL=$?
    return $RETVAL
}

start()
{
    start_daemon --lockfile "$LOCKFILE" --make-pidfile --pidfile "$PIDFILE" --set-user "$USER" -- $DAEMON "$OPTIONS"
    RETVAL=$?
    return $RETVAL
}

stop()
{
    stop_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user root --displayname $NAME su
    RETVAL=$?
    return $RETVAL
}

restart()
{
	stop
	start
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	reload)
		reload
		;;
	restart)
		restart
		;;
	condstop)
		if [ -e "$LOCKFILE" ]; then
			stop
		fi
		;;
	condrestart)
		if [ -e "$LOCKFILE" ]; then
			restart
		fi
		;;
	condreload)
		if [ -e "$LOCKFILE" ]; then
			reload
		fi
		;;
	status)
		status --pidfile "$PIDFILE" --expect-user root --displayname $NAME su
		RETVAL=$?
		;;
	*)
		msg_usage "${0##*/} {start|stop|reload|restart|condstop|condrestart|condreload|status}"
		RETVAL=1
esac

exit $RETVAL

