#!/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; CGI::ReadParse(*input);
use Socket;
unshift(@INC, substr($0, 0, rindex($0,'/')) ); # add prog path to INC
require ("config");

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

$starttime=gettimeofday(); $starttimestr=timestr();

$query=clean_str($input{'query'}); 
$query_raw=CGI::escape($query);
$page=$input{'page'};
$client=ip2fqdn(client_ip());
$resultfname=$query_raw.".desc";

if ( $query!~m![/\w\d\.\+\-\\_~]{2}! ) {	# keyword to short
  print "Location: $SCRIPT_flsummary?form=dsearch\n\n";
  exit 0;
} elsif ( -f "$CACHEDIR/$resultfname" ) {	# cache hit
  $cachestr="cache";
} elsif ( -f "$FTPLOCATEDIR/dindex.pl.RUNNING" ) {	# service not available
  print html_header_str($STR_title);
  print qq|<br><br>
<b><font color=#cc0000>Indexing, please try later...</font></b>
<br><br><br>
<table width=89%><tr><td nowrap align=right><hr size=1>
$STR_dslab
</td></tr></table></body></html>|;
  exit 0;
} else {					# cache miss
  $cachestr="";
  do_dsearch($query, $resultfname);
}

open (L, "$CACHEDIR/$resultfname");
$i=0;
while ( <L> ){
  chop; $list[$i]=$_;
  $i++;
  if ( $i>$MAXREC ) { last; }
}
close(L);

$searchtime=int((gettimeofday()-$starttime)*100+0.5)/100;
loguser($starttimestr, $searchtime, $client, $fsite, $i, $query, $cachestr);

print html_header_str($STR_title);
print qq|
<form method=get action="$ENV{'SCRIPT_NAME'}">
<table width=89%><tr>
<td nowrap width=40%>
<input name=query size=20>
<input type=submit VALUE="Description Search">
&nbsp;
<a href="$URL_help">$STR_help</a>
</td>
<td nowrap align=center width=50%>
|;

$n=$#list+1;
($listmin, $listmax)=init_pageline($n, 100, 10, $page, "$ENV{'SCRIPT_NAME'}?query=$query_raw$CGIF");
print_pageline();

print qq|</td>
<td align=right nowrap width=10%>
<a href="$SCRIPT_flsummary?form=flsearch">$STR_search_by_file</a>
</td>
</form>
</tr></table>

<table width=90%><tr>
<td colspan=2 bgcolor=#408080>
<font color=#ffffff>$STRF&nbsp;$STR_related_desc $query $STR_related_desc2 $n</font>
&nbsp;
<font color=#cccccc>$STR_shorthelp</font>
</td>
<td bgcolor=#408080 nowrap>
<font size=-1 color=#cccccc>$cachestr&nbsp;</font>
</td>
</tr>
|;

$prevprefix="";
$prevdesc="";
for ($i=$listmin; $i<=$listmax; $i++) {
  ($fname, $prefix, $desc)=split(/!!/,$list[$i],3);
  $desc=~s!($query)!<b>$1</b>!gi;

  if ( $prefix eq "" ) {
    if ( $fname=~/^rfc.*\.txt/ ) {
      $prefix='RFC';
    } elsif ( $fname=~/^draft.*\.txt/ ) {
      $prefix='Draft';
    } else {
      $prefix='FreeBSD';
    }
    $flquery=CGI::escape("$fname");
  } else {
    $flquery=CGI::escape("$prefix/$fname\$");
  }

  if ( $desc eq $prevdesc ) {
    $desc="";
  } else {
    $prevdesc=$desc;
  }
  if ( $prefix eq $prevprefix && $desc eq "" ) {
    $prefix="";
  } else {
    $prevprefix=$prefix;
  }

  print qq|<tr valign=top>
<td><font size=-1>$desc</font></td>
<td><font color=#999999 size=-1>$prefix</font></td>
<td><font size=-1><a href="$SCRIPT_flsearch?query=$flquery">$fname</a></font></td>
</tr>
|;
}

print qq|</table>
<hr size=1 width=90%>
<table width=89%><tr>
<td nowrap>
<font color=#999999 size=-1>
$searchtime sec
</font>
</td>
<td nowrap width=60% align=center>
|;

print_pageline();

print qq|</td>
<td nowrap align=right>
$STR_dslab
</td></tr>
</table>
</center>
</body>
</html>
|;
exit 0;
