#!/usr/bin/perl

# $rcs = ' $Id: delete.cgi,v 1.2 1998/06/20 03:47:18 fitz Exp $ ' ;

unshift(@INC, "lib");

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


&ReadParse; # Read the script input
print "Content-type: text/html\n";

# Determine if we are running as an administrator
$admin = ($ENV{SCRIPT_NAME} =~ m!/admin/! ? 1 : 0);

# If the ID is specified as part of the URL,
# then we are editing an existing file
($id = $ENV{PATH_INFO}) =~ s!.*/!!;
if ($id)
{
    &error("Invalid format for ID '$id'.")
      unless $id =~ /^\d+$/;

    &error("Editing allowed only for administrators.")
      unless $admin;

    %entry = &db::read_entry($id);
    &error("Error reading bibliography entry for ID '$id': $db::error")
      unless defined %entry;
}
else
{
    &error("There was no database ID specified as part of the URL.");
}


if (%in)
{
    $entry{META} = "delete";

    if ($in{delete_mark})
    {
        if (&bib::write_entry($id, %entry))
        {
            &success("Marked entry $id deleted.");
        }
    }
    elsif ($in{delete_kill})
    {
        # Move to "killed" subdir
        if (rename("$db::dir/$id$db::ext",
                   "$db::dir/killed/$id$db::ext"))
        {
            &success("Killed entry $id.");
        }
    }
    else
    {
        &error("Neither delete_mark nor delete_kill was specified - contact webmaster.");
    }

    &error("Could not delete entry $id: $!");
}

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

$data = &bib::template("delete.html",
                       "ID", $id,
                       "DATA", $entry,);


print "\n", &bib::template("basic.html",
                           "TITLE", "Delete Entry $id",
                           "DATA", $data);

exit 0;
