#!/afs/athena/contrib/perl/perl
#
# tompalka
#
# try to mimic "klook".
#

open(ans,"/afs/athena/astaff/project/eolcdev/ans/ans.compiled") || die "couldn't open ans.txt: $!\n";

# check what program we're running.
# if "getsa", then display the answers
# if "klook", then display just the titles and the respective number
# if "shans", then display the answer for a specific counter number

$shans = 1 if ( grep(/shans/,$0 ));
$klook = 1 if ( grep(/klook/,$0 ));
$getsa = 1 if ( grep(/getsa/,$0 ));

if ( ! ($klook || $shans || $getsa ))
{
    die "don't know how to run that.\n";
}
    

$keyword = $ARGV[0];
$found = 0;
$counter = -1;


while( <ans> )
{

    if ( (! $found) && grep(/^!/,$_) )
    {
	$counter++;

	if ( grep(/$keyword/,$_) || ( $shans && ($counter == $keyword ) ))
	{
	    $found = 1;
	    next;
	}
    }

    if ( $found )
    {
	if ( grep(/^\./,$_) )
	{
	    print "\n\n--------------------------------------------------\n\n";
	    $found = 0;

	    if ( $shans )
	    {
		close(ans);
		exit;
	    }
	}
	else 
	{
	    if ( $klook )
	    {
		printf "%4d %s", $counter, $_;
		$found = 0;
		next;
	    }
	    else			# $shans is true
	    {
		print;
	    }
	}
    }
}

close(ans);

exit;
