#!/usr/bin/perl

# Paths definitions
$file = '/afs/csail.mit.edu/u/b/brussell/public_html/research/LabelMe/Logs/querylog.txt';

# The index file is generated by running torralba/projects/LabelMe/queryTool/createIndexSearchAndCrops.m
$cachedqueries = '/afs/csail.mit.edu/u/b/brussell/public_html/research/LabelMe/querycache/indexobjects.txt';

%box = &read_query_string;	# Read keys and values
$query = $box{'query'};

# Remove from the query bad characters
$query =~ tr/ A-Za-z0-9//cd;

# Store the length of the query string
$querylength = length($query);

# If the query is not empty run UNIX command
if ($querylength>0) {
# Save query in log file
$r = open(FILE, ">>$file");
print FILE "\nQueryObject: $query";
close(FILE);

@terms = split(' ', $query);
$querystring = "egrep -iw \'.*;.*;.*;.*$terms[0]\' $cachedqueries";
for ($i=1; $i <= $#terms; $i++) {
   $querystring = "$querystring | egrep -iw \'.*;.*;.*;.*$terms[$i]\'";
}

@listoffiles = `$querystring`;

$numberofmatches = scalar(@listoffiles);
}

open(FP,"js/my_scripts.js");
@fp = readline(FP);
close(FP);

# CREATE PAGE-------------------
# WEB PAGE QUERY BOX
print "Content-type: text/html

<head>
<script type=\"text/javascript\">
@fp
</script>
	<title>LabelMe query</title>
<style type=\"text/css\">
  #top_mid {margin-left:4em; font-size:small;}
  .top {vertical-align:top;}
</style>

</head>
<body>

<table id=\"header\">
<tr>
  <td class=\"top\">
<img src=\"http://people.csail.mit.edu/brussell/research/LabelMe/Icons/LabelMeNEWtight198x55.gif\" height=\"55\"><br>
  </td>

  <td class=\"top\" nowrap>
  <div id=\"top_mid\">

 <font size=-1>
  <a href=\"http://people.csail.mit.edu/brussell/research/LabelMe/\">Labeling tool</a>
  &nbsp;&nbsp;&nbsp;&nbsp;<a href=\"javascript:help_popup();\">Help</a>
  &nbsp;&nbsp;&nbsp;&nbsp;<a href=\"javascript:giveMe_popup();\">Downloads</a><sup><font color=red>New!</font></sup>
  &nbsp;&nbsp;&nbsp;&nbsp;<a href=\"javascript:about_popup()\">About</a>
  &nbsp;&nbsp;&nbsp;&nbsp;<a href=\"javascript:toolbox_popup()\">Matlab Toolbox</a>
  &nbsp;&nbsp;&nbsp;&nbsp;<a href=\"javascript:upload_popup()\">Upload Images</a>
  &nbsp;&nbsp;&nbsp;&nbsp;<a href=\"javascript:more_stats_popup()\">Stats</a>
</font>
  </div>
  </td>
</tr>
</table>

<form action=\"http://people.csail.mit.edu/torralba/research/LabelMe/js/LabelMeQueryObjectFast.cgi\"
      method=\"POST\">
<input type=\"text\" name=\"query\">
<input type=\"submit\" value=\"Search\">
</form>
";

if ($querylength>0) {
#$query =~ tr/ /AND/cd;
print "
<hr>
Your query (<b>$query</b>) matches <b>$numberofmatches</b> images.<br> 
Parts within each object are shown in color (only when labeled).
See 
<a href=\"http://labelme.csail.mit.edu/Stats/stats.html\">
summary statistics</a> for all the objects in the dataset, and the <a href=\"http://people.csail.mit.edu/torralba/research/LabelMe/wordnet/test.html\">wordnet hierarchy</a>. <br>
<hr>
<br>
";

# CREATE WEB PAGE AND THUMBNAILS POINTERS
# Parse file names
foreach $i (@listoffiles) {
($folder,$imagename,$objectname,$description) = split(";",$i);
#($imagename,$rest2) = split(";",$rest);
#($objectname,$rest3) = split(";",$rest2);
print "
<a href=\"http://labelme.csail.mit.edu/tool.html?collection=LabelMe&folder=$folder&image=$imagename\">
<IMG src=\"http://people.csail.mit.edu/brussell/research/LabelMe/ThumbnailsObjects/$folder/$objectname\" border=2 title=\"$description\"></a>";
}
}

# CLOSE WEB PAGE
print "
<br>
<!--WEBBOT bot=\"HTMLMarkup\" startspan ALT=\"Site Meter\" -->
<script type=\"text/javascript\" language=\"JavaScript\">var site=\"s21labelme\"</script>
<script type=\"text/javascript\" language=\"JavaScript1.2\" src=\"http://s21.sitemeter.com/js/counter.js?site=s21labelme\">
</script>
<noscript>
<a href=\"http://s21.sitemeter.com/stats.asp?site=s21labelme\" target=\"_top\">
<img src=\"http://s21.sitemeter.com/meter.asp?site=s21labelme\" alt=\"Site Meter\" border=\"0\"/></a>

</noscript>
<!-- Copyright (c)2005 Site Meter -->
<!--WEBBOT bot=\"HTMLMarkup\" Endspan -->

</body>";

if ($querylength>0) {
# Save query result in log file
$r = open(FILE, ">>$file");
print FILE " Found $numberofmatches matches";
close(FILE);
}

sub read_query_string
{
    local ($buffer, @pairs, $pair, $name, $value, %FORM);
    # Read in text
    $ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/;
    if ($ENV{'REQUEST_METHOD'} eq "POST")
    {
	read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
    } else
    {
	$buffer = $ENV{'QUERY_STRING'};
    }
    @pairs = split(/&/, $buffer);
    foreach $pair (@pairs)
    {
	($name, $value) = split(/=/, $pair);
	$value =~ tr/+/ /;
	$value =~ s/%(..)/pack("C", hex($1))/eg;
	$FORM{$name} = $value;
    }
    %FORM;
}


