#!/usr/local/bin/perl 

require 5.001;
use FileHandle;

$filename = "base";

$firstword = shift;
$looking = 0;

if( $firstword ne "" )
{
	print "Looking for $firstword\n";
	$looking = 1;
}

unless( open( RULES, "$filename" ) )
{
	print STDERR "Can't open filename: $!\n";
	return(1);
}

open( TEXT2PHONE, "|text2phone -d -n" );
TEXT2PHONE->autoflush( 1 );

MAINLOOP:
while( $in = <RULES> )
{
	$nlines++;
		
	# Eliminates end of line
	chop;
	
	# Eliminates lines without a comment
	
	if( $in =~ /^.*##(.*)$/ )
	{
		$in = $1;
		
		@words = split( ' ', $in );
	 
		foreach $word ( @words )
		{
			if( $looking )
			{
				if( $firstword eq $word )
				{
					$looking = 0;
				}
			}
			
			if( ! $looking )
			{
				do
				{
					print "[$word]\n";
					print "press ()next (a)gain (q)uit...\n";
					print TEXT2PHONE "$word\n";
					
					$wait = <STDIN>;
					chop $wait;
					last MAINLOOP if( $wait eq "q" );
					if( $wait eq "a" )
					{
						close( TEXT2PHONE );
						open( TEXT2PHONE, "|text2phone" );
						TEXT2PHONE->autoflush( 1 );
					}
				}
				until( $wait eq "" );
			}
		}
	}
}

close( TEXT2PHONE );

close( RULES );
