#!/usr/bin/perl

require "./search_config.pl";           # configuration

&ReadParse(*in);

$terms=$in{'terms'};
$file=$in{'file'};
$type=$in{'type'};
$case=$in{'case'};
$test=$in{'test'};

$root_ref="..";

$file =~ s/~amp/&/g;
$file =~ s/~quot/"/g;
$file =~ s/~gt/>/g;  
$file =~ s/~lt/</g;  


(@fpath)=split /\//,$file;

$pth='';

if ($#fpath)
{
 pop @fpath;
 foreach $fpath (@fpath)
 {
  $pth="$pth$fpath/";
 }
}


@terms=split " ", $terms;

print &PrintHeader;

$pth=$root_ref.$pth;
#print "$pth";
#print "---$#terms";

#$tmp=open IN, "$path$file";
$tmp=open IN, "$root_ref/$file" if ( -r "$root_ref/$file" );

if (!$tmp)
{
 print "An error has occured, please email njackson\@wash.k12.ut.us with the following information:<br>";
# print "<br>$tmp-$path$file-$terms-";
 print "<br>$tmp-$file-$terms-";
 $blah=`ls $path$file`;
 print "<br>-$blah-";
 exit;
}

$hfront="<font color=\"$hcolor\">";
$hend="</font>";
$blar=1;

while (<IN>)
{

  if ($type eq "phrase")
  {
#    $term2=uc $terms;
#    $term2="<font color=red>$term2</font>";
#    $term2="<font color=red>$term</font>";

#    if ($case) { $_ =~ s/(\b)($terms)(\b)(?!(\/<>))/\1$hfront\2$hend\3/g; }
#    else { $_ =~ s/(\b)($terms)(\b)(?!(\/<>))/\1$hfront\2$hend\3/gi; }
#    if ($case) { $_ =~ s/(?!<[^>]+>)($terms)(?!(\/>))/\1$hfront\2$hend/g; }
#    else { $_ =~ s/(?!<[^>]+>)($terms)(?!(\/>))/\1$hfront\2$hend/gi; }
#      if ($case) { $_ =~ s/($terms)(?!(\/>))/$hfront\1$hend/g; }
#      else { $_ =~ s/($terms)(?!(\/>))/$hfront\1$hend/gi; }

    if (!$test)
    {
      # 1 take everything that matches terms that are in a tag somewhere <..> and replaced it with something else
      #    that will not get matched by those not in tags
      # 2 change text that matches terms
      # 3 change the first items back to normal

      if ($case)
      {
          $_ =~ s/(<[^>]*)$terms([^>]*>)/\1--XYZZY--\2/g;
          $_ =~ s/($terms)/$hfront\1$hend/g;
          $_ =~ s/(<[^>]*)--XYZZY--([^>]*>)/\1$terms\2/g;
      }
      else
      {
          $_ =~ s/(<[^>]*)$terms([^>]*>)/\1--XYZZY--\2/gi;
          $_ =~ s/($terms)/$hfront\1$hend/gi;
          $_ =~ s/(<[^>]*)--XYZZY--([^>]*>)/\1$terms\2/gi;
      }
    }
    else
    {
      # 1 take everything that matches terms that are in a tag somewhere <..> and replaced it with something else
      #    that will not get matched by those not in tags
      # 2 change text that matches terms
      # 3 change the first items back to normal

      if ($case)
      {
          $_ =~ s/(<[^>]*)$terms([^>]*>)/\1--XYZZY--\2/g;
          $_ =~ s/($terms)/$hfront\1$hend/g;
          $_ =~ s/(<[^>]*)--XYZZY--([^>]*>)/\1$terms\2/g;
      }
      else
      {
          $_ =~ s/(<[^>]*)$terms([^>]*>)/\1--XYZZY--\2/gi;
          $_ =~ s/($terms)/$hfront\1$hend/gi;
          $_ =~ s/(<[^>]*)--XYZZY--([^>]*>)/\1$terms\2/gi;
      }
    }
  }
  else
  {
    foreach $term (@terms)
    {
#      $term2=uc $term;
#      $term2="<font color=red>$term2</font>";
#      $term2="<font color=red>$term</font>";

#      if ($case) { $_ =~ s/(\b)($term)(\b)(?!(\/<>))/\1$hfront\2$hend\3/g; }
#      else { $_ =~ s/(\b)($term)(\b)(?!(\/<>))/\1$hfront\2$hend\3/gi; }
#      if ($case) { $_ =~ s/(?!<[^>]+>)($term)(?!(\/>))/\1$hfront\2$hend/g; }
#      else { $_ =~ s/(?!<[^>]+>)($term)(?!(\/>))/\1$hfront\2$hend/gi; }
#        if ($case) { $_ =~ s/($term)(?!(\/>))/$hfront\1$hend/g; }
#        else { $_ =~ s/($term)(?!(\/>))/$hfront\1$hend/gi; }

      if (!$test)
      {
        # 1 take everything that matches terms that are in a tag somewhere <..> and replaced it with something else
        #    that will not get matched by those not in tags
        # 2 change text that matches terms
        # 3 change the first items back to normal

        if ($case)
        {
          $_ =~ s/(<[^>]*)$term([^>]*>)/\1--XYZZY--\2/g;
          $_ =~ s/($term)/$hfront\1$hend/g;
          $_ =~ s/(<[^>]*)--XYZZY--([^>]*>)/\1$term\2/g;
        }
        else
        {
          $_ =~ s/(<[^>]*)$term([^>]*>)/\1--XYZZY--\2/gi;
          $_ =~ s/($term)/$hfront\1$hend/gi;
          $_ =~ s/(<[^>]*)--XYZZY--([^>]*>)/\1$term\2/gi;
        }

      }
      else
      {
        # 1 take everything that matches terms that are in a tag somewhere <..> and replaced it with something else
        #    that will not get matched by those not in tags
        # 2 change text that matches terms
        # 3 change the first items back to normal

        if ($case)
        {
          $_ =~ s/(<[^>]*)$term([^>]*>)/\1--XYZZY--\2/g;
          $_ =~ s/($term)/$hfront\1$hend/g;
          $_ =~ s/(<[^>]*)--XYZZY--([^>]*>)/\1$term\2/g;
        }
        else
        {
          $_ =~ s/(<[^>]*)$term([^>]*>)/\1--XYZZY--\2/gi;
          $_ =~ s/($term)/$hfront\1$hend/gi;
          $_ =~ s/(<[^>]*)--XYZZY--([^>]*>)/\1$term\2/gi;
        }

      }

    }

  }


  $_ =~ s/(src=")/\1$pth/gi;
  $_ =~ s/(href=")(?!#)/\1$pth/gi;
  $_ =~ s/(href=")(?:#)/\1$file#\2/gi;

  print $_;

}

sub ReadParse
{
local $a = $_[0] ? $_[0] : \%in;
local $i;
if ($ENV{'REQUEST_METHOD'} eq 'POST') {
        read(STDIN, $in, $ENV{'CONTENT_LENGTH'});
        }
else {
        $in = $ENV{'QUERY_STRING'};
        }
@in = split(/\&/, $in);
foreach $i (@in) {
        local ($k, $v) = split(/=/, $i, 2);
        $k =~ s/\+/ /g; $k =~ s/%(..)/pack("c",hex($1))/ge;
        $v =~ s/\+/ /g; $v =~ s/%(..)/pack("c",hex($1))/ge;
        $a->{$k} = defined($a->{$k}) ? $a->{$k}."\0".$v : $v;
        }
}

sub PrintHeader {
  return "Content-type: text/html\n\n";
}

