#!/usr/athena/bin/perl

system ("attach shakespeare");
open (TRAG, "/mit/shakespeare/tragedy") ||die "Bad";
open (HIST, "/mit/shakespeare/history") ||die "No good";
open (COMD, "/mit/shakespeare/comedy") ||die "Nope";
open (SONN, "/mit/shakespeare/sonnets") ||die "Can't open";
@dirs = ("tragedy", "comedy", "history", "sonnets");

#Need to do:
#	Set up associative array based on data in /mit/shakes*/*/README
#		to serve as a "nameserver" for the plays.
#	Usage description.
#	Clean up output.
#	Provide -p option:  search specific play.
#	Provide -s option:  search for speaker.
#	Provide -pl option: lookup play, act, scene, line
#	Set it to ignore line breaks...
#	Print off entire ten line block containing phrase.

$lookup = 0;
$pplay =0;

#       This section will parse arguments
while($_=$ARGV[0], /^-/) {
    shift;
    if(/^-p(.*)/) {
	$pplay = 1;
	$pstr = $1;
    }
    if(/^-l/) {
	$lookup = 1;
    }
    if(/^-P(.*)/) {
	$lplay = $1;
    }
    if(/^-A(.*)/) {
	$lact = $1;
    }
    if(/^-S(.*)/) {
	$lscene = $1;
    }
    if(/^-L(.*)/) {
	$lline = $1;
    }
}
print("Searching plays (all): $pstr");

foreach (@dirs) {
    $foundit = 0;
    opendir (CDIR, "/mit/shakespeare/$_");
    $dir =  $_;
    #print "Examining category: $dir\n";
    @files = sort grep (/\w+\d\.txt/, readdir(CDIR));
    #print join(' ',@files),"\n";
    open (RM, "/mit/shakespeare/$dir/README") || die "No README file in $dir";
    print "Please wait, setting up NameServer\n";
    while (<RM>) {
	print "Here\n";
	(@temp) = split;
	if ($foundit) {
	    $filename = pop(@temp);
	    #print $filename;
	    $title = join (' ', @temp);
	    @filparts = split (/\./, $filename);
	    #print "Filep0 = $filparts[0]";
	    #print "Filep1 = $filparts[1]";
	    foreach $num (1..5) {
		$files = $filparts[0].$num.'.txt';
		$NMSRV{$files} = $title;
		#print "NMSRV($files) = $title\n";
	    }
	}
	if ($temp[7] eq "filenames,") {	# Needs to be fixed to handle things
				# better.  Particularly sonnets
	    $foundit = 1;
	    #print "Found It!, $temp[8]";
	}
    }
    efiles : foreach (@files) {
	print "Examining file: $_\n";
	open (FI, "/mit/shakespeare/$dir/$_") || die "Couldn't open";
	$play = $_;
	$notthisone = 0;
	$notthisone = 1 if (($NMSRV{$play} !~ /$pstr/i)&&$pplay);
	print("$play:$NMSRV{$play}:$pstr:  ");
	print("$notthisone..");
	$notthisone = 1 if ((($NMSRV{$play} !~ /$lplay/i)||($play !~ /$lact/))&&$lookup);
	print("$notthisone  ");
	$arg = join (' ', @ARGV);
	sub usage {
	    print STDERR "Usage: $0  -whatever whatever whatever";
	    exit 1;
	}

while (<FI>) {
    last if $notthisone;
    if($lookup&&($play =~ /$lact/)) {
	system("more +/$lact\/$lscene\/$lline $play");
	die("Done.");
    }
    $ok = 0;
    $speakc++;
    (@line) = split;
    if (!(/[a-z]/)) {
	$speaker = $_;
	($ok = 1) if($speaker =~ /END\./);
	$speakc =0;
	@speakblok=();
	$speakblok[$speakc] = $_;
    }
    else {
	if ($line[$#line] =~ m#[1-9]/[0-9]+/[0-9]+#) {
	    @place = split(/\//,$line[$#line]);
	    $act = $place[0];
	    if ($scene ne $place[1]) { print "Scanning Scene $place[1]\n"; 
	    }
	    $scene = $place[1];
	    $line = $place[2];
	    
#	    if (!$gotit) {
#		@linea = ();
#		@linea = @speakblok;
#	    }
#	    $linec = $#linea +1;
	    $ok = 1;
	}
    }
    if ($gotit) {
        $linec++;
	$linea[$linec] = $_;}
	else { 
	    $speakblok[$speakc] = $_;}
    


    $occ = $_;
    if (/$arg/i) {
	if (!$gotit) {
	    $rspeaker = $speaker;
	$gotit= 1;
      	@linea = ();
       	@linea = @speakblok;
        $linec = $#linea;
	print "Linec:  $linec";
	$linea[$linec]=$occ;
	print "Found an instance of $arg\n";
	print "\t$_";
	chop($linea[$linec]);
	$linea[$linec] = "[4m".$linea[$linec]."[0m";
#	$linea[$linec+2] = "_" x length;
#	$linea[$linec+1] = "" x length;
	$linea[$linec] .= "\n";
	$linec = $linec + 1;
	}		  
	else	   
	{
	$linea[$linec] = "[4m".$linea[$linec]."[0m";
    }

    }

    if ($gotit && ($ok)) {
        $gotit = 0;
        $ulin = $line +10;
        foreach $l (0..$#linea) {
            print "$linea[$l]";
        }
        print "\n\n\n\n\n";
        print "***************************************\n";
        print "First Speaker: $rspeaker";
        print "Play: $NMSRV{$play}\n";
        print "Act :$act \n";
        print "Scene: $scene \n";
        print "****************************************\n";
        print "Press return to continue\n";
        $pause = <STDIN>;
        if ($pause eq "q") {die;}
        system ("clear");
        @linea = ();
    }

}
}


}

close (FI);















