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

if ( defined($ENV{'REMOTE_ADDR'}) ) { exit; }	# cgi not allowed

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

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

$SIG{INT} = sub { 
  unlink("$FTPLOCATEDIR/$myname.RUNNING"); 
  print timestr()." user break!\n";
  exit 1;
};

$myname=filename($0);
if ( -f "$FTPLOCATEDIR/$myname.RUNNING") {
  print "Another $myname running! abort!\n";
  exit 1;
} else {
  open(T,">$FTPLOCATEDIR/$myname.RUNNING"); print T $$; close(T); # create tag
}

if ( $ARGV[0] ne "" ) {
  @ftpsite=@ARGV;
  print "Sorry, incremental index function is broken now,
a full indexing will be done after filelist processing!
Waiting for 10 seconds.....\n";
  sleep 10;
} else {
  @ftpsite=sitelist();
}
open (LOG, ">>$SYSTEMLOG");
select(LOG); $| = 1; 	# unbuffer mode
select(stdout); $|=1;	# unbuffer mode

foreach $site (@ftpsite) {

  if ( ! -f "$FILELISTDIR/collect/$site.gz" ) { next; }

  print timestr()." flindex - clear $site old sets\n";
  print LOG timestr()." flindex - clear $site old sets\n";

  if ( -d "$FILELISTDIR/$site" ) {
    opendir(D, "$FILELISTDIR/$site"); @f=readdir(D); closedir(D);
    chdir("$FILELISTDIR/$site"); unlink(@f);

  } else {
    mkdir("$FILELISTDIR/$site", 0755);
  }

  # unzip site.gz into sets
  print timestr()." flindex - unzip $site.gz into sets\n";
  print LOG timestr()." flindex - unzip $site.gz into sets\n";

  open(T, "$CMD_GZIP -dc $FILELISTDIR/collect/$site.gz|");
  $i=0; $setnum=sprintf($SETFPATTERN, $i);
  open(F, ">$FILELISTDIR/$site/$setnum");
  $j=0;
  while (<T>) {
    print F $_;
    $j++;
    if ($j==$SETSIZE) {
      close(F);
      $i++; $setnum=sprintf($SETFPATTERN, $i);
      open(F, ">$FILELISTDIR/$site/$setnum");
      $j=0;
    }
  }
  close(F);
  close(T);

  rename("$FILELISTDIR/collect/$site.summary", "$FILELISTDIR/$site/summary");
  unlink("$FILELISTDIR/collect/$site.gz");
}

print timestr()." flindex - do glimpse index\n";
print LOG timestr()." flindex - do glimpse index\n";

if ( ! -f "$FILELISTDIR/.glimpse_filters" ) {
  open(F, ">$FILELISTDIR/.glimpse_filters");
  print F "*\t$FTPLOCATEDIR/flfilter.pl <\n";
  close(F);
}
if ( ! -f "$FILELISTDIR/.glimpse_exclude" ) {
  open(F, ">$FILELISTDIR/.glimpse_exclude");
  print F ".glimpse\n";
  print F "summary\$\n";
  close(F);
}

chdir($FILELISTDIR);

#if ( $ARGV[0] ne "" ) { 	# incremental index, broken now?
#  $incremental="-a";
#  @ftpsite=@ARGV;
#} else {			# full index
  @ftpsite=sitelist();
#}

$sitelist_str=join(" ", @ftpsite);
open(X, "$CMD_GLIMPSEINDEX -I $sitelist_str|$CMD_GLIMPSEINDEX -H $FILELISTDIR -F -B -n $incremental -z -o|");
while (<X>) {
  if ( $_ !~ /^Indexing/ ) {
    print $_;
    print LOG $_;
  }
}
close(X);
`chmod 644 $FILELISTDIR/.glimpse*`;

print timestr()." flindex - clear cache\n";
print LOG timestr()." flindex - clear cache\n";

if ( -d "$CACHEDIR" ) {
  opendir(D, "$CACHEDIR"); @f=readdir(D); closedir(D);
  chdir($CACHEDIR); unlink(@f);
} else {
  mkdir ($CACHEDIR, 0777);
}


print timestr()." flindex - update maplist\n";
print LOG timestr()." flindex - update maplist\n";

maplistupdate(maplistmerge( localmaplist(), mymap() ));
maplistupdate(maplistmerge( localmaplist(), remotemaplist($MASTERURL) ));

print timestr()." flindex - done\n";
print LOG timestr()." flindex - done\n";
close(LOG);

unlink("$FTPLOCATEDIR/$myname.RUNNING");	# remove tag
