#!/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);
print qq(<form action="order.cgi" method="POST">\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 $found = 0;
while (my $i = <INF>) { # read each line one at a time
   my ($stocknum,$name,$instock,$price,$category) = split(/\|/,$i);
   # change both to lowercase before trying to match with index()
   # since index() is case-sensitive, "Parafoil" won't match "parafoil"
   if (index(lc($name), lc($keyword)) > -1) {  
      $found++;         # increment the results counter
      print qq(<input type="text" name="$stocknum" size=5> $name - \$$price<p>\n);

if ($found) {
    print qq(<input type="submit" value="Order!">\n);
    print qq(<p>$found 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;