#!/usr/local/bin/perl 
print "Content-type:text/html\n\n";
&chugCGI(*input);

print<<__EOQ__;
<head>
<title>kirk.is journal search</title>
</head>
__EOQ__


print "<BODY BGCOLOR=\"WHITE\">\n";

$jdir = "/data/MAIN/kirk.is/journal";
$kdir = "/data/MAIN/kirk.is/khftcea";
$tdir = "/data/MAIN/kirk.is/tumble";

$search = $input{"search"};
if($search ne ""){
    opendir(DIR,"$kdir") or print "can't open $dirname";
    while(defined($file= readdir(DIR))) {
	if($file =~ /\d\d\d\.html$/){
	    push @kfiles, $file;
	}
    }
    closedir(DIR);
#  @kfiles = qw();
   # @kfiles = "110.html";
    foreach $file (sort @kfiles){
	$found = 0;

	open(READFILE,"$kdir/$file");
	while($nextline = <READFILE>){
	    if($nextline =~ /\<title\>(.*)\<\/title\>/i){
		$title = $1;
	    }
	    if($nextline =~ /$search/i){
		if(!$found){
		    $found = 1;
		    print<<__EOQ__;
		      <h2><a href="/khftcea/$file">$title</a></h2>
__EOQ__
		}
		$nextline =~ s/($search)/\<b\>$1\<\/b\>/i;
		print $nextline;
		print "<hr width=100 align=\"center\">\n";
	    }
	}
	close READFILE;

    }


    opendir(DIR,"$jdir") or print "can't open $dirname";
    while(defined($file= readdir(DIR))) {
	if($file =~ /\.txt$/i){
	    push @blogfiles, $file;
	}
    }
    closedir(DIR);
#  @blogfiles = qw();
    foreach $file (sort @blogfiles){
	$found = 0;


	open(READFILE,"$jdir/$file");
	$title = "";
	while($nextline = <READFILE>){
	    if($title eq ""){
		$title = $nextline;
		chomp $nextline;
	    }
	    if($nextline =~ /$search/i){
		if(!$found){
		    $found = 1;
		    $file =~ /^(.*)\.txt/i;
		    $date = $1;
		    $url = $date;
		    $url =~ s/\./\//g;
		    $url = '/'.$url.'/';
		    print<<__EOQ__;
		      <h2><a href="$url">kirk.is $date</a>

			  $title</h2>
__EOQ__
		}
		$nextline =~ s/\</\&lt\;/ig;
		$nextline =~ s/\>/\&gt\;/ig;
		$nextline =~ s/($search)/\<b\>$1\<\/b\>/i;

		print $nextline;
	      
		print "<hr width=100 align=\"center\">\n";
	    }
	}
	close READFILE;
    }

##SEARCH TUMBLE

    opendir(DIR,"$tdir") or print "can't open $dirname";
    while(defined($file= readdir(DIR))) {
	if($file =~ /^\d\d\d\d\.\d\d\.\d\d$/){
	    push @tumblefiles, $file;
	}
    }
    closedir(DIR);
#  @tumblefiles = qw();
  foreach $file (sort @tumblefiles){
$found = 0;
	open(READFILE,"$tdir/$file");
	while($nextline = <READFILE>){
	    if($nextline =~ /$search/i){
		if(!$found){
		    $found = 1;

		    $date = $file;
		    $url = $date;
		    $url =~ s/\./\//g;
		    $url = '/'.$url.'/';
		    print<<__EOQ__;
		      <h2><a href="$url">$date</a></h2>
__EOQ__
		}
		$nextline =~ s/\</\&lt\;/ig;
		$nextline =~ s/\>/\&gt\;/ig;
		$nextline =~ s/($search)/\<b\>$1\<\/b\>/i;
		
		print qq(<div style="background-color:#EEEEFF">$nextline</div>);
	      
		print "<hr width=100 align=\"center\">\n";
	    }
	}
	close READFILE;
}
##/SEARCH TUMBLE
} #search ne ""


print<<__EOQ__;
<div align="center">
<form>

phrase:"<input name="search" value="$search">"
<input type="submit" value="search">
<br><br>
<b>
back to <A HREF="/">kirk.is</A></div></b>
__EOQ__








sub chugCGI {
  local (*in) = @_ if @_;
  local ($i, $key, $val);
  # Read in text
  if ($ENV{'REQUEST_METHOD'} eq "GET") {
    $in = $ENV{'QUERY_STRING'};
  } elsif ($ENV{'REQUEST_METHOD'} eq "POST") {
    read(STDIN,$in,$ENV{'CONTENT_LENGTH'});
  }
  @in = split(/&/,$in);
  foreach $i (0 .. $#in) {              
   # Convert plus's to spaces
    $in[$i] =~ s/\+/ /g;
    # Split into key and value.
    ($key, $val) = split(/=/,$in[$i],2); # splits on the first =.
    # Convert %XX from hex numbers to alphanumeric
    $key =~ s/%(..)/pack("c",hex($1))/ge;
    $val =~ s/%(..)/pack("c",hex($1))/ge;
    # Associate key and value
    $in{$key} .= "\0" if (defined($in{$key})); # \0 is the multiple separator
    $in{$key} .= $val;
  }
  return length($in);
}                                


