#!/usr/bin/perl

# $rcs = ' $Id: convert.cgi,v 1.2 1998/06/20 04:00:04 fitz Exp $ ' ;

unshift(@INC, "../lib");

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

%regexp =
  (
   #<strong>Title</strong>
   #<blockquote>
   # Author<br>
   # Year, Publisher; Pages<br>
   # <em>#Keyword#, ...</em>
   #</blockquote>
   #Review-Text<p>

   "book", # ($title, $author, $info, $keywords, $comment)
   "<strong>(.*)</strong>.*<blockquote>(.*)<br>(.*)<br>.*<em>(.*)</em>.*</blockquote>(.*)",

   "internet", # $url, $title, $keywords, $comment
   "<strong>.*href=\"?([^\"]+)\"?>(.*)</a></strong>.*<blockquote>.*<em>(.*)</em>.*</blockquote>(.*)",
  );

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

# Determine if we are running as an administrator
unless ($ENV{SCRIPT_NAME} =~ /admin/)
{
    &error("This program must be run from the admin subdir.");
}

if (! defined %in)
{
    print &bib::template("basic.html", "DATA",
                         &bib::template("convert.html"));
    exit 0;
}
else
{
    $MULTILINE_MATCHING = 1;

    $entry = $in{entry};
    undef %in;

    $vars = "<FONT SIZE=2><PRE>" . &untaint($entry) . "</PRE></FONT>";

    $entry =~ s/\n/ /g;
    $entry =~ s/\s+/ /g;

    # Try to figure out what kind of entry it is
    if ($entry =~ /$regexp{book}/i)
    {
        $in{TITLE} = $1;
        $in{AUTHOR} = $2;
        $in{MISC} = $3;
        $in{KEYWORDS} = $4;
        $in{COMMENT} = $5;

        if ($in{MISC} =~ s/(19\d\d),?//i)
        {
            $in{YEAR} = $1;
        }

        if ($in{MISC} =~ s/^(.*);//i)
        {
            $in{PUBLISHER} = $1;
        }
    }
    elsif ($entry =~ /$regexp{internet}/i)
    {
        # $url, $title, $keywords, $comment
        $in{URL} = $1;
        $in{TITLE} = $2;
        $in{KEYWORDS} = $3;
        $in{COMMENT} = $4;
        $in{CATEGORY} = "internet";
    }
    else
    {
        $in{COMMENT} = $entry;
    }
    $MULTILINE_MATCHING = 0;

    # Fix the inputs
    @keywords = split(/\s*,\s*/, $in{KEYWORDS});
    grep(s/^\s+//, @keywords);
    grep(s/\s+$//, @keywords);
    grep(s/\s+/_/g, @keywords);
    grep(s/[^\w:-]//g, @keywords);
    $in{KEYWORDS} = "@keywords";
    $in{COMMENT} =~ s/<p>//i;

    %in = &remove_spaces(%in);

    &bib::load("CATEGORY", "KEYWORDS");

    # Get the categories
    @categories = &bib::get_categories();
    &error($bib::error) unless defined @categories;

    # Get the keywords
    @keywords = &bib::get_keywords(1);
    &error($bib::error) unless defined @keywords;

    # Construct a list of all current categories
    # and make sure the correct one is selected
    # if we are editing an existing entry
    foreach $category (@categories)
    {
        $select = ($category eq $in{CATEGORY}) ? "SELECTED" : "";
        $categories .= "<OPTION $select>$category";
    }
    $in{CATEGORY} = $categories;

    # Construct a list of all current keywords
    # and make sure the correct ones are selected
    # if we are editing an existing entry
    foreach $keyword (@keywords)
    {
        #$select = ($in{KEYWORDS} =~ /\b$keyword\b/) ? "SELECTED" : "";
        $keywords .= "<OPTION $select>$keyword";
    }
    $in{NEW_KEYWORDS} = $in{KEYWORDS};
    $in{KEYWORDS} = $keywords;

    $data = &bib::template("edit.html", %in,

                           "FORM_ACTION",
                           "ACTION=$bib::admin_cgi/edit.cgi",

                           "NEW_CATEGORY",
                           "<INPUT NAME=NEW_CATEGORY VALUE=\"\">",

                           "REVERT",
                           "<INPUT TYPE=reset VALUE=\"Erase Changes\">",

                           "VARS", $vars,
                           "HEADING", "Create Entry",
                          );
    print &bib::template("basic.html",
                         "TITLE", "Convert Old Entry",
                         "DATA", $data);
}

exit 0;


sub remove_spaces
{
    my(%in) = @_;

    foreach (keys %in)
    {
        $in{$_} =~ s/^\s+//;
        $in{$_} =~ s/\s+$//;
        $in{$_} = &untaint($in{$_});
    }
    %in;
}
