#!/usr/bin/perl
#

# sbloch added handling for GET, 10/15/1999
# sbloch replaced all this with uniform "readenv.pl" routines, 1/14/2000

&readStuff;

print "Content-type: text/html\n\n";

print "<HTML>\n";
print "<HEAD><TITLE>Project Plans vs. Reality</title></head>\n";
print "<BODY BGCOLOR=\"white\">", "\n";
 print "<H1 align=center>Project Plans vs. Reality</h1>\n";
 
 $prog_num = $stuff{'prog_num'};
 $psp_userid = $stuff{'psp_userid'};
 $data_file_path = $stuff{'data_file_path'};
 $est_filename = "$data_file_path" . "unsafe/$psp_userid" . ".estimate";
 $time_filename = "$data_file_path" . "unsafe/$psp_userid" . ".time";
 $loc_filename = "$data_file_path" . "unsafe/$psp_userid" . ".lines";
 $defect_filename = "$data_file_path" . "unsafe/$psp_userid" . ".defect";
 
 print "<h2 align=center>User $psp_userid, all programs</h2>\n";
 
 $have_estimates = 1;
 $have_time = 1;
 $have_loc = 1;
 $have_defects = 1;
 
 # open file to get program names
 open(ESTIMATE, "$est_filename") || 
    do { print "No estimate file.\n"; $have_estimates = 0; } ;
 
# if ($have_estimates) {
#    print "Found estimates file.\n";
#     }
# else {
#    print "Didn't find estimates file.\n";
#    }
# if ($have_time) {
#    print "Found time file.\n";
#    }
# else {
#    print "Didn't find time file.\n";
#    }
# if ($have_loc) {
#    print "Found loc file.\n";
#    }
# else {
#    print "Didn't find loc file.\n";
#    }
# if ($have_defects) {
#    print "Found defects file.\n";
#    }
# else {
#    print "Didn't find defects file.\n";
#    }
 
 @stages = ("N/A","Planning","Analysis/Design","Coding","Code Review","Testing","Post Mortem");
 
 
 # actual_time, est_loc, est_time, est_defects, loc, num_defects are all hashes,
 # indexed by prog_num.
 
 my %est_loc, %est_time, %est_defects;
 my %loc, %actual_time, %num_defects;
 
 # assert: at all times, each of these six hashes has exactly the same indices
 
 if ($have_estimates) {
    while (<ESTIMATE>) {
        @words=split(' ', $_);
 #        print "Read from estimate file: ";
 #        print "prog_num=$words[0], est_loc=$words[1], est_time=$words[2]:$words[3], est_defects=$words[4]<br>\n";
        $prog_num = $words[0];
        $est_loc{$prog_num} = $words[1];
        $est_time{$prog_num} = $words[2]*60 + $words[3];
        $est_defects{$prog_num} = $words[4];
        # Note that we do this even if there's already a $prog_num entry;
        # later estimates override earlier estimates.
 
        $loc{$prog_num} = 0;
        $actual_time{$prog_num} = 0;
        $num_defects{$prog_num} = 0;
        }
    }
 # print "<br>Est. LOC $est_loc, est. time $est_time, est. defects $est_defects\n";
 close (ESTIMATE);
 
 
 # open data file to read time recording log data
 open(TMDATA, "$time_filename") || 
    do { print "No time-log file.\n"; $have_time = 0; } ;
 
 if ($have_time) {
   while (<TMDATA>) {
      @words=split(' ', $_);
#      print "Read \"$_\" from time file.<br>\n";
    
      $prog_num = $words[0];
      unless (exists $actual_time{$prog_num}) {
         # the entry wasn't created when we went through the estimate file
         $loc{$prog_num} = 0;
         $actual_time{$prog_num} = 0;
         $num_defects{$prog_num} = 0;
         $est_loc{$prog_num} = 0;
         $est_time{$prog_num} = 0;
         $est_defects{$prog_num} = 0;
         }
      $actual_time{$prog_num} += $words[3];
      }
   }
# print "<br>Total time $phaseTotals[0]\n";
close (TMDATA);

 # open file to read line of code counts
 open(LOC, "$loc_filename") ||
    do { print "No lines-of-code file.\n"; $have_loc = 0; } ;
 
if ($have_loc) {
   # This loop currently assumes each file occurs only once per program.
   # To remedy this, we'll need a hash (indexed by prog_num) containing
   # a hash (indexed by filename) containing a line count; fix this
   while (<LOC>) {
       @words=split(' ', $_);
       # trouble if filenames contain spaces; fix this
       # words[0]=prog_num
       # words[1]=filename
       # words[2]=count of all lines
       # words[3]=count of non-blank lines
       # words[4]=count of non-blank, non-comment lines
       # print "Read \"$_\" from LOC file.<br>\n";

       $prog_num = $words[0];
       unless (exists $loc{$prog_num}) {
          # the entry wasn't created when we went through the estimate or
	  # time files
          $loc{$prog_num} = 0;
          $actual_time{$prog_num} = 0;
          $num_defects{$prog_num} = 0;
          $est_loc{$prog_num} = 0;
          $est_time{$prog_num} = 0;
          $est_defects{$prog_num} = 0;
	 }
       $loc{$prog_num} += $words[4];
       }
   }
# print "<br>Actual LOC $loc\n";
close (LOC);

 # open file to read defect data
 open(DEFECT, "$defect_filename") ||
    do { print "No defect-log file.\n"; $have_defects = 0; } ;
 
