#!/usr/bin/perl

open(IN,$ARGV[0]) or die "Could not open file $ARGV[0] for input.\n";
#open(OUT,">",$ARGV[1]) or die "Could not open file $ARGV[1] for output.\n";

@inputfile = <IN>;
close IN;
@paperhash = (); # A list of hashes representing paper qualities
$thishash = {};
foreach $p (@inputfile) { # read lines from the input file
  if ($p eq "\n") { # A blank line indicates that we are talking about a new paper now!
    push(@paperhash,$thishash);
    $thishash = {}; # Create a key/value hash representing this article
  }
  foreach $kv (split(/\n/gs,$p)) {
    chomp $kv;
    ($key,@value) = split(/=/,$kv); # The part before the = is the key; the part after is the value.
    $key = 'issue' if $key eq 'number';
    $val = join("=",@value); # But values can contain the '=' symbol character
#    print "line is $kv, key $key, val $val, scalar is ".scalar(@paperhash)."\n";
    if ($key eq "author") {
      # Fix lname middle lastname -> lastname, first middle
      # This is a best guess kind of thing.
      # Last name is the final word, may be hyphenated, may include
      # prefixes "van", "van der", "van den", "de",
      # May have suffixes Jr Sr II III
      # Don't modify if it already looks right.
      unless ($val =~ /,/ and $val !~ /, ?(Jr|Sr|II|III)\.?$/) {
        my($suffix,$lname);
        # Extract suffix, if any.
        $suffix = $1 if $val =~ s/, ?(Jr\.?|Sr\.?|II|III)$//;
        # Extract last name
        $val =~ s/ (((van|van der|van den|de) )?\S+$)//i;
        $lname = $1;
        $val = "$lname, $val";
        $val .= ", $suffix" if $suffix;
      }
      push(@{$thishash->{$key}},$val); # Authors are always in an ORDERED list
    }
    else {
      $thishash->{$key} = $val; # Other fields are not repeated, so the last one listed is the one used
    }
  }
}

foreach $p ( sort { $a->{"startpage"} <=> $b->{"startpage"} } @paperhash ) { # If the .dat was not in page order, make sure the output is
  next unless $p->{"title"}; # Skip this item if the paper has no title (probably an extra newline in the data file)
  @outfile = split('/',$p->{html});
  $outfile = @outfile[0];
  open(OUT,">>","allvols.ris");
  print OUT "TY  - JOUR\n";
  foreach $author ( @{$p->{"author"}} ) {
    print OUT "A1  - $author\n";
  }
  print OUT <<EOP;
T1  - $p->{title}
JO  - Judgment and Decision Making
Y1  - $p->{year}
VL  - $p->{volume}
IS  - $p->{issue}
SP  - $p->{startpage}
EP  - $p->{endpage}
KW  -
UR  - http://journal.sjdm.org/$p->{html}
L1  - http://journal.sjdm.org/$p->{pdf}
L2  - http://journal.sjdm.org/$p->{html}
N2  - 
ER  -

EOP
}

