#!/usr/local/bin/perl -w
#
&readparse;
print "Content-type: text/html\n\n";

$progdir = ".";

$docdir="../../../doc/RUN_INFO/ldc2";
$cgibin="../../../cgi-bin/RUN_INFO/ldc2";
$runinfoplace = "../../../doc/RUN_INFO/ldc2";

$log="../../../doc/RUN_INFO/ldc2/RUN_LOG.txt";
$comments="../../../doc/RUN_INFO/ldc2/COMMENTS.txt";
$add_im="../../../doc/RUN_INFO/ldc2/ADDITIONAL_IMAGES.txt";
$currentRun="../../../doc/RUN_INFO/ldc2/CURRENTRUN.txt";


$html="../../../doc/RUN_INFO/ldc2/RUN_INFO_COMMENTS.html";

if (length($value[0])>2) {
    $redirectAddrComp = "$runinfoplace/RUN_INFO_COMMENTS.html#run$value[0]";
}
else {
    $redirectAddrComp = "$runinfoplace/RUN_INFO_COMMENTS.html";
}

open(OUT,">$html");
print OUT '
<html>
<head>
<title>Run Info</title>
</head>
<LINK REL=StyleSheet HREF="style.css" TYPE="text/css">
<body>';
print OUT "Displaying all runs with comments. This page is not automatically updated. Go back to <a href=\"../../../doc/RUN_INFO/ldc2/RUN_INFO.html\">Run Info Page</a>. <a href=\"$docdir/RUN_INFO_SEARCH.html\">Search for runs</a>.<br>\n";
print OUT '<table border=1 class=top>';
close(OUT);


open(COMMENTS,"<$comments");
@commentslines=<COMMENTS>;
chomp(@commentslines);
close(COMMENTS);
$commlen = scalar(@commentslines);


$processeddiv=0;
open(LOG,"<$log");
@loglines=<LOG>;
chomp(@loglines);
$nrLines=scalar(@loglines);
close(LOG);

