#!/bin/sh

default_libdir="/usr/lib/festival"
default_datadir="/usr/share/festival"

while true
	do
	case "$1" in
	-f ) festival="${2}"
		shift 2
		;;
	-l ) libdir="$2"
		shift 2
		;;
	-d ) datadir="$2"
		shift 2
		;;
	-- ) shift; break ;;
	* ) break;;
	esac
done

text=${1-"/usr/share/doc/festival/examples/benchmark.text"}

for i in /usr/bin . src/main ../src/main $HOME/projects/festival/src/main /cstr/bin
	do
	if [ -n "$festival" ]
		then
		break;
	fi
	if [ -x "$i/festival" ]
		then
		festival="$i/festival"
	fi
done

[ -n "$festival" ] ||
	{
	echo "Can't find festival"
	exit 1
	}

if [ -z "$libdir" ]
	then
	case $festival in 
	*main/festival )	libdir=`dirname $festival`/../../lib;;
	* ) libdir=$default_libdir;
	esac
fi

if [ -z "$datadir" ]
	then
	case $festival in 
	*main/festival )   datadir=`dirname $festival`/../../share;;
	* ) datadir=$default_datadir;;
	esac
fi

echo Using $festival

start_flag_file="/tmp/fest_start_$$"
end_flag_file="/tmp/fest_end_$$"
script="/tmp/fest_script_$$"

echo -n > $flag_file;

cat > $script <<__END__

(set! libdir "$libdir/")
(set! datadir "$datadir/")
(set! lexdir "$default_datadir/dicts/")
(set! voiced_dir "$default_datadir/voices/")

(load (string-append datadir "init.scm"))
(if (probe_file (format nil "%s/.festivalrc" (getenv "HOME")))
    (load (format nil "%s/.festivalrc" (getenv "HOME"))))


(audio_mode 'async)
(set! tts_hooks (list utt.synth))

(puts "start...\n" nil)
(fclose (fopen "$start_flag_file" "w"))

(tts_file "$text" (quote text))

(fclose (fopen "$end_flag_file" "w"))
(puts "...end\n" nil)
(audio_mode 'close)

(quit)

__END__

eval $festival --script $script

perl -e 'print "running time = ", (stat($ARGV[1]))[8]-(stat($ARGV[0]))[8], " seconds\n";' $start_flag_file $end_flag_file

/bin/rm -f $start_flag_file $end_flag_file $script
	
