#!/usr/bin/perl

# CGI script interface to place a nice wrapper around pictures using an
# existing document template. This might end up adding a caption from a
# similar local database, but then again that might get added later.
#
# our parameters as a CGI/GET request come directly from the QUERY_STRING
# environment variable. We can afford to be somewhat flexible in our path
# definitions here, since it would be harder to re-write the damn things
# for the VMS target.
#
# QUERY string will have two parts, being "path&filename", we can barf
# if there are more than two parts.

#$filebase = "/users/packrat/web/rattus/packrat";
#$filebase = "/home/packrat/public_html";
#$filebase = "/home/packrat/public_html/rattus.net/~packrat";
$filebase = "/var/www/rattus.net/people/packrat";
$webbase = "/~packrat";

if (not exists $ENV{'QUERY_STRING'}) {
    printerror("No parameters passed to script");
    exit(0);
}

@querybits = split(/\&/, $ENV{'QUERY_STRING'});
if (@querybits != 2) {
    printerror("Incorrect number of parameters");
    exit(0);
}

$path = $querybits[0];
$safepath = $path;
$safepath =~ y/-a-zA-Z0-9.//cd;
if ($safepath ne $path) {
    printerror("Invalid characters in path");
}

$path =~ y/./\//;
$templatefile = $filebase . "/" . $path . "/webpic.html";
if (not -f $templatefile ) {
    printerror("No template file found in path $templatefile");
}
open(TEMPLATE, $templatefile) ||
    printerror("Unable to read template file");

$filename = $querybits[1];
$safefilename = $filename;
$safefilename =~ y/-a-zA-Z0-9.//cd;
if ($safefilename ne $filename) {
    printerror("Invalid characters in filename");
}

# this is what will get inserted into the web page template to load
# the image itself.
$imagefile = $webbase . "/" . $path . "/" . $filename;

$captionfile = $filebase . "/" . $path . "/captions.txt";
if (not -f $captionfile ) {
    $usecaptions = 0;
} else {
    $usecaptions = 0;
    open(CAPTIONS, $captionfile) ||
	printerror("Unable to read caption file");
    while(<CAPTIONS>) {
	# grab the text from the file, if it exists.
	if ($_ =~ /^${filename}:(.*)/) {
	    $captiontext = $1;
	    $usecaptions = 1;
	    last;
	}
    }
    close(CAPTIONS);
}

# now everything is in place, we can process the template file from
# input to output with appropriate headers, replacing the IMAGE tag
# appropriately if it needs it

# headers.
print "Content-type: text/html\n\n";

while(<TEMPLATE>) {

    if (/<!--IMAGE-->/) {
	print "<IMG SRC=\"$imagefile\">\n";
    } elsif ($usecaptions and /<!--CAPTION-->/) {
	print "$captiontext\n";
    } else {
	# 
	print;
    }
}
close(TEMPLATE);
exit(0);

# this prints out a fairly standard HTML form error for the rattus.net
# stuff. We could also redirect to the standard form errors later on
# but this would make debugging a great deal more difficult.
sub printerror {
    my ($errormessage) = @_;
    
    print 'Content-type: text/html

<!DOCTYPE HTML PUBLIC "-//IETC//DTD HTML//EN">
<html>

<!-- rattus.net page headers -->
<head> <title> rattus.net: Error displaying image </title></head>
<body bgcolor=#ffffff>
<table width=100%>
<tr>
<td valign=top> <h1> Error displaying image </h1>
<td align=right valign=top> 
<img src="/images/rattitle-small.jpg" alt="rattus.net">
</tr>
</table>

<P> [' . $errormessage . ']
</P>

<P> There was a problem displaying the requested image. If this error
was generated by a rattus.net link, then you can contact the webmaster
at
<a href="mailto:webmaster@rattus.net"><i>webmaster@rattus.net</i></a>
to report the problem.
</P>

<P> Otherwise, you can probably navigate to the page you were seeking
from the main rattus.net page at
<A href="http://www.rattus.net">http://www.rattus.net</a>

</BODY>
</HTML>

';

exit(0);
}
