package niupepautil;

use util;
use doc;
use strict;

# reads in the meta.txt file and sets metadata
sub set_main_metadata {
    my ($doc_obj, $dir, $verbosity) = @_;

    my $metafile = &util::filename_cat ($dir, "meta.txt");
    return unless (-e $metafile);

    if (!open (METAFILE, $metafile)) {
        print STDERR "NPPlug: Couldn't read $metafile\n" if $verbosity;
        return;
    }

    my $cursection = $doc_obj->get_top_section();
    my $line = "";
    while (defined ($line = <METAFILE>)) {
        next unless $line =~ /\w/;
        chomp $line;
        if ($line =~ /<([^>]*)>(.*)$/) {
            # note we're using set_metadata_element (not add_metadata_element)
            # this will override any previously set metadata of the same name
            $doc_obj->set_utf8_metadata_element ($cursection, $1, $2);
        } elsif ($verbosity) {
            print STDERR "niupepautil: Badly formatted line in $metafile\n";
            print STDERR "meta.txt lines should be formatted '<metaname>metavalue'\n";
        }
    }
}

sub get_title_string {
    my ($paper_id) = @_;
    print STDERR "paper_id = $paper_id\n";
    my ($series, $vol, $num) = split /\_/, $paper_id;
    my $title = "";
    $title .= "_vol_ $vol" if defined $vol && $vol =~ /\w/;
    if (defined $num && $num =~ /\w/) {
        $title .= ", " if defined $vol && $vol =~ /\w/;
        $title .= "_num_ $num";
    }
    return $title;
}


1;
