#!/usr/bin/perl

use CGI;

my $binomial =
"/home/adulau/website/foo/docs-free/catalogue-of-life/itisMySQL013009/binomial.txt";

sub Approx {

    my $val = shift;
    my @output;
    open( DESC, "-|" ) || exec "agrep", "-i", "-1", $val, $binomial;
    while (<DESC>) {
        push( @output, $_ );
    }
    close DESC;
    return @output;
}

my $web = CGI->new;
print $web->header;
if ( !( $web->request_method eq "POST" ) ) {
    print "debug mode";
    my @val = Approx("Borrago");
    foreach my $l (@val) {
        print "--> $l";
    }
}
else {
    print <<MYHEADER;
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
            <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" xmlns:dc="http://purl.org/dc/elements/1.1/">
            <head profile="http://gmpg.org/xfn/11"><title>Binomial taxonomy - fuzzy search result</title>
    <link rel="stylesheet" type="text/css" href="http://www.foo.be/css/screen.css" />
            </head><body>    
            <div id="wrapper"> <div id="header">
            <h2>Binomial taxonomy - fuzzy search result</h2>
            </div>
            <div id="content">    
MYHEADER
    #$web->param('val') =~ /^(\w{1,10})$/;
    my $goodchar = 'a-zA-Z0-9 ,.';  
    my $query = $web->param('val');
    $query =~ s/[^$goodchar]//go;
    print "<h3><i>$query</i> gives </h3>";
    my @val = Approx( $query );
    print "<ul>";
    foreach my $l (@val) {
        chomp ($l);
        print "<li>$l" ;
        print "<pre>taxonomy:binomial=\"$l\"</pre>";        
        print "</li>";
    }
    print "</ul>";
    print <<MYFOOTER;
    </div>
    <div id="footer">
    <a href="http://www.foo.be/cgi-bin/wiki.pl/MachineTagTaxonomy">Part of my messy machine tags project. </a>
    </div>
    </div> </body></html>
MYFOOTER
}
