#!/usr/bin/perl

# $rcs = ' $Id: display.cgi,v 1.4 1998/06/20 07:15:03 fitz Exp $ ' ;

# Given an ID, displays a single entry from the bibliography.
# The ID must be part of the URL, for example:
# /cgi-bin/display.cgi/ID

unshift(@INC, "lib");

require 'config.pl';
require 'db.pl';
require 'web.pl';
require 'cgi-lib.pl';

print "Content-type: text/html\n\n";
&ReadParse();

if ($in{id})
{
    $id = $in{id};
}
elsif ($in{next_submit} && $in{next})
{
    $id = $in{next};
}
elsif ($in{previous_submit} && $in{previous})
{
    $id = $in{previous};
}
else
{
    ($id = $ENV{PATH_INFO}) =~ s!.*/!!;
    &error("Bibliography ID must be specified as part of the URL.") unless $id;
}

(%entry) = &db::read_entry($id);
&error("Error reading bibliography entry for ID '$id': $db::error")
  unless defined %entry;

if ($entry{SUBMITNAME})
{
    if ($entry{SUBMITADDR})
    {
        $entry{SUBMITNAME} = sprintf("<A HREF=mailto:%s>%s</A>",
                                     &untaint($entry{SUBMITADDR}),
                                     $entry{SUBMITNAME});
    }

    $entry{SUBMITNAME} = "<P>Reviewed by $entry{SUBMITNAME}";
}

foreach $keyword (sort {uc($a) cmp uc($b)} split(/\s+/, $entry{KEYWORDS}))
{
    push(@keywords, $keyword);
}
$entry{KEYWORDS} = join(', ', @keywords);

($entry{TITLE2} = $entry{TITLE}) =~ s/\W+/ /g;

if ($entry{URL})
{
    $entry{TITLE} = &hotlink($entry{URL}, $entry{TITLE});
}

foreach (split(/,/, $in{series}))
{
    if ($need_next)
    {
        $next = "<INPUT TYPE=hidden NAME=next VALUE=$_>\n";
        $next .= "<INPUT TYPE=submit NAME=next_submit VALUE=Next>";
        last;
    }

    if ($id eq $_)
    {
        $need_next = 1;
        next;
    }

    $previous = "<INPUT TYPE=hidden NAME=previous VALUE=$_>\n";
    $previous .= "<INPUT TYPE=submit NAME=previous_submit VALUE=Previous>";
}


$entry .= &bib::template("entry3.html",
                         "NEXT", $next,
                         "PREVIOUS", $previous,
                         "SERIES", $in{series},
                         "HEADING", $in{heading},
                         "HEADING_UNTAINT", &untaint($in{heading}),
                         %entry)
  if ($next || $previous);

$entry .= "<FONT SIZE=2><CENTER><A HREF=/bib/$id>http://$ENV{SERVER_NAME}/bib/$id</A></CENTER></A><P>";

$entry .= &bib::template("entry.html",
                        "ID", $id,
                        %entry);
$entry .= &bib::template("entry2.html",
                         "ID", $id,
                         %entry);

print &bib::template("basic.html",
                     "TITLE", $entry{TITLE2},
                     "DATA", $entry);

exit 0;
