#!/bin/sh
args=""
opts="+q -a"
zero=false
notopt=false
getarg=false
aaprompts=/usr/share/aaprompts

function rsynth
{
	echo -n >$1.index
	echo -n >$1.data
	while read word ; do
		say $opts -o /tmp/tmpword.au $word
		sox /tmp/tmpword.au /tmp/tmpword.ul
		first=`wc -c <$1.data`
		cat /tmp/tmpword.ul >>$1.data
		last=`wc -c <$1.data`
		echo "$word $first $last" >>$1.index
		rm /tmp/tmpword.au
		rm /tmp/tmpword.ul
	done
}

while test ! -z "$1" ; do
	if test $getarg = true ; then
		getarg=false
		opts="$opts$1"	
	elif test $notopt = true ; then
		if test ! -z "$args" ; then
			echo "use: rswords [options] table <wordlist"
			exit -1
		fi
		args=$1
	else case "$1" in
	-z)
		zero=true
		;;
	-t)
		aaprompts="../aaprompts"
		;;
	-c | -F | -f | -x | -p | -j | -S | -K | -d)
		opts="$opts $1"
		getarg=true
		;;
	-c* | -F* | -f* | -x* | -p* | -j* | -S* | -K* | -d*)
		opts="$opts $1"
		;;
	-*)
		echo rswords: unsupported option "($1)"
		exit -1
		;;
	*)
		notopt=true
		if test ! -z "$args" ; then
			echo "use: rswords [options] table <wordlist"
			exit -1
		fi
		args=$1
		;;
	esac ; fi
	shift
done

if test -z "$args" ; then
	echo "use: rswords [options] table <wordlist"
	exit -1
fi

grep -v "^#" | tr '[:lower:]' '[:upper:]' | sort -f | uniq | grep -v "^$" | rsynth $aaprompts/$args
