#!/usr/bin/perl

$opcFile = "/var/www/html/visionstarllc_com/lens_create_files/lnames.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; 
	}
}
$lname1 = $in{'lname1'};
$lname1 =~ /(\d.+)/;
$lname1 =~ s/^0*//;
$lname2 = $in{'lname2'};
$lname2 =~ /(\d.+)/;
$lname2 =~ s/^0*//;
$lname3 = $in{'lname3'};
$lname3 =~ /(\d.+)/;
$lname3 =~ s/^0*//;
$vendor = $in{'vendor'};
$vendor =~ /(\w.+)/;
print "Content-type: text/html\n\n";
print "<html>\n";
print "<head>\n";
print "<title>Lens Name 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($lname1 =~ /(\w.+)/)
{

        if(length ($lname2) > 0  && length ($lname3) > 0)
	{
	        @matches = split('\n',`grep -ih $lname1 $opcFile | grep -i $lname2 | grep -i $lname3`);
        }
	elsif(length ($lname2) > 0 )
	{
	        @matches = split('\n',`grep -ih $lname1 $opcFile | grep -i $lname2`);
        }
	elsif(length ($lname3) > 0 )
	{
	        @matches = split('\n',`grep -ih $lname1 $opcFile | grep -i $lname3`);
	}
	else
	{
	        @matches = split('\n',`grep -ih $lname1 $opcFile`);
        }

	print "<table>\n<tr><td>Vendor</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=\"file\">";
		print "<a href=\"$vendor/$file\">$file</a></td></tr>\n";
	}
	print "</table>\n";
}
else
{
        print "<div class=\"login\">Lens Name Search<br>";
        @self = split('/',$ENV{'REQUEST_URI'});
        $name = $self[$#self];
        print "<form action=\"".$name."\" method=\"get\">\n";
	print "<p style=\"font-size:16px\">Enter up to 3 words or partial words to narrow the results<br>Top field is required</p>\n";
        print "<p style=\"font-size:16px\"><input type=\"input\" name=\"lname1\"><br>";
        print "<input type=\"input\" name=\"lname2\"><br>";
        print "<input type=\"input\" name=\"lname3\"></p>\n";
        print "<input name=\"submitreport\" type=\"submit\" value=\"Search\">\n";
        print "</form></div>\n";
}
print "</body>\n";
print "</html>\n";


