#!/perl/bin

# $Header:   //reebok/xyzL/JavaComp/webpack/doc/common/lookup.pl   1.3   02 Oct 1997 11:38:04   rflynn  $
# ------------------------------------------------------------
# lookup.pl
#
# lookup
# 
# ------------------------------------------------------------
# Maintenace Log
# --------------



&html_header ("Kona Doc Search Results");  # from cgi-lib.pl


# Get the input

&parse_request;
#
#  If debugging on, show all the keys and values from the form.
#
#   if ($query{'debug'} =~ /no debug/) { }
#   else{
#  &show_debug_info;  }
#
#  If name missing, let them retry the form.
#
if ($query{'name'} eq "") {

   print "<h2>Error!  It seems you did not enter a keyword.</h2><p>";
   print "<A HREF=\"search_script.pl\">";
   print "Try Again? </A>   <hr>";

   &home;   # let them go back home if they want to.
   exit 1;
              }
#this line sets the name of the three column text file
$rolodex = "target2.txt";

&field_head();  # for the Results columns.

print"<HR>";
$hitctr = 0;  # hit counter variable

open(ROLODEX, $rolodex) || die "cannot open $rolodex data file";
#
#  Mod:  use Dict flag = 0 - *all* characters 
#
&look(*ROLODEX, $query{'name'},0,1);  # use the assoc array

while (<ROLODEX>){

      last unless /^$query{'name'}/i;
      @line = split(/\s\s+/);
      $hitctr++;
      if ($hitctr > $query{'limit-List'}) {
          $hitctr--;  # must adjust this to get it right.
          print "<i>User limit of $hitctr reached...ending search.</i>";
         last;  }
     
    
       print "<pre>";
          $X="<A HREF=http://kona-dpp.lotus.com/dpp_doc/$line[2] Target=_top>$line[1]</A><BR>";
        printf("  %-20s   %-15s   ",$line[0],$X);
       print "</pre>";

}   # end of WHILE


close(ROLODEX) || die "cannot close $rolodex data file";   

print "Your search found <b>$hitctr</b> item(s).<p>";
print "<A HREF=\"search_script.pl\">New</A> search?";  

# 
&home;
exit 0;

sub field_head{
  $fhdr="<B>Keyword</B>";
  $chdr="<B>Topic</B>";
#  $shdr="<B>Link</B>";
  print "<pre>";
  printf(" %-25s    %-20s      ",$fhdr, $chdr);
  print "</pre>";
}  

sub show_debug_info {

while (($key,$value) = each(%query)) {
   print "The value of $key is $value <br>"; }

}

exit 0;  
 
;# Usage: &look(*FILEHANDLE,$key,$dict,$fold)

;# Sets file position in FILEHANDLE to be first line greater than or equal
;# (stringwise) to $key.  Pass flags for dictionary order and case folding.

sub look {
    local(*FH,$key,$dict,$fold) = @_;
    local($max,$min,$mid,$_);
    local($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,
       $blksize,$blocks) = stat(FH);
    $blksize = 8192 unless $blksize;
    $key =~ s/[^\w\s]//g if $dict;
    $key =~ y/A-Z/a-z/ if $fold;
    $max = int($size / $blksize);
    while ($max - $min > 1) {
	$mid = int(($max + $min) / 2);
	seek(FH,$mid * $blksize,0);
	$_ = <FH> if $mid;		# probably a partial line
	$_ = <FH>;
	chop;
	s/[^\w\s]//g if $dict;
	y/A-Z/a-z/ if $fold;
	if ($_ lt $key) {
	    $min = $mid;
	}
	else {
	    $max = $mid;
	}
    }
    $min *= $blksize;
    seek(FH,$min,0);
    <FH> if $min;
    while (<FH>) {
	chop;
	s/[^\w\s]//g if $dict;
	y/A-Z/a-z/ if $fold;
	last if $_ ge $key;
	$min = tell(FH);
    }
    seek(FH,$min,0);
    $min;
}

1;

#
#  file: cgi-lib.pl
#
#  auth: Brad Burdick
#  desc: This library deals with basic CGI POST or GET method request
#        elements such as those delivered by an HTTPD form, i.e. a url
#        encoded line:  a=b&b=c&c=d
#
#        Also handles <ISINDEX> GET requests.
#
#	       
#

#
# parse_request reads the POST or GET request from STDIN, and then splits
# it into its name=value pairs.  Special test for <ISINDEX> input.
#
sub parse_request {

	if ($ENV{'REQUEST_METHOD'} eq "POST") {
		# assumes read gets everything!!
		read(STDIN, $raw_query, $ENV{'CONTENT_LENGTH'});
	} elsif ($ENV{'REQUEST_METHOD'} eq "GET" ) {
		$raw_query = $ENV{'QUERY_STRING'};
	} else {
		# unrecognized request method
		return;
	}

	# Decode HEX values and spaces, if any
	if ($raw_query !~ /[&=]/) {  # handle <ISINDEX> input
		$isindex = $raw_query;
		&decode_url($isindex);
	} else {
		%query = &decode_url(split(/[&=]/, $raw_query));
	}
}

#
#	Decode a URL encoded string or array of strings 
#		+ -> space
#		%xx -> character xx
#
sub decode_url {
	foreach (@_) {
		tr/+/ /;
		s/%(..)/pack("c",hex($1))/ge;
	}
	@_;
}

#
# html_header sends an HTML header for the document to be returned
#
sub html_header {
	local($title) = @_;
	print "Content-type: text/html\n\n";
	print "<html><head>\n";
	print "<title>$title</title>\n";
	print "</head>\n<body BGCOLOR=white>\n";
}


# keep require happy
1;


#-------------------------#
# Standard EDGAR Routines #
#-------------------------#

#
#  Supply the go-home and back.gif link.
#
sub home{
  local ($gif,$text) = @_; 

  # if nothing supplied, set default to back.gif and vanilla caption.
  
  if ($#_ < 0) {   # check out the funky $#_ !!
      $gif = "back.gif";
      $text = "Return to the Programmatic Reference Home Page";
} 

print "<HR>";
print "<a href=\"http://kona-dpp.lotus.com/dpp_doc/prog_ref_home.html\" target=_top>";
print "<img src=\"http://kona-dpp.lotus.com/dpp_doc/$gif\">";
print "$text</A>";
print "<HR>";
}

1;  # it is CRITICAL to end a subroutine library with a 1;
    # or else, no requires using this would work.







