#!/usr/local/bin/perl
# Copyright 1994-2000, Cunningham & Cunningham, Inc.
# in collaboration with Dave W. Smith
# Open Source for personal use only.
# ... and then only
# with the understanding that the owner(s) cannot be
# responsible for any behavior of the program or
# any damages that it may cause. See LICENSE.TXT

# use strict;
$|++;
local $| = 1; 

$WikiDB = "../db/pustwiki.txt";

print "Content-type: text/html\n\n";

my $mark = "\263";
my $pomark = "\265";
# What is this mark ? - Что это за метка здесь ???


my $query=&urldecode($ENV{'QUERY_STRING'}) ;

my ($target) = $query =~ /search=([^\&]*)/;

my $pat = $target;

#$pat =~ s/[+?.*()[\]{}|\\]/\\$&/g;
#$pat = "\\b\\w*($pat)\\w*\\b";

$target =~s/"/&quot;/g;

my $body = <<EOF;
 <form action=search.pl>
  <input
   type="text"
   size="40"
   name="search"
   value="$target">
  <input type="submit" value="Search">
 </form>
EOF

open (TEXT,"$WikiDB") or die "Couldn't open for reading \n";
my %kees = ();	
while (<TEXT>) {
        ($name,$descript,$time) = split("#") ;
     $kees{$name} = $descript ;
	# OR push ( @{$kees{$name}},$descript) ;
		}
close TEXT ;

my ($files, $hits);
@files = %kees ; 
foreach $eachkey (keys %kees) {
if ($eachkey =~  /$pat/i || $kees{$eachkey} =~ /$pat/i) {
		$hits++;
	$body .= "<a href=wiki.pl?$eachkey>$eachkey<\/a> . . . . . .  $&<br>\n";

										}
					}


my %par;
$par{summary} = ($hits || "Ноль") . " страниц найдено среди просмотренных записей";
$par{title} = "Результаты поиска";
$par{page} = "Вики_Вики";
$par{body} = $body;

open(T, "../p_template.html") or die "template.html: $!";
undef $/;
$_ = <T>;
close(T);
s/\$(\w+)/defined($par{$1}) ? $par{$1} : ''/geo;
print;
$/ = "\n";

# == --  Russian Subs

sub urldecode{
 local($val)=@_;
 $val=~s/\+/ /g;
 $val=~s/%([0-9a-hA-H]{2})/pack('C',hex($1))/ge;
 return $val;
 }

