#!/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>
<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 width=\"50%\" align=\"top\" alt=\"Society for Judgment and Decision Making\" src=\"sjdmLOGOgv_e.png\">
&nbsp;&nbsp;
<img width=\"35%\" style=\"margin-top: 45px;\" alt=\"European Association for Decision Making\" src=\"eadm.png\">
</td>
</tr>
<tr>
<td bgcolor=\"#dddddd\" width=\"20%\"> 
<P>For citations of each article, in several formats, see
<a href=\"http://econpapers.repec.org/article/jdmjournl/\">RePEc</a>
<p><a href=\"http://journal.sjdm.org/\">Journal Home Page</a>
<P>ISSN 1930-2975<!-- LOCKSS system has permission to collect, preserve, and serve this open access Archival Unit -->
</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. Some data sources are listed in articles (not here).

";

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
print OUT "\n<br>Data: <a href=\"".$p->{"html"}."\"></a>";
# end of addition, still need to change url
  }
}

print OUT "\n\n<p><hr><br>Web page maintained by <a href=\"mailto:jonathanbaron7\@gmail.com\">
<cite>jonathanbaron7\@gmail.com</cite></a>;
image by Gaëlle Vallée-Tourangeau.
</td>
</tr>
</tbody></table>

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