#!/usr/local/bin/perl
# scan referee reports for ISCA #20 

# modified by Hennessy 1/7/93
# modified by Andrew Erlichson 1/4/93
# usage:  scantext.pl <filename> beginrang# endrange#
# produces reports for all papers in range
# if no paper numbers are specified, the whole file is processed, 
# as before.

# pno = paper number
# ptl = paper title
# rnm = referee name
# raf = referee affiliation
# @comments = verbatim text of comments
# @master = one line per paper


sub INIT {
    &OUTPUT if ($dirty);
    # initialize state for new paper
    $pno = "???";
    $ptl = "???";
    $cnf = "???";
    $imp = "???";
    $qty = "???";
    $tch = "???";
    $act = "???";
    $rnm = "???";
    $raf = "???";
    $nln = 0;
    $mode = 0;
}

# stash current paper into master list
sub OUTPUT {
    $apno = sprintf("%03d",$pno);
    $index[$npaps] = join(":%:",$apno,$npaps);
    $master[$npaps] = join(":%:",$apno,$ptl,$cnf,$imp,$qty,$tch,
			           $act,$rnm,$raf,$nln,@comments);
    $npaps++;
    $dirty = 0;
}

# dump out the master list
sub DUMP {
NEXTPAPER:
    while($npaps--) {
	($pno,$offset) = split(/:%:/,$sindex[$npaps]);
        if (!$printall) {
            $printIt = ($pno >=  $ARGV[0]) & ($pno <= $ARGV[1]);
            }
            if (!$printIt) {
                next NEXTPAPER;
            }
	($pno,$ptl,$cnf,$imp,$qty,$tch,$act,$rnm,$raf,$nln,$comments) = 
	    split(/:%:/,$master[$offset],11);
	@comments = split(/:%:/,$comments);
	$~ = HEADER;
	write;
	$~ = ECHO;
	$i = 0;
	while ($i < $nln) {
	    $comments = $comments[$i++];
	    print "$comments\n";
	}
    }
}

$blank = 0;
$ignore = 0;
$npaps = 0;
$dirty = 0;
$mode = 0;
&INIT;
$filename = shift(@ARGV);       # first line is input file
if (!open(INPUTFILE, $filename)) {
   print STDERR "Can't open $filename---exiting\n";
   print "Usage: scantext.pl <inputfile> <reqpaper1> [<reqpaper2>] ..etc\n";
   exit (1);
}
$printall = 0;
if ($#ARGV==-1) { 
  $printall = 1;
} 
while(<INPUTFILE>) {
    chop;
    s/\f//;
    if (/^\s*$/) {		# if line is blank
	$ignore = $blank;
	$blank = 1;
    }
    else {
	$ignore = 0;
	$blank = 0;
    }

    if ($mode == 0  && !$ignore) {
      MAIN: {
	  if (/^\s*1993 ISCA #20--Electronic Referee\'s Report\s*$/) {
	      &INIT;
	      last MAIN;
	  }				
	  if (/Paper Number:/i) {
	      die "Redundent data at $. in $ARGV\n" unless ($pno == "???");
	      $dirty = 1;
	      /:\s*(\d+)/;
	      $pno = $1;
	      die "Invalid data at $. in $ARGV\n" unless (1 <= $pno && $pno <= 1000);
	      last MAIN;
	  }
	  if (/Paper Title:/i) {
	      die "Redundent data at $. in $ARGV\n" unless ($ptl == "???");
	      $dirty = 1;
	      ($junk,$ptl) = split(/:\s*/);
	      last MAIN;
	  }
	  if (/Confidence in your evaluation:/i) {
	      die "Redundent data at $. in $ARGV\n" unless ($cnf == "???");
	      $dirty = 1;
	      /:\s*(\d+\.?\d*)/;
	      $cnf = $1;
	      die "Invalid data at $. in $ARGV\n" unless (0 <= $cnf && $cnf <= 5);
	      last MAIN;
	  }
	  if (/Interest\/Importance to ISCA:/i) {
	      die "Redundent data at $. in $ARGV\n" unless ($imp == "???");
	      $dirty = 1;
	      /:\s*(\d+\.?\d*)/;
	      $imp = $1;
	      die "Invalid data at $. in $ARGV\n" unless (0 <= $imp && $imp <= 5);
	      last MAIN;
	  }
	  if (/Quality of presentation:/i) {
	      die "Redundent data at $. in $ARGV\n" unless ($qty == "???");
	      $dirty = 1;
	      /:\s*(\d+\.?\d*)/;
	      $qty = $1;
	      $qty = $1;
	      die "Invalid data at $. in $ARGV\n" unless (0 <= $qty && $qty <= 5);
	      last MAIN;
	  }
	  if (/Technical contribution of paper:/i) {
	      die "Redundent data at $. in $ARGV\n" unless ($tch == "???");
	      $dirty = 1;
	      /:\s*(\d+\.?\d*)/;
	      $tch = $1;
	      die "Invalid data at $. in $ARGV\n" unless (0 <= $tch && $tch <= 5);
	      last MAIN;
	  }
	  if (/Recommended action for paper:/i) {
	      die "Redundent data at $. in $ARGV\n" unless ($act == "???");
	      $dirty = 1;
	      /:\s*(\d+\.?\d*)/;
	      $act = $1;
	      die "Invalid data at $. in $ARGV\n" unless (0 <= $act && $act <= 5);
	      $mode = 1;
	      last MAIN;
	  }
	  if (/Referee\'s name:/i) {
	      die "Redundent data at $. in $ARGV\n" unless ($rnm == "???");
	      $dirty = 1;
	      ($junk,$rnm) = split(/:\s*/);
	      last MAIN;
	  }
	  if (/Referee\'s affiliation:/i) {
	      die "Redundent data at $. in $ARGV\n" unless ($raf == "???");
	      $dirty = 1;
	      ($junk,$raf) = split(/:\s*/);
	      last MAIN;
	  }
	  if (/\+{10,}/) {		# 10 or more pluses
	      &INIT;
	      last MAIN;
	  }
      }
    }
    elsif (!$ignore) {
	$mode = 0 if (/-{10,}/);		# 10 or more minuses
	$comments[$nln++] = $_;
    }

    if (eof)
    {
	close(INPUTFILE);	# reset line numbering

    }
}

@sindex = reverse sort @index;
&DUMP;

# end of executable code    

format top = 
Referee Reports                  paper @<<                         page @<<
$pno,$%
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

.

format HEADER =

***************************************************************************
***************************************************************************
paper #@<<   @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$pno,        $ptl
@<<<<<<<<<<<<<<<<<<<<<<                          @>>>>>>>>>>>>>>>>>>>>>>>>>
$rnm,                                            $raf
Confidence: @.#  Importance: @.#  Quality: @.#  Technical: @.#  ACTION: @.#
            $cnf,            $imp,         $qty,           $tch,        $act

.

format ECHO =
@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$comments
.





