#!/srvtools/http/bin/perl
# Falke Bruinsma (falke@innersource.com)
# Inner Source Computer Services, Inc. (http://www.innersource.com)
#

require 'cgi-lib.pl';
require 'defines.pl';

MAIN:
{
  if (not &ReadParse(*input)) {
    CgiError("Error", "Parameters were expected and none were given.");
    exit;
  }
  print &PrintHeader;
  print <<EOF;
<HTML>
<HEAD> 
<TITLE>Search results</TITLE>
</HEAD>
<BODY>
<H3>Search results:</H3>
<HR>
EOF
   readlist();
   search();
   print <<EOF;
<HR>
Back to:
<A HREF="$home/homepages/index.html">Student Homepage Directory</A> |
<A HREF="$home/index.html">
The Student Center Main Page</A> |
<A HREF="$home/hub.html">
Student HUB</A>

</BODY>
</HTML>
EOF
}

sub readlist {
  open(LIST, $student_file);
  $/ = '';
  while ( <LIST> ) {
    @lines = split(/\n/);
    $index = "$lines[$LASTNAME], $lines[$FIRSTNAME]";
    $info{$index} = [ @lines ];
  }
  close(LIST);
}

sub search {
  $matches = 0; 
  @keywords = split(/\s+/, $input{'keywords'});
  foreach $i (keys %info) {
    @lines = @{$info{$i}};
    $string = join(' ',@{$info{$i}});
    foreach $keyword (@keywords) {
      if ($input{'boolean'} eq 'AND') {
        if (!($string =~ /$keyword/i)) {
           $include = 0;
           last;
        } else {  
           $include = 1;
        }
      } else {
         if ($string =~ /$keyword/i) {
            $include = 1;
            last;
         } else {
            $include = 0;
         } 
      }
    }
    if ($include == 1) { 
      $matches++; 
      if ($matches >= 25) {
         print "<B> Note: Your search has been cut off at $matches matches. </B>\n";
         last;
      }
      PrintRecord($lines); 
    }
  } 
}

