#!/usr/bin/perl 
##eval "exec perl -S  $0 $*"
##                         if $running_under_some_shell;
# 
# Martin & Jon     5-25-93
# Matt 12Oct94 added septexexpand 
# Matt 26Apr95 give relative path from Adm directory
#	       find additional plot types, handle figs that are commented out
#       4may95 added listing .warning files
#	4may95 translation into perl
#	       added tabulated statistics
#	       checks with FIGLIST
#	       works on an individual directory
# Joel 31aug95 change to GNU make
#
# keywords report make redoc

$usage = "Usage: $0 - \n";
$selfdoc = "
Resstat computes result statistics for reports and books
Resstat has to be run in the chapter/article directory
See also: Execstat Resstat Wastestat Tour Grepper\n";

if( $#ARGV < 0 ||$#ARGV > 1 ) {
  print $usage;
  die $selfdoc;
  }

$doctype = "paper";
$makecmd = "gmake";

print "Gathering the plot statements from the paper.tex files\n";

open(TEXINPUT,"septexexpand < $doctype.tex |");

@plotlist = (plot,passiveplot,activeplot,activesideplot,passivesideplot,activeplottop,activeplotbottom,activeplotpage);

while($texentry=<TEXINPUT>){
  $texentry=~ s/%.*$//;
  foreach $plottype (@plotlist) {
    if ($texentry =~ /$plottype/) {
      $texentry =~ s/\\//;
      $texentry =~ s/{/ /;
      $texentry =~ s/}.*//;
      $texentry =~ s/\n//;
      $texstem  =  $texentry;
      $texstem  =~ s/.* //;
      $restype  =  $texentry;
      $restype  =~ s/ .*//;
      if ($tex{$texstem}) {
        $tex{$texstem} = join(":",$tex{$texstem},$restype);
        }
      else {
        $tex{$texstem} = $restype;
        }
      }
    }
  }
close(TEXINPUT);
@texstems = sort keys(%tex);

#this would print the stem of all results plotted in paper
#foreach $texstem (sort keys(%tex)) {
#  printf "%20s %20s\n", $texstem, $tex{$texstem};
#  }

print "Gathering the RESULTS[ECN]R information from the makefiles\n";

$reslist = `$makecmd RESULTSALL.varvalue` ; # if error die "make returned errors.";

@tmp = split(" ",$reslist);

while($resname = pop(@tmp)) {
  $make{$resname}++;
  }
@makestems = sort keys(%make);

# check if reasonable list
foreach $makestem (sort keys(%make)) {
  if ($make{$makestem} > 1 ) {
    print "MAKEFILE : *** Multiple RESULTS[ECN]R entries for $makestem *** \n";
    }
  }

#this plots the stem of all results listed in FIGLIST
#foreach $makestem (sort keys(%make)) {
#  printf "%20s %20s\n", $makestem, $make{$makestem};
#  }
#exit;
print "Gathering the result file information from the result directories\n";

$resdir = `$makecmd RESDIR.varvalue` ; # if error die "make returned errors.";
$resdir =~ s/\n//g; # remove newlines

if (opendir(RESDIR,"$resdir")) {
  @resfiles = sort readdir(RESDIR);
  close(RESDIR);
}

@plottype = ('ps','v','v3','A','warning');
foreach $stemfile (@resfiles) {
  $stem = $stemfile;  # we must make a copy to keep from altering resfiles
  foreach $plottype (@plottype) {
    $stem =~ s/\.$plottype$//;
  }
  if ($stem ne "." && $stem ne ".." && $stem ne "YMTRANS.TBL") { $files{$stem}++;}
}
@filestems = sort keys(%files);

foreach $stem (@filestems) {
  foreach $plottype (@plottype) {
    if (grep(/^$stem\.$plottype$/,@resfiles)) {
      $files{$stem} = join(":",$files{$stem},"$plottype");
    }
    else {
      $files{$stem} = join(":",$files{$stem},".");
    }
  }
}

###this lists the stem of all files in RESDIR
#foreach $filestem (@filestems) {
#  printf "%20s %20s\n", $filestem, $files{$filestem};
#  }
#exit;

print "RESDIR: following files have no known result suffix:\n";
foreach $filestem (@filestems) {
  @attributes = split(":",$files{$filestem});
  $resnumber = shift(@attributes);
  foreach $attribute (@attributes) {
    if ($attribute ne "." && $attribute ne "YMTRANS.TBL") {
      $resnumber--; 
      }
    }
    if ($resnumber > 0) {
      print "/bin/rm $resdir/$filestem\n";
    }
  }
print "\n";

print "Checking for consistency\n";

# diff reslist resfiles -> resfiles are missing  (should only use ps entries)
#			-> reslist entry missing
print "\n";
print "Following results are listed in RESULTS[ECN]R but do not exist in RESDIR:\n";
foreach $makestem (@makestems) {
  if ( !(join(" ",@filestems) =~ /\b$makestem\b/)) {
    print "$makestem\n";
    }
  }
