#!/usr/bin/perl

# $rcs = ' $Id: comment.cgi,v 1.3 1998/06/20 03:54:24 fitz Exp $ ' ;

unshift(@INC, "lib");

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

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

($id = $ENV{PATH_INFO}) =~ s!.*/!!;
if ($id)
{
    (%entry) = &db::read_entry($id);
    $title = "Comment about bibliography entry &quot;<STRONG>$entry{TITLE}</STRONG>&quot;:"
}

if (%in)
{
    &error("You must enter your email address.")
      unless $in{from};

    &error("Invalid email format: $in{from}")
      unless $in{from} =~ /.*@.*/;

    &error("You must enter a comment.")
      unless length($in{comment}) > 3;

    if ($id)
    {
        $link = "http://$ENV{SERVER_NAME}$bib::cgi/display.cgi/$id";
    }

    if ($in{to} eq "admin")
    {
        $to = $bib::email{admin};
    }
    elsif ($in{to} eq "webmaster")
    {
        $to = $bib::email{webmaster};
    }
    else
    {
        &error("No TO address selected.");
    }

    unless (&bib::sendmail("<COMMENT>\n$in{comment}\n</COMMENT>\n\n$link\n$entry{TITLE}\n",
                           "To: $to",
                           "From: $in{from} ($in{name})",
                           "Subject: comment from SKEPTIC bibliography",))
    {
        &error("Sendmail error: $bib::error");
    }

    &success("Thank you, your comments were sent to <B>$to</B>.<P>" .
             ($id ? "Return to: <A HREF=\"$link\">$entry{TITLE}</A>" : ""));
}

# Create the HTML page
print &bib::template("basic.html",
                     "TITLE", "Comment",
                     "DATA",
                     &bib::template("comment.html",
                                    "TITLE", $title));

exit 0;
