#!/usr/local/bin/perl
#################################################################
#
# + FtpLocate - FTP search engine
#
# by Distributed System Lab E.E. NCKU Taiwan 2000
# http://ftp.ee.ncku.edu.tw/ftplocate/
#
#################################################################

use CGI;
unshift(@INC, substr($0, 0, rindex($0,'/')) ); # add prog path to INC
require ("config");

############################# main ##############################

if ( ! defined($ENV{'REMOTE_ADDR'}) ) {	# cmd mode 
  if ( $ARGV[0] >= 1) {
    ($totalcount, $topcount, @top)=toplist($ARGV[0]);
    for ($i=0; $i<=$#top; $i++) {
      ($query,$found,$count)=split(/!!/, $top[$i], 3);
      print "$query\n";
    }
    exit 0;

  } else {
    $myname=filename($0);
    print "
This program has 2 mode:\n
1. CGI mode: 
   it lists the summary of hotest queries
2. cmd mode: 
   it lists plain strings of hotest queries.
   These strings can be used with fltrain.pl to rebuild cache after reindex.
   ex: '$myname 50|fltran.pl' will build the top 50 queries into cache.

";
    exit 1;
  }
}


($totalcount, $topcount, @top)=toplist($MAXTOP);

print html_header_str("FtpLocate Query Hot List");
print qq|
<table width=90%>
<tr><td colspan=3>
&nbsp;<font color=#c00000>FtpLocate $STR_hotlist</font>
</td>
<td align=right colspan=3>
<a href="javascript:history.go(-1)">
$STR_back</a>
&nbsp;
<a href="$ENV{'SCRIPT_NAME'}">$STR_refresh</a>
&nbsp;
&nbsp;
</td></tr>
<tr bgcolor=#408080>
<td nowrap width=5% align=center><font color=#ffffff>#</font></td>
<td nowrap width=40%><font color=#ffffff>$STR_querystring</font></td>
<td nowrap width=5% align=center><font color=#ffffff>$STR_count</font></td>
<td nowrap width=5% align=center><font color=#ffffff>#</font></td>
<td nowrap width=40%><font color=#ffffff>$STR_querystring</font></td>
<td nowrap width=5% align=center><font color=#ffffff>$STR_count</font></td>
</tr>
|;

$maxline=int($#top/2)+1;
for ($i=0; $i<$maxline; $i++) {

  ($query,$found,$count)=split(/!!/, $top[$i], 3);
  $url=$SCRIPT_flsearch."?query=".CGI::escape($query);
  $n=$i+1;
  print qq|<tr>
<td align=center nowrap><font size=-1 color=#c00000>$n</font></td>
<td nowrap><font size=-1><a href="$url">$query</a></font>
<font size=-2 color=#999999>($found)</font></td>
<td align=center nowrap><font size=-1>$count</font></td>
|;

  if ( defined($top[$i+$maxline]) ) {
    ($query,$found,$count)=split(/!!/, $top[$i+$maxline], 3);
    $url=$SCRIPT_flsearch."?query=".CGI::escape($query);
    $n=$i+$maxline+1;
    print qq|
<td align=center nowrap><font size=-1 color=#c00000>$n</font></td>
<td nowrap><font size=-1><a href="$url">$query</a></font>
<font size=-2 color=#999999>($found)</font></td>
<td align=center nowrap><font size=-1>$count</font></td>
</tr>
|;
  }
}

if ( $totalcount==0 ) {
  $rate=0;
} else {
  $rate=int($topcount/$totalcount*100*100+0.5)/100;
}

print qq|
</table>
<table cellpadding=0 cellspacing=0 width=90%>
<tr><td colspan=2>
<hr width=100% size=1>
</td></tr>
<tr>
<td>
$topcount/$totalcount ($rate%)
</td>
<td align=right>
$STR_dslab
</td></tr>
</table>
</center>
&nbsp;
&nbsp;
</body>
</html>
|;

exit(0);
