#!/usr/bin/perl


# Path to header file.
my $HEADER = "../../cgi-bin/rant/header.html";
# Path to footer file.
my $FOOTER = "../../cgi-bin/rant/footer.html";



use CGI qw(:standard);
use strict;
use vars;
print header;


# Standard HTML.
	print start_html("RANT: Images from the Left Lane");
	open(HEADER, "$HEADER") ||
		&bail("Can't open header file.", "$!");
	while (<HEADER>) { print; } close HEADER;
	print '
<IMG SRC="title.gif" ALT="Images from the Left Lane" ALIGN="BOTTOM"><BR>
<small>By Mahlon Smith and Trevor Leahy<br>
</small></CENTER>
<HR NOSHADE SIZE=1>

<TABLE WIDTH="100%" BORDER="0" CELLSPACING="2" CELLPADDING="0">
<TR>
<TD WIDTH="15%"></TD>
<TD WIDTH="85%"><P>&nbsp;</P>
';


# Get the current image.
my $image = param("image");
&bail("Invalid path.","Can't browse through other directories.") if
    ($image =~ /^\.\./);

opendir(IMAGEDIR, "./images") || &bail("Can't open images directory.", "$!");

my @FILES;
my $file;
foreach $file (sort readdir(IMAGEDIR)) {
next if (-d $file);
$file =~ s/\..{3}//;
push(@FILES, $file);
}
closedir IMAGEDIR;

# If this is the first run of the CGI. pick the 1st image.
$image = $FILES[0] unless ($image);


# Print the image and the matching text.
print "<IMG SRC=\"./images/$image\.jpg\">";
print '<br>';
open(FILETEXT, "./texts/$image\.txt") 
	|| &bail("Whoops.","No matching text for this image.");
print '<p>';
while (<FILETEXT>) { print; } close FILETEXT;
print '</p><br><br><p>';

# Create the next and previous buttons.
my $count = 0;
my $curcount = 0;
my $FILES;
foreach $file (@FILES) {
$curcount++;
if ($image eq $file) {

if ($FILES[$count + 1]) {
        print "<a href=\"./show_image.cgi?image=$FILES[$count + 1]\">   
<IMG SRC=\"/icons/right.gif\" BORDER=0 ALIGN=MIDDLE HEIGHT=15 WIDTH=15>next</a>
<br>";
        }
if (($FILES[$count - 1]) && ($count != 0) && ($count != $#FILES)) {
        print "<a href=\"./show_image.cgi?image=$FILES[$count - 1]\">
<IMG SRC=\"/icons/left.gif\" BORDER=0 ALIGN=MIDDLE HEIGHT=15 WIDTH=15>previous</a>
";
	}
	last;
	}
	$count++;
}


    print '</P></TD></TR></TABLE>';

print "<P align=right><font size=-2>";
print $curcount . " of " . ($#FILES + 1);
print "<br><a href=\"./\">start</a>";
print "</P>";

    open(FOOTER, "$FOOTER") ||
        &bail("Can't open footer file.", "$!");
    while (<FOOTER>) { print; } close FOOTER;
    print end_html();
    exit;


# Exit gracefully.
sub bail {
    my $error = shift;
    my $errormsg = shift;
    print "<font color = red><b>";
    print "Error: $error<br>";
    if ($errormsg) {
        print "<br><HR NOSHADE SIZE=1></b>";
        print "System message: <b>$errormsg</b><br>";
    }
    print "</font>";
    print end_html();
    exit;
}   