if ($have_defects) {
   while (<DEFECT>) {
       @words=split(' ', $_);
       $prog_num = $words[0];
       unless (exists $num_defects{$prog_num}) {
          # the entry wasn't created when we went through the estimate,
	  # time, or LOC files
          $loc{$prog_num} = 0;
          $actual_time{$prog_num} = 0;
          $num_defects{$prog_num} = 0;
          $est_loc{$prog_num} = 0;
          $est_time{$prog_num} = 0;
          $est_defects{$prog_num} = 0;
	  }
       $num_defects{$prog_num} += $words[1];
       }
   }
# print "<br>Actual number of defects $num_defects\n";
close (DEFECT);

print "<p><table border=1>\n";
print "<tr><th>&nbsp;</th>";
print "<th colspan=2 align=center>LOC</th>";
print "<th colspan=2 align=center>Time</th>";
print "<th colspan=2 align=center>Defects</th>";
print "<th colspan=2 align=center>LOC/hour</th>";
print "<th colspan=2 align=center>Defects/KLOC</th>";
print "</tr>\n";
print "<tr><th>Program \#</th>";
print "<th>estimated</th><th>actual</th>";
print "<th>estimated</th><th>actual</th>";
print "<th>estimated</th><th>actual</th>";
print "<th>estimated</th><th>actual</th>";
print "<th>estimated</th><th>actual</th>";
print "</tr>\n";

while (($prog_num,$dummy) = each %est_loc) {
    print "<tr><td align=right>$prog_num</td>";
    print "<td align=right>$est_loc{$prog_num}</td>";
    if ($loc{$prog_num} > 0) {
       print "<td align=right>$loc{$prog_num}</td>";
       }
    else {
       print "<td align=right>unknown</td>";
       }
    print "<td align=right>$est_time{$prog_num}</td>";
    print "<td align=right>$actual_time{$prog_num}</td>";
    print "<td align=right>$est_defects{$prog_num}</td>";
    print "<td align=right>$num_defects{$prog_num}</td>";

    #  Calculate planned and actual Minutes/LOC and LOC/Hour
    # print "est_time ", $est_time, "\n";
    # print "est_loc  ", $est_loc , "\n";
    # print "actual_time ", $actual_time, "\n";
    # print "loc ", $loc, "<br>\n"; 
#     if ($est_loc{$prog_num} > 0) {
#        $planMinLoc = sprintf ("%10.2f",$est_time{$prog_num}/$est_loc{$prog_num});
#        }
#     else {
#        $planMinLoc = "unknown";
#        }
     
    if ($est_loc{$prog_num} > 0 && $est_defects{$prog_num} > 0) {
      $planDefectKLOC = sprintf("%10.2f",$est_defects{$prog_num}*1000/$est_loc{$prog_num});
      }
    else {
      $planDefectKLOC = "unknown";
      }
    
    if ($est_loc{$prog_num} > 0 && $est_time{$prog_num} > 0) {
      $planLocHr = sprintf ("%10.2f",$est_loc{$prog_num}*60/$est_time{$prog_num});
      }
    else {
      $planLocHr = "unknown";
      }
    
    if ($loc{$prog_num} > 0 && $num_defects{$prog_num} > 0)  {
      $actDefectKLOC = sprintf("%10.2f",$num_defects{$prog_num}*1000/$loc{$prog_num});
      }
    else {
      $actDefectKLOC = "unknown";
      }
    
    if ($loc{$prog_num} > 0 && $actual_time{$prog_num} > 0)  {
      $actLocHr = sprintf("%10.2f",$loc{$prog_num}*60/$actual_time{$prog_num});
      }
    else {
      $actLocHr = "unknown";
      }

    print "<td align=right>$planLocHr</td>";
    print "<td align=right>$actLocHr</td>";
    print "<td align=right>$planDefectKLOC</td>";
    print "<td align=right>$actDefectKLOC</td>";
    print "</tr>\n";
    } # end while
print "</TABLE></CENTER>";


print "</BODY>", "\n";
print "</HTML>", "\n";
 






sub readStuff {
  my @cookie_pairs = split (/;\s/, $ENV{'HTTP_COOKIE'});
  parse_pairs(@cookie_pairs);

  my $buffer;
  # read cookies first, so GET, POST, etc. can override cookies

  if ( $ENV{'REQUEST_METHOD'} eq "GET" ) {
    $buffer = $ENV{'QUERY_STRING'};
  } elsif ($ENV{'REQUEST_METHOD'} eq "POST") {
    read(STDIN,$buffer,$ENV{'CONTENT_LENGTH'});
  } else {
        # Added for command line debugging
        # Supply name/value form data as a command line argument
        # Format: name1=value1\&name2=value2\&...
        # (need to escape & for shell)
        # Find the first argument that's not a switch (-)
        $buffer = ( grep( !/^-/, @ARGV )) [0];
        $buffer =~ s/\\&/&/g;
  }

  my @pairs = split(/&/,$buffer);
  parse_pairs(@pairs);
  }

sub parse_pairs {
  my $pair;
  foreach $pair (@_) {
     my ($name, $value) = split(/=/, $pair);
	 # replace plus signs with spaces
         $value =~ tr/+/ /;
	 # convert escape characters to ASCII form 
	 $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

	 # Stop people from using subshells to execute commands
	 $value =~ s/~!/ ~!/g;

	 # Uncomment for debugging purposes
         # print "Setting $name to $value<P>";
 
         # place name/value pair into array $stuff 
         $stuff{$name} = $value;
  }
}

