#!/usr/bin/perl

use CGI qw(:standard);
print header;

my $FILE = "../XML/papers.xml";
my $res = "";
my $key = param('key');
my $item = param('item');
my $home = "../";

if ($item eq "abs") {
    $tag = "abstract";
    $header = "Abstract";
} else {
    $tag = "bibtex";
    $header = "BibTex entry";
}

open(FILE, $FILE) or die("Could not open $FILE.: $!\n");
while (<FILE>) {
    # at this point, not inside <Paper>...</Paper> tags
    # We know that the xml file has these tags alone on a line, which
    # makes parsing easier.

    if (/<Paper .*>/) {
        # inside <Paper>...</Paper>
        # gather everything for this entry
        my $paper = "";
        while (<FILE>) {
            if (/<\/Paper.*>/) {
                # end of the paper
                last;
            }
            chomp;
            $paper = $paper . "$_<BR />";
        }

        # get the key
        if ($paper =~ m/<key>(.*?)<\/key>/) {
            # if the key matches, read the bibtex entry and quit
            if ($1 eq $key && $paper =~ m/<$tag>(.*?)<\/$tag>/) {
                 $res = $1;
                 last;
            }
        }
    }
}

close(FILE);

print start_html($header);
print $res;
print p("Go back to ", a({href=>$home}, "Angelina's homepage"));
print end_html;