$firstfound=0;
open(OUT,">>$html");
for ($index=$nrLines-1; $index>=0; $index--) {
    $logline=$loglines[$index];
    @value=split("___",$logline);
    $runNr=$value[0];$startTime=$value[1];$endTime=$value[2];$segCount=$value[3];
    $fileSize=$value[4];$nrEvents=$value[5];$scanType=$value[6];$dacId=$value[7];$consist=$value[8];
    if ($scanType eq "-1") {$scanType="&nbsp;";} 
    if ($dacId eq "-1") {$dacId="&nbsp;";}
    if ($nrEvents>0) {
	if ($processeddiv==0) {
	    $processeddiv-=5;
	    print OUT "<tr class=\"header\"><td>RunNr</td><td>Start Time</td><td>End Time</td><td>Nr Segments</td><td>FileSize [MB]</td><td>Nr Events</td><td><a href=\"http://tydes.home.cern.ch/tydes/doc/SPDscan/\">ScanType</a></td><td>DacId</td><td>Nr of <a href=\"http://tydes.home.cern.ch/tydes/doc/Consistency/\">Consistency Errors</a></td></tr>\n";
	}
	if ($firstfound==0) {   ### check if there is a new run
	    $firstfound=1;
	    open(CUR,"<$currentRun");
	    @curlines=<CUR>;
	    chomp(@curlines);
	    close(CUR);
	    if ($curlines[0]>$runNr) {   # enable comments for the new run
		$processeddiv++;
		print OUT "<tr>";
		print OUT "<td>$curlines[0]</td>";
		print OUT "<td colspan=8><font color=\"ff0000\">Running...</font></td>";
		print OUT "</tr>\n";
		print OUT "<tr><td colspan=9>\n";
		print OUT "<table cellspacing=5><tr>";
		print OUT "<td valign=top>";
		print OUT "<form action=\"$cgibin/updateComment.pl\" method=\"POST\">";
		print OUT "<input type=\"hidden\" name=\"run\" value=\"$curlines[0]\">";
		print OUT "<input type=\"submit\" value=\"Update Comment\">";
		print OUT "</form>\n";
		print OUT "</td>";
		print OUT "<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>";
		print OUT "<td width=400 bgcolor=#ffffff valign=top class=\"comment\">";
	      FORLOOP2: for ($i=$commlen-1;$i>=0;$i--) {
		  @info = split("___",$commentslines[$i]);
		  $commrun = $info[0];
		  $info[1]=~s/\r/<br>/g;
		  if ($commrun==$curlines[0]) {
#		print OUT "<td>Run $curlines[0]</td>";
		      print OUT "$info[1]";
		      last FORLOOP2;
		  }
		  else {
		      if ($commrun<$curlines[0]) {
#		    print OUT "&nbsp;\n";
			  last FORLOOP2;
		      }
		  }
	      }
		print OUT "</td></tr></td></table></td></tr>\n";
	    }
	}                      ###

	#check if there is a comment for this run
	$foundcomment=0;
        FORLCHK: for ($i=$commlen-1;$i>=0;$i--) {
	    @info = split("___",$commentslines[$i]);
	    $commrun = $info[0];
	    $info[1]=~s/\r/<br>/g;
	    if ($commrun==$runNr) {
		$foundcomment=1;
		last FORLCHK;
	    }
	    else {
		if ($commrun<$runNr) {
		    last FORLCHK;
		}
	    }
	}

	if ($foundcomment==1) {

	    print OUT "<tr class=\"runheader\">";
	    print OUT "<td>";
	    print OUT "<a id=\"run$runNr\" name=\"run$runNr\"></a>";
	    print OUT "$runNr</td>";
	    print OUT "<td>$startTime</td>";
	    print OUT "<td>$endTime</td>";
	    print OUT "<td>$segCount</td>";
	    print OUT "<td>$fileSize</td>";
	    print OUT "<td>$nrEvents</td>";
	    print OUT "<td>$scanType</td>";
	    print OUT "<td>$dacId</td>";
	    print OUT "<td>$consist</td>";
	    print OUT "</tr>\n";
	    $processeddiv++;

	    print OUT "<tr><td colspan=9>\n";
	    print OUT "<table cellspacing=5><tr>";
	    print OUT "<td valign=top>";
	    print OUT "<form action=\"$cgibin/updateComment.pl\" method=\"POST\">";
	    print OUT "<input type=\"hidden\" name=\"run\" value=\"$runNr\">";
	    print OUT "<input type=\"hidden\" name=\"redirect_addr\" value=\"VIEW_COMMENTS_ONLY.pl\">";
	    print OUT "<input type=\"submit\" value=\"Update Comment\">";
	    print OUT "</form>\n";
	    print OUT "</td>";
	    print OUT "<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>";
	    
	  FORL:	for ($i=$commlen-1;$i>=0;$i--) {
	      @info = split("___",$commentslines[$i]);
	      $commrun = $info[0];
	      $info[1]=~s/\r/<br>/g;
	      if ($commrun==$runNr) {
#		print OUT "<td>Run $runNr</td>";
		  print OUT "<td width=400 bgcolor=#ffffff valign=top class=\"comment\">";
		  print OUT "$info[1]";
		  print OUT "</td>";
		  last FORL;
	      }
	      else {
		  if ($commrun<$runNr) {
#		    print OUT "&nbsp;\n";
		      print OUT "<td width=400 bgcolor=#ffffff valign=top></td>";
		      last FORL;
		  }
	      }
	  }


	    print OUT "<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>";
	    
	    print OUT "<td valign=top>";
	    print OUT "<form action=\"$cgibin/uploadImage.pl\" method=\"POST\">";
	    print OUT "<input type=\"hidden\" name=\"run\" value=\"$runNr\">";
	    print OUT "<input type=\"submit\" value=\"Upload Image\" style=\"width:9em\">";
	    print OUT "</form>\n";
#	print OUT "<br>\n";
	    print OUT "<form action=\"$cgibin/removeImage.pl\" method=\"POST\">";
	    print OUT "<input type=\"hidden\" name=\"run\" value=\"$runNr\">";
	    print OUT "<input type=\"submit\" value=\"Remove Image(s)\" style=\"width:9em\">";
	    print OUT "</form>\n";
	    print OUT "</td>";
	    
	    print OUT "<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>";
	    
	    print OUT "\n<td valign=top>";
	    $imcount=0;
	    $vallen=scalar(@value);
	    for ($i=9; $i<$vallen; $i++) {
		$tmp = ($runNr-$runNr%100)/100;
		$imagesdir = "images$tmp";	    
		if ($nrLines-$index>20) {
		    $valueX=$value[$i];
		    $valueX=~s/jpg/gif/;
		    if (-e "$progdir/$imagesdir/$valueX") {
			$command="rm -f $progdir/$imagesdir/$valueX";
			system($command);
		    }
		}
		else {
		    $value[$i]=~s/jpg/gif/;
		}
		$imcount++;
		print OUT "<a href=\"$imagesdir/$value[$i]\">$value[$i]</a>&nbsp;&nbsp;&nbsp;";
		if ($imcount%2 == 0) {
		    print OUT "<br>\n";
		}
	    }
	    
	    print OUT "<br>\n";
	    
	    open(ADD_IM,"<$add_im");
	    @add_imlines=<ADD_IM>;
	    chomp(@add_imlines);
	    close(ADD_IM);
	    $add_imlen = scalar(@add_imlines);
	    $addcount=0;
	  FORL:	for ($i=0;$i<$add_imlen;$i++) {
#          $tmp = ($runNr-$runNr%100)/100;
	      $imagesdir = "additional_images$runNr";   
	      @info = split("___",$add_imlines[$i]);
	      $add_imrun = $info[0];
	      if ($add_imrun==$runNr) {
		  $addcount++;
		  print OUT "<a href=\"$imagesdir/$info[1]\">$info[1]</a>&nbsp;&nbsp;&nbsp;";
		  if ($addcount%2 == 0) {
		      print OUT "<br>\n";
		  }
		  
	      }
	  }
	    
	    print OUT "</td></tr></table></td></tr>\n";
	    
	    
	}
	
    }
}

print OUT '
</table>
</body>
</html>
';
close(OUT);


print "<html>\n<head>\n";
print "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"0; URL=$redirectAddrComp\">\n";
print "</head>\n";
print "<body bgcolor=\"#0000b0\" text=\"white\" link=\"yellow\" vlink=\"ff9900\">";
print "Redirecting to the commented runs page.<br>\n";
print "</body>\n</html>\n";


#******** END BODY************************
#
# EACH VALUE IN THE HTML FORM WILL BE CONTAINED IN
# THE THE @VALUE ARRAY.
sub readparse {
    read(STDIN,$user_string,$ENV{'CONTENT_LENGTH'});
    if (length($ENV{'QUERY_STRING'})>0) {$user_string=$ENV{'QUERY_STRING'}};
    $user_string =~ s/\+/ /g;
    @name_value_pairs = split(/&/,$user_string);
    foreach $name_value_pair (@name_value_pairs) {
	($keyword,$value) = split(/=/,$name_value_pair);
	$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/ge;
	push(@value, "$value");
	$user_data{$keyword} = $value;
	if ($value=~/<!--\#exec/) {
	    print "Content-type: text/html\n\nNo SSI permitted";
	    exit;
	};
    };
};

