#!/usr/bin/perl
#
# Part of the Escher Database
#
# Pass as the query string the ID of the picture from the database which
# is to be displayed. A page will be displayed with the picture and info
# about it from the database. Html files are supported to be displayed
# instead of pictures.

BEGIN { push (@INC,'../include/') }

use Db;
require 'mce.conf';
require 'cgi-lib.pl';
print &PrintHeader;

# id of item to display is...
$id=$ENV{QUERY_STRING};

# Find the image or html file for a picture in the passed subdirectory,
# and return the extention of the file. Return undef if it isn't found.
sub FindPicture { my ($dir,$id)=@_;
	if (-e "$dir/$id.gif") { return '.gif' }
	elsif (-e "$dir/$id.jpg") { return '.jpg' }
	elsif (-e "$dir/$id.jpeg") { return '.jpeg' }
	elsif (-e "./$id.html") { return '.html' }
}

# Look up the item in the database:
Db::load($dbloc);
if (!$Db::db{$id}{name}) { # bad database/can't find.
  print "<H1>Error!</H1>Unable to find item $id in database.<P>\b";
}
else { #pretty-print item.
	$ext=FindPicture($dir,$id);

	print "<html>
		<head>
		<title>$Db::db{$id}{name}</title>
		</head>";
	if ($ext && $ext ne ".html") {
		print "<A HREF=\"$url/$id$ext\">$Db::db{$id}{name}</a> ";
	}
	else {
		print $Db::db{$id}{name}." ";
	}
	print ", also known as $Db::db{$id}{othernames}, " if $Db::db{$id}{othernames};
	print "was created in $Db::db{$id}{date} " if $Db::db{$id}{date};
	print "<br>$Db::db{$id}{comments}" if $Db::db{$id}{comments};
	if ($ext && $ext ne '.html') { # display the picture.
		print "<BR>
			<P><A HREF=\"$url$id$ext\"><IMG SRC=\"$url$id$ext\" ALT=\"\"></A><BR>
			<UL>";
	}
	elsif ($ext eq 'html') { # display a html file.
		print "<BR>";
		open (IN,"<./$id.html") || print $!;
		while (<IN>) { print $_ }
		close IN;
		print "<UL>";
	}
	print "<LI>Medium: $Db::db{$id}{medium}" if $Db::db{$id}{medium};
	print "<LI>Dimensions: $Db::db{$id}{dimensions} millimeters" if $Db::db{$id}{dimensions};
	if ($Db::db{$id}{group}) { # gix up groups into search links.
		if ($Db::db{$id}{group}=~m/,/ ne undef) {
			print '<LI>Groups: ';
		}
		else {
			print '<LI>Group: ';
		}
		foreach $group (split(/,/,$Db::db{$id}{group})) {
			$group2=$group;
			$group2=~tr/ /+/;
			$group2=lc($group2);
			$g_string.="<A HREF=\"mcesearch.cgi?quickgroup=$group2\">$group</A>, ";
		}
		$g_string=~s/, $/\n/;
		print $g_string;
	}
	if ($Db::db{$id}{xref}) { # Fix up xrefs into links.
		print "<LI>Cross references: ";
		foreach $a (split(/[ |,]/,$Db::db{$id}{xref})) {
			print "<A HREF=\"$show?$a\">$Db::db{$a}{name}</A> ";
		}
	}
	print '</UL>';
}
&PrintFooter;
print '</html>';