print "Following results exist in RESDIR but are not listed in RESULTS[ECN]R:\n";
foreach $filestem (@filestems) {
  if ( !(join(" ",@makestems) =~ /\b$filestem\b/)) {
    print "/bin/rm $resdir/$filestem*\n";
    }
  }

# diff reslist resplot  -> reslist files not used in paper
#			-> paper requires additional reslist entries
print "Following results exist in RESULTS[ECN]R but are not used in $doctype:\n";
foreach $makestem (@makestems) {
  if ( !(join(" ",@texstems) =~ /\b$makestem\b/)) {
    print "$makestem\n";
    }
  }
print "Following results are used in $doctype but do not exist in RESULTS[ECN]R:\n";
foreach $texstem (@texstems) {
  if ( !(join(" ",@makestems) =~ /\b$texstem\b/)) {
    print "$texstem\n";
    }
  }

# diff resfiles resplot -> resfiles not used in paper
#			-> resplot without resfile 
print "Following results are used in $doctype but do not exist in RESDIR:\n";
foreach $texstem (@texstems) {
  if ( !(join(" ",@filestems) =~ /\b$texstem\b/)) {
    print "$texstem\n";
    }
  }
print "Following results exist in RESDIR but are not used in $doctype:\n";
foreach $filestem (@filestems) {
  if ( !(join(" ",@texstems) =~ /\b$filestem\b/)) {
    print "/bin/rm $resdir/$filestem*\n";
    }
  }
print "\n";

$noc =0; $psc =0; $vc =0; $v3c =0; $Ac =0; $warningc =0;
foreach $makestem (@makestems) {
  if (join(" ",@filestems) =~ /\b$makestem\b/) {
    ($no,$ps,$v,$v3,$A,$warning) = split(":",$files{$makestem});
    $noc += $no;
    if ($ps      eq 'ps'     ) {$psc++;     }
    if ($v       eq 'v'      ) {$vc++;      }
    if ($v3      eq 'v3'     ) {$v3c++;     }
    if ($A       eq 'A'      ) {$Ac++;      }
    if ($warning eq 'warning') {$warningc++;}
    }
  }

print "An attribute table for each result in RESULTS[ECN]R:\n";
format =
@<<<<<<<<<<<<<<<<< @<< @<< @<< @<< @<< @< @<< @< @<<<<<<< 
$resname           $nr $cr $er $no $ps $v $v3 $A $warning 
.

$resname = 'result'; $no = '#';
($ps,$v,$v3,$A,$warning) = @plottype;
$nr = 'NR'; $cr = 'CR'; $er = 'ER';
write;
$resname = '----------------'; $no = '--';
$nr = '--'; $cr = '--'; $er = '--';
$ps = '--'; $v  = '-' ; $v3 = '--'; $A = '-'; $warning = '-------';
write;

# $resname = ''; $no = '';
# ($ps,$v,$v3,$A,$warning) = ();
# $nr = ''; $cr = ''; $er = '';
# write;

$nrc = 0; $crc = 0; $erc = 0;
foreach $makestem (@makestems) {
  if (join(" ",@filestems) =~ /\b$makestem\b/) {
    $resname = $makestem;
    ($no,$ps,$v,$v3,$A,$warning) = split(":",$files{$makestem});
    $nr = '.'; $cr = '.'; $er = '.';
    if (`$makecmd $resname.testNR`) {
      $nr = 'NR'; $nrc++;
      }
    if (`$makecmd $resname.testCR`) {
      $cr = 'CR'; $crc++;
      }
    if (`$makecmd $resname.testER`) {
      $er = 'ER'; $erc++;
      }
    if ($nr ne 'NR' && $cr ne 'CR' && $er ne 'ER') {
      $nr = '?'; $cr = '?'; $er = '?';
      }
    write;
    }
  else {
    print "RESDIR: No result file with stem $makestem\n";
    }
  }

print "\n";
print "Some summary statistics:\n";
#print "\n";

#open(OUT,"|tee junk.resstat") || die "Cannot open pipe";
$dir = `pwd`;
chop($dir);
$dir =~ s/.*\///;
print "$dir total=$noc ps=$psc v=$vc v3=$v3c A=$Ac warning=$warningc\n";
print "$dir NR=$nrc CR=$crc ER=$erc \n";

#print OUT "$dir total=$noc ps=$psc v=$vc v3=$v3c A=$Ac warning=$warningc\n";
#print OUT "$dir NR=$nrc CR=$crc ER=$erc \n";
#close(OUT); # makes it print out on top instead of bottom

# statistics: look at last file: NR CR ER weird (single line)
#		NR pssave 	no ps, no warn
#		CR ps warn
#		R  ps 		no warn


# overview statistics: 
# 	# of figlist members
	# of figplot members by type
	# of resfiles by type

# output all to stdout
# better std out and turning on and off different options
