#!/usr/bin/perl -wT
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use Fcntl qw(:flock :seek);
use strict;

print header;
print start_html("Kite Catalog - Search Results");
print qq(<h2>Search Results</h2>\n);

my $keyword = param('keyword');
print qq(<p>Results for search of `$keyword':</p>\n);

open(INF,"data2.db") or &dienice("Can't open data.db: $! \n");
flock(INF, LOCK_SH);    # shared lock
seek(INF, 0, SEEK_SET); # rewind to beginning

my @names = ();
my %data = ();
while (my $i = <INF>) { # read each line one at a time
   my ($stocknum,$name,$instock,$price,$category) = split(/\|/,$i);
   push(@names, $name);
   $data{$name} = $i;

my @results = grep(/$keyword/i, @names);
my $num = @results;
if ($num) {
   print qq(<form action="order.cgi" method="POST">\n);
   foreach my $i (@results) {
       my ($stocknum,$name,$instock,$price,$category) = split(/\|/,$data{$i});
       print qq(<input type="text" name="$stocknum" size=5> $name - \$$price<p>\n);
   print qq(<input type="submit" value="Order!">\n);
   print qq(<p>$num kites found.</p>\n);
} else {
   print qq(<p>No kites found.</p>\n);

print end_html;

sub dienice {
    my($msg) = @_;
    print "<h2>Error</h2>\n";
    print $msg;