#!/usr/bin/perl

use strict;
use CGI qw/:standard/;

#my $DOCROOT = '/whoiweb/apache/htdocs';
my $DOCROOT = '/raid/b1/web/virtual/www/www-htdocs';
my $DIR = '/science/B/whalepelvics/data';

my $q = CGI->new;
my $t = $q->param('t') || 'default';

my $title = 'The pelvic bones of some cetacea';
my $content;

if ($t eq 'default') {
    $content = homepage({ dir => "$DOCROOT/$DIR" }) . q(
<h3>Species</h3>
<ul>
    );
    my @items = dir_contents("$DOCROOT/$DIR");
    foreach my $item (@items) {
        my $nice_item = ucfirst $item;
        $nice_item =~ s:_: :g;
        my $english;
        {
          local $/ = undef;
          open(F, "$DOCROOT/$DIR/$item/info.txt");
          $english = <F>;
          close F;
        }
	chomp $english;
        $content .= qq(<li> <a href="page.cgi?t=$item"><b>$nice_item</b></a> ($english)\n);
    }
    $content .= '</ul>';
} else {
    # One of the species
    #$content .= "$DOCROOT/$DIR/$t";
    if (-d "$DOCROOT/$DIR/$t") {
        my $nicename = ucfirst $t;
        $nicename =~ s:_: :g;
        my $english;
        {
          local $/ = undef;
          open(F, "$DOCROOT/$DIR/$t/info.txt");
          $english = <F>;
          close F;
        }        
	    $content .= qq(<h2>$nicename ($english)</h2>);
        my @images = dir_contents("$DOCROOT/$DIR/$t");
        foreach my $img (@images) {
            next if $img eq 'info.txt';
            $content .= qq(<a href="$DIR/$t/$img" rel="lightbox"><img src="$DIR/$t/$img" width="125" border="0"></a> &nbsp;);
        }
    }
    $content .= q(<hr><a href="page.cgi">Home</a>);
}
print header() . 
qq(
<html>
<head>
<title>$title</title>
<link rel="/css/stylesheet" href="/science/B/whalepelvics/js/lightbox.css">
<script src="/science/B/whalepelvics/js/lightbox.js" type="text/javascript"></script>
</head>
<body>
$content
</body>
</html>
);


#####
sub dir_contents {
    my $dir = shift;
    opendir D, $dir;
    my @items = grep /\w+/, readdir D;
    closedir D;
    
    return sort @items;
}

sub homepage {
    my $args = shift;
    my $debug;
    #foreach my $key (keys %ENV) {
    #    $debug .= qq($key -- $ENV{$key}<br>);
    #}
    my $content;
    open (F, "/raid/b1/web/virtual/www/www-htdocs/science/B/whalepelvics/homepage.html");
    {
        local $/ = undef;
        $content = <F>;
    }
    close F;
    return $content;
}

__END__
<img src="http://www.whoi.edu/science/B/whalepelvics/whoilogo2.gif" align="left" hspace="5">
<h2>The pelvic bones of some cetaceans.</h2>
<p>
This web site provides digital photographs of the pelvic bones of
several different species of whales, dolphins and porpoise.  They are
referred to as pelvic bones because of the unresolved issue of labeling
them vestiges, rudiments or remnants.
</p>
<p>
The data with each picture, when available, includes species, sex,
length and either field number or museum number.  Missing information is
due in large part to the fact that some bones were collected over a
century ago and arrived with incomplete data.
</p>
<p>
The source of these bones include the Smithsonian, the Museum of
Comparative Zoology at Harvard, Northeastern University, the University
of Massachusetts at Amherst, the American Museum of Natural History, Los
Angeles County Museum, New Brunswick Museum and whale recovery efforts
of Dr. Tom French, Dr. Michael Moore, the author and others.
</p>
<p>
Anyone wishing to contribute photographs of cetacean pelvic bones or
comment on existing pictures is encouraged to e-mail me at
<a href="mailto:dbtaylor\@whoi.edu">dbtaylor\@whoi.edu</a>.
</p>
