#!/usr/bin/perl
# input output: use this for making individual TOCs
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>
<table width=\"100%\">
<tbody><tr><td colspan=\"2\" bgcolor=\"#dddddd\">
<img alt=\"Society for Judgment and Decision Making\" src=\"http://journal.sjdm.org/sjdmLOGOgv_e.png\">
</td>
</tr>
<tr>
<td bgcolor=\"#dddddd\" width=\"15%\"> 
<a href=\"http://journal.sjdm.org/\">Journal Home Page</a>
<p><a href=\"http://www.sjdm.org/\">Society Home Page</a><p>
</p><hr>
ISSN 1930-2975
</td>
<td>
<p></p><h1>Judgment and Decision Making</h1> 
<h2>Volume ".$p->{"volume"}.", Number ".$p->{"issue"}.", ".$p->{"month"}." ".$p->{"year"}."</h2>
<hr>
<h2>Contents</h2>

The title is linked to the pdf version, which should be used for
printing, quotation, or citation.  The html version is for
convenience.

";

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)
  print OUT "
<p><a href=\"".$p->{"pdf"}."\">".$p->{"title"}."</a>, pp. ".$p->{"startpage"}."-".$p->{"endpage"}."
(<a href=\"".$p->{"html"}."\">html</a>).
<br>";
  # Make a perl array print like an English list.
  $a = scalar(@{$p->{"author"}});
  if ($a == 1) {
    print OUT ${$p->{"author"}}[0];
  }
  else {
    ${$p->{"author"}}[$a-1] = "and ".${$p->{"author"}}[$a-1];
    print OUT join(", ",@{$p->{"author"}});
  }
# added by JB
  if ($p->{"data"} ne "") {
    print OUT "<br>(<a href=\"".$p->{"data"}."\">data</a>)";
  }
# end of addition
}

print OUT "<p><hr><br>Web page maintained by <a href=\"mailto:baron\@psych.upenn.edu\">
<cite>baron\@psych.upenn.edu</cite></a>;
image by Gaëlle Villejoubert.
</td>
</tr>
</tbody></table>

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

