#! /opt/rh/rh-perl530/root/usr/bin/perl -w

###! /usr/bin/perl -w


use strict;
use CGI;
use DBI;

exit(1);

my $CGI = new CGI();
print $CGI ->header();

# open the accessDB file to retrieve the database name, host name, user name and password
open( ACCESS_INFO, "<../..\/accessDB" ) || die "Can't access login credentials";

my $database = <ACCESS_INFO>;
my $host     = <ACCESS_INFO>;
my $user     = <ACCESS_INFO>;
my $passwd   = <ACCESS_INFO>;

chomp( $database, $host, $user, $passwd );
close(ACCESS_INFO);

if ( $CGI->param() ) {
    my $xname    = $CGI->param('Name');
    my $author   = $CGI->param('Author');
    my $tribe    = $CGI->param('Tribe');
    my $subfam   = $CGI->param('Subfamily');
    my $family   = $CGI->param('Family');
    my $superfam = $CGI->param('Superfamily');
    my $photo    = $CGI->param('Photo');
    my $qsearch  = "Name = '$xname'";
    my @name     = split /\s+/, $xname;
    my $spec     = pop @name;
    my $genus    = shift @name;

###################### Access MySQL database ###############################

    my $dbconnection = "$database:$host:3306";
    my $data_source  = "dbi:mysql";              #define a mysql database

    my $dbh = DBI->connect( "$data_source:$dbconnection", $user, $passwd )
      or die "failed to connect to $dbconnection\n";

################ Prepare Output ############################################

    print <<EOT;
        <p align="center"><font size="+3" face="Times New Roman, Times, serif">
           Lepidoptera of French Polynesia - Species Account</font></p>
        <p align="center"><a FPLeps.htm"
           target="_parent">NEW SEARCH</a></p><p></p>
        <table align="center" width="810" border="0">
        <tr>
            <td width="800" height="100" align="left"
                valign="top"><p><span class="style3">
                <p><b>TAXONOMY</b></p>
                <b>$superfam : $family : $subfam : $tribe </b></p>
                <b><em>$xname</em>  $author</b></span></p>

EOT
############# Process Synonym query results ################################

    #  my $statement1 = qq< SELECT Name, Synonym, SynStatus,
    #                              PubYear, Source, Reference, Comments
    #                       FROM Lepsyn
    #                       WHERE $qsearch ORDER BY PubYear>;
    #
    my $statement1 =
        "SELECT Name, Synonym, SynStatus, "
      . "       PubYear, Source, Reference, Comments "
      . "FROM Lepsyn "
      . "WHERE ? "
      . "ORDER BY PubYear";

    my $cursor = $dbh->prepare($statement1);
    $cursor->execute($qsearch);

    while ( my $query = $cursor->fetch ) {

        print <<EOT;
            <em>$query->[1]</em> $query->[4] $query->[6] [$query->[2]]<br>
EOT

    }

############# Post photo carried over from LepQuery  #######################

    print <<EOT;
            <br>
            </td>
        </tr>
        <tr>
            <td width="800" height="100" align="left" valign="top"><a href=$photo><img src=$photo></a></td>
        </tr>
        <tr>
            <td width="800" height="100" align="left" valign="top">
            <p></p>
            <p><b>DISTRIBUTION</b></p>
EOT

########### Process Distribution query results  ###########################

    my $statement2 =
        "SELECT Name, RefName, Archipelago, "
      . "       Island, Source, Notes "
      . "FROM Lepdis "
      . "WHERE ? "
      . "ORDER BY Archipelago";

    my $cursor = $dbh->prepare($statement2);
    $cursor->execute($qsearch);

    while ( my $query = $cursor->fetch ) {

        print <<EOT;

        $query->[2]: $query->[3] - $query->[4] (as $query->[1])<br>

EOT

    }

############### Process Biology query results #############################

    print <<EOT;
          <br>
          </td>
        </tr>
        <tr>
          <td width="800" height="100" align="left" valign="top">
             <b>HOST RECORDS - BIOLOGY</b></p>
EOT

    my $statement3 =
        "SELECT Name, RefName, Host, Source, Notes "
      . "FROM Lepbio "
      . "WHERE ? ";

    my $cursor = $dbh->prepare($statement3);
    $cursor->execute($qsearch);

    while ( my $query = $cursor->fetch ) {

        print <<EOT;

            $query->[2] - $query->[3] (as $query->[1]) $query->[4]<br>

EOT

    }

    print <<EOT;
          <br>
          </td>
        </tr>
        <tr>
          <td width="800" height="100" align=left" valign="top">
             <b>EXTERNAL LINKS</b></p>
             Google Web Search: 
                <a href="http://www.google.com/search?q=$xname" target="_blank"><b><em>$xname</em></b></a><br>
             Google Image Search: 
                <a href="http://www.google.com/images?q=$xname" target="_blank"><b><em>$xname</em></b></a><br>
             Google Scholar Search: 
                <a href="http://scholar.google.com/scholar?q=$xname" target="_blank"><b><em>$xname</em></b></a><br>
             Bishop Museum Search: 
                <a href="http://www2.bishopmuseum.org/HBS/checklist/results.asp?grp=Arthropod&gen=$genus"  target="_blank"><b><em>$genus </em></b></a> or 
                <a href="http://www2.bishopmuseum.org/HBS/checklist/results.asp?grp=Arthropod&gen=&sp=$spec" target="_blank"><b><em> $spec</em></b></a><br>
             Cook Islands Search: 
                <a href="http://cookislands.pacificbiodiversity.net/cibed/dbs/cibd-results-page?t=$xname&c=1" target="_blank"><b><em>$xname</em></b></a><br>
             Funet (Finland) Search:
                <a href="http://www.nic.funet.fi/cgi-bin/life/goto?taxon=$genus" target="_blank"><b><em>$genus</em></b></a><br>
             LepIndex Search: 
                <a href="http://www.nhm.ac.uk/jdsml/research-curation/research/projects/lepindex/list.dsml?SCIENTIFIC_NAME_on_card=$genus" target="_blank"><b><em>$genus </em></b></a> or 
                <a href="http://www.nhm.ac.uk/jdsml/research-curation/research/projects/lepindex/list.dsml?SCIENTIFIC_NAME_on_card=$spec" target="_blank"><b><em> $spec</em></b></a><br>

             <p></p>   
             <a href="http://www1.ala.org.au/gallery2/main.php" target="_blank">Australia Moths Online</a><br>
             <a href="http://www.mothsofborneo.com/" target="_blank">Borneo Moths Online</a><br>
             <a href="http://www.usp.ac.fj/fijimoths/families-id.php?PHPSESSID=de4ffa1e167fb253e39e50c7bb15142e" target="_blank">Fiji Moths Online</a><br>
             <a href="http://www.landcareresearch.co.nz/research/biosystematics/invertebrates/lepidoptera/index_single.asp" target="_blank">New Zealand Arthropod Collection - Lepidoptera Type Specimens</a><br>

        </tr>
        </table>
        <p align="center">
        <a href="FPLeps.htm" target="_parent">NEW SEARCH</a>
        </p>
EOT

############## Close off database  ########################################

    $cursor->finish();     # close off cursor
    $dbh->disconnect();    # close off database
}
