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

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(@data) = <INF>;
close(INF);

print header;
print start_html("Kite Catalog");

print <<EndHdr;
<h2 align="CENTER">Kite Catalog</h2>
To order, enter the quantity in the input box next to the item.<p>
<form action="order.cgi" method="POST">
EndHdr

my $cat = param('cat');
foreach my $i (sort pricesort @data) {
   chomp($i);
   my ($stocknum,$name,$instock,$price,$category) = split(/\|/,$i);
   if ($instock > 0) {
      if ($cat eq "" or $category eq $cat) {
         print qq(<input type="text" name="$stocknum" size=5> $name - \$$price<p>\n);
      }
   }
}

print qq(<input type="submit" value="Order!">\n);

print end_html;

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

sub pricesort {
    my @a = split(/\|/, $a);
    my @b = split(/\|/, $b);
    $a[3] <=> $b[3];
}