#!/usr/bin/perl

$opcFile = "opcs.txt";

if (length ($ENV{'QUERY_STRING'}) > 0)
{
	$buffer = $ENV{'QUERY_STRING'};
	@pairs = split(/&/, $buffer);
	foreach $pair (@pairs)
	{
		($name, $value) = split(/=/, $pair);
		$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
		$in{$name} = $value; 
	}
}
$OPC = $in{'OPC'};
$OPC =~ /(\d.+)/;
$OPC =~ s/^0*//;
$vendor = $in{'vendor'};
$vendor =~ /(\w.+)/;
print "Content-type: text/html\n\n";
print "<html>\n";
print "<head>\n";
print "<title>OPC Search</title>\n";
print "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n";
print "<link rel=stylesheet href=opc_search.css>\n";
print "</head>\n";
print "<body>\n";
if($OPC =~ /(\d.+)/)
{
	@matches = split('\n',`grep $OPC $opcFile`);
	print "<table>\n<tr><td>Vendor</td><td>OPC</td><td>File</td></tr>";
	foreach $match (@matches)
	{
		@result = split(',',$match);
		$vendor = shift(@result);
		$file = shift(@result);
		$final_opc = sprintf("%010s",shift(@result));
		print "<tr>";
		print "<td class=\"vendor\">$vendor</td><td class=\"opc\">$final_opc</td><td class=\"file\">";
		print "<a href=\"$vendor/$file\">$file</a></td></tr>\n";
	}
	print "</table>\n";
}
else
{
        print "<div class=\"login\">OPC Search<br>";
        @self = split('/',$ENV{'REQUEST_URI'});
        $name = $self[$#self];
        print "<form action=\"".$name."\" method=\"get\">\n";
        print "<p><input type=\"input\" name=\"OPC\"><br></p>\n";
        print "<input name=\"submitreport\" type=\"submit\" value=\"Search\">\n";
        print "</form></div>\n";
}
print "</body>\n";
print "</html>\n";


