#!/bin/bash

#   FILE: speak -- Festival will read the file which is the first command 
#         line argument.
# AUTHOR: W. Michael Petullo <speak@flyn.org>
#   DATE: 19 SEP 1998

ESD=`which esd`

if test "$#" = 0; then
	echo "Usage: $0 filename."
	exit
fi

if [ -f /tmp/be_nice ]; then
	# Don't want to take up CPU cycles.
	# echo "CPU intensive process in progress, exiting."
	exit
fi

if [ -f /tmp/speaking/$1 ]; then
	# Another process is already trying to speak this file, give up.
	exit
fi

if [ ! -d /tmp/speaking ]; then
	mkdir /tmp/speaking
else
	# Another process is speaking, wait until it is done.
	while [ -d /tmp/speaking ]; do
	        sleep 60
	done
fi

dir=`dirname $1`
mkdir -p /tmp/speaking/$dir
touch /tmp/speaking/$1

if [ -r $1 ]; then
	if /sbin/fuser $ESD | grep -c '[0123456789]' > /dev/null; then
		esddsp festival --tts $1
	else
		festival --tts $1
	fi
fi

rm -rf /tmp/speaking
