#!/usr/bin/perl

use CGI qw/:standard/;
print
    header,
    start_html('Part search Script'),
    h1('Part search Script'),
    start_form,
    "what are you looking for?",textfield('searchstring'),p,
    radio_group(-name=>'searchtype',
                   -values=>['any item', 'exact string', 'all items', 'part number'],
                   -default=>'any item'),p,

    submit,
    end_form,
    hr,"\n";


 if (param('searchstring') ne "") {
    $query =  param('searchstring');

    if (param('searchtype') eq "any item") {
       print "search type is any item. (watch for duplicates!) <p>";
       foreach (split(' ', $query)) {
           $results = `grep -il "$_" *.txt `;
	   results($results) ;  # I need to "uniq" this
       }
       print "<p>";
    } elsif (param('searchtype') eq "all items") {
       print "search type is all items.<p>";
#       print param('searchtype') ;
       print "this doesn't work right yet, use \"exact string\" or \"any items\" for now.<p>";
       print "<p>";
    } elsif (param('searchtype') eq "part number") {
       print "search for part numbers.<p>";
       @fiches = ("hawkfiche", "VFR750F-1997-fiche", "VFR800-1998-fiche", "VTR1000F-superhawk-fiche", "F2-fiche","F3-fiche","F4-fiche") ;
       foreach $dir (@fiches ){
           print ("<b>matches from <a href=\"http://www.hawkworks.net/", $dir, "\">", $dir, "</a></b>") ;
	   print "<ul>";
           $results = `grep -il "$query" ../$dir/*.txt` ;
	   results($results);
	   print "<\/ul>";
       }
    } else {  # exact string
	print "search type is \"exact string\". <p>";
        $results = `grep -il "$query" *.txt` ;
        results($results);
    }



   print "<p>";




}
print end_html;




sub results {
   foreach  (split(' ', $results)) {
       chomp;
       $_ =~ s/.txt//;
       $pretty = $_;
       $pretty =~ s/.*\///;
       print "<a href=\"http:\/\/www.hawkworks.net\/hawkfiche\/$_.html\">$pretty<\/a><br>";
   }
}
