#!/usr/athena/bin/perl

$viewdir = "/mit/mkgray/www/z/";

open(Z, ">$viewdir/index.html");
select(Z);

$windowsize = 100;
$freqthresh = 30;
$pat = "Instance:";
if($ARGV[0] eq "-sipb"){
    shift;
    $pat = "Instance:";
    open(ZB, "/mit/mkgray/afs/sipb.baseline");
}
else{
    open(ZB, "/mit/mkgray/afs/wm.baseline");
}
while(<ZB>){
    ($word, $freq) = split;
    $bl{$word} = $freq;
}

while(<>){
    if(/^$pat/){
	$from =~ /Instance: (.+) Time:/;
	$instance = $1;
	$from =~ /\<([^>]+)>$/;
	$sender = $1;
	&log($instance, "$from\n$zeph");
	&analyze("$from\n\n\nuser:$sender instance:$instance\n$zeph");
	$from = $_;
	$totzeph++;
	/Time: (.+)Host:/;
	$time = $1;
	$date= "";
	$zeph = "";
	next;
    }
    if(/^From:/){
	$waitforblank = 1;
	$from .= $_;
	next;
    }
    elsif($waitforblank){
	if(/^$/){
	    $waitforblank = 0;
	}
	else{
	    $from .= $_;
	}
    }
    else{
	$zeph .= $_;
    }
}
$ct = 7;
for (1..25){
    &analyze("");
}
print "\n\nDisplayed $displayed out of $totzeph total zephyrs\n";

sub analyze {
    $z = shift;
    $oz = $z;
    $anct++;
    $*=1;
    ($from, $z) = split("\n\n\n", $z);
    $z =~ s/\n\r/ /g;
    $z =~ y/A-Z/a-z/;
    @w = split(" ", $z);
    foreach $w (@w){
	print "Adding word $w\n" if $keyscan;
	$wct{$w}++;
	$tot++;
    }
    $oz =~ s/\n\n//;
    push(@curz, $oz);
    push(@timedate, "$time $date");
    if($#curz > $windowsize){
	$x = shift(@curz);
	($from, $x) = split("\n\n", $x);
	shift(@timedate);
	$x =~ s/\n\r/ /g;
	$x =~ y/A-Z/a-z/;
	@w = split(" ", $x);
	foreach $w (@w){
	    $wct{$w}--;
	    $tot--;
	}
    }
    $ct++;
    if($ct%20 == 0){
	return unless ($#curz == $windowsize);
	$ct2=0;
	@keys = ();
	for $w (sort {$wct{$b} <=> $wct{$a}; } keys %wct){
	    $act = int(10000*$wct{$w}/$tot);
	    print "Scanning keyword $w $act\n" if $keyscan;
	    print "Turbines: ", $wct{"turbines"}, "\n" if $keyscan;
	    print "Turbine: ", $wct{"turbine"}, "\n" if $keyscan;
	    $act = 0 if($wct{$w} == 1);
	    last if ($act < $freqthresh);
	    if($act > 10*$bl{$w}){
		if($bl{$w}){
#		    print "$w ($act, $bl{$w}) ", $act/$bl{$w}, "\n";
		}
		else{
#		    print "$w ($act)\n";
		}
		push(@keys, $w);
	    }
	}
	$*=1;
	$best = "";
	%segment = ();
	$maxhits = 0;
	$zoff = 0;
	$tothits = 0;
	$zct = 0;
	foreach $zeph (@curz){
	    $hits = 0;
	    foreach $k (@keys){
		$prehits = ($zeph =~ s/\b\Q$k\E\b/$k/g);
		$prehits = 2 if ($prehits > 2);
		$hits += $prehits;
	    }
	    $segment{$zeph} = $hits;
	    $tothits += $hits;
	    $zct++;
	    if($hits > $maxhits){
		$maxhits = $hits;
		$best = $zeph;
		$off = $zoff;
	    }
	    $zoff++;
	}
	if($maxhits > 2){
#	    print "***", join(" ", @keys), "***\n";
	    print "<h2>Conversation</h1>";
	    for $k (@keys){
		if(!$topics{$k}){
		    print "<font color=red>$k</font> ";
		    $topics{$k} = 1;
		}
		else{
		    print "$k ";
		}
	    }
	    print "<hr>\n";
	    $avg = $tothits/$zct;
	    $curzct=0;
	    for $zeph (@curz){
		if((($zeph eq $best) ||
		   $segment{$zeph} > (2.75*$avg))
		   && (!$seen{$zeph})){
		    print "<table><tr><td bgcolor=black><font color=white>\n";
		    print "<h2>$timedate[$curzct]</h2>\n";
		    $zeph =~ s/\n\n.+\n/\n\n/;
		    $zeph =~ s/^/\t/g;
		    $htz = $zeph;
		    $htz =~ s/Instance: (.+) Time:/Instance: <a href=plchldr.html><font color=red>\1<\/font><\/a> Time:/;
		    $htz =~ /color=red>([^<]+)</;
		    $in = $1;
		    while($in =~ /\.d$/){
			chop($in);
			chop($in);
		    }
		    $htz =~ s/plchldr/$in/;
		    $htz =~ s/From: .+\<(.+)\>$/From: <font color=yellow>\1<\/font>/;
		    $htz =~ s/\n/<br>\n/g;
		    print "$htz";
		    print "</td></tr></table>\n";
		    $seen{$zeph} = 1;
		    $displayed++;
		}
		else{
#		    print "--- $timedate[$curzct]\n";
		}
		if($zeph eq $best){
		    $lbest = $best;
		    %topics = ();
		}
		$curzct++;
	    }
	}
	else{
	    $newtopics = "";
	    foreach $k (@keys){
		if($bl{$k}){
		    $newtopics = "$k+ " unless $topics{$k};
		}
		else{
		    $newtopics = "$k " unless $topics{$k};
		}
		$topics{$k}=1;
	    }
	    print "<h2>New topics: <font color=green>$newtopics</font></h2><hr>\n" if($newtopics ne "");
#	    print "((", join(" ", @keys), "))\n";
	}
    }
}

sub log {
    my $i = shift;
    my $zeph = shift;

    while($i =~ /\.d$/){
	chop($i);
	chop($i);
    }

    if($opened{$i}){
	open(L, ">>$viewdir/$i".".html");
    }
    else{
	open(L, ">$viewdir/$i".".html");
	$opened{$i} = 1;
    }
    $*=1;
    print L "<table><tr><td bgcolor=black><font color=white>\n";
    print L "<h2>$timedate[$curzct]</h2>\n";
#    $zeph =~ s/\n\n.+\n/\n\n/;
    $zeph =~ s/^/\t/g;
    $htz = $zeph;
    $htz =~ s/Instance: (.+) Time:/Instance: <font color=red>\1<\/font> Time:/;
    $htz =~ s/From: .+\<(.+)\>$/From: <font color=yellow>\1<\/font>/;
    $htz =~ s/\n/<br>\n/g;
    print L "$htz";
    print L "</td></tr></table><br>\n";

}
