#!/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.
    $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") {
      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
    }
  }
}

$p = $paperhash[0]; # Assumes that year, journal, volume, issue, month are all correct in the first paper!
print OUT "
<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">
<html><head>


<title>Judgment and Decision Making, vol. ".$p->{"volume"}.", no. ".$p->{"issue"}."</title>
<link rel=\"stylesheet\" type=\"text/css\" href=\"http://journal.sjdm.org/sjdm.css\">
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">
</head><body>
<h2>Citations for all articles</h2>

";

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)
  # My clever code to change the perl array of authors into an APA-style reference list
  map {
    my @foo = split(/\s+/,$_); # Split an author into names
    my $result = pop(@foo).", "; # Keep the last name, add a comma and a space
    map { s/^(\w).*/\1\./ } @foo; # Keep the first letter of every OTHER name; add a period after it
    $_ = $result.join(" ",@foo); # Join the last-name-with-comma to the initials, space-delimited.
  } @{$p->{"author"}};
  $a = scalar(@{$p->{"author"}});
  if ($a == 1) {
    print OUT ${$p->{"author"}}[0];
  }
  else {
    ${$p->{"author"}}[$a-1] = "\&amp; ".${$p->{"author"}}[$a-1];
    print OUT join(", ",@{$p->{"author"}});
  }

  print OUT " (".$p->{"year"}."). ".$p->{"title"};
  print OUT "." unless ($p->{"title"} =~ /\W$/);
  print OUT " <i>Judgment and Decision Making, ".$p->{"volume"}."</i>, ".$p->{"startpage"}."-".$p->{"endpage"}.". <br>(<a href=\"".$p->{"pdf"}."\">pdf</a>, <a href=\"".$p->{"html"}."\">html</a>)<br><br>\n";
}

print OUT "<p><hr><br>Web page maintained by <a href=\"mailto:baron\@psych.upenn.edu\">
<cite>baron\@psych.upenn.edu</cite></a>;
Adam Kramer wrote software to help automate the construction of this page, as well as
performing other functions.

</body></html>\n";
