#!/usr/bin/perl
# massage AER XML files
print "\naer v1.0.1\n\n";
print "C F Baum  Jul 2001\n\n";
use LWP::Simple;
use Text::Wrap;
open(IN,"/users/baum/doc/repec/aea/aer.list") || die 'cannot open aer.list';
$nrtem=0;
while (<IN>) {
chomp;
print "processing $_ \n";
$chunk = get("http://www.aeaweb.org/aer/contents/sgml/AER_$_");
#print "$chunk\n";
$outf=">/users/baum/doc/repec/aea/aer$_.rdf";
open(OUT,$outf) || die "Cannot open $outf for output";
$chunk =~ s/\n/ /ig;
$chunk =~ s/\r/ /ig;
$chunk =~ s/&ast;//ig;
$chunk =~ s/&ouml;/o/ig;
(@str) = split("<\/head>",$chunk);
$nstr=@str;
print " $nstr articles in chunk $_\n";
foreach $str (@str) {
#print   wrap(" "," ",$str);
($jnl) = ($str =~ /.+jrnti>(.+?)<\/jrnti/);
($vol) = ($str =~ /.+vol>(.+?)<\/vol/);
($iss) = ($str =~ /.+<iss>(.+?)<\/iss>/);
($pubdt) = ($str =~ /.+cd>(.+?)<\/cd/);
($pubmo,$pubyr) = ($pubdt =~ /(.+?) (\d{4})/);
#print "\n $pubdt|$pubyr|$pubmo\n";
($pp1) = ($str =~ /.+ppf>(.+?)<\/ppf/);
($pp2) = ($str =~ /.+ppl>(.+?)<\/ppl/);
$ti="";
($ti) = ($str =~ /.+ti>(.+?)<\/ti/);
$abst="";
($abst) = ($str =~ /.+ab>(.+?)<\/ab/);
($url) = ($str =~ /.+<art_url>(.+?)<\/art_url/);
($doi) = ($str =~ /.+<doi>(.+?)<\/doi/);
($dataset) = ($str =~ /.+<dataset>(.+?)<\/dataset/);
#if (length $ti > 1) {
#print OUT "\nTemplate-Type: ReDIF-Article 1.0\n";
($aug) = ($str =~ /.+augp>(.+?)<\/augp>/);
(@au) = ($aug =~ /.+?au>(.+?)<\/au/ig);
$au=@au;
#print "N authors: $au\n";
if (length $ti > 1 & $au > 0) {
print OUT "\nTemplate-Type: ReDIF-Article 1.0\n";
$etal="";
$et="";
$etal = ($aug =~ m/<etal>/ig); 
if ($etal) {$et=" et al." ;}
foreach $i (@au) {
#print "\n $i\n";
($f) = ($i =~ m/gnm>(.+?)<\/gnm/ig);
($m) = ($i =~ m/nmsufx>(.+?)<\/nmsuf/ig);
($l) = ($i =~ m/snm>(.+?)<\/snm/ig);
#($f,$m,$l) = ($i =~ m/fnms>(.+?)<\/fnm.+?<inits>(.+?)<\/init.+?<snm>(.+?)<\/snm/);
#print "\n |$f|$m|$l| \n";
print OUT "Author-Name: $f $l $m $et\n";
}

print OUT "Title: $ti \n";
if (length $abst > 1) {
	print OUT  "Abstract:\n";
	print OUT  wrap(" "," ",$abst);
	print OUT "\n";
	}
print OUT "Journal: $jnl \n";
print OUT "Pages: $pp1-$pp2 \n";
print OUT "Issue: $iss \n";
print OUT "Volume: $vol \n";
print OUT "Year: $pubyr \n";
print OUT "Month: $pubmo \n";
print OUT "File-URL: http://hdl.handle.net/$doi \n";
print OUT "File-Format: text/html\n";
print OUT "File-URL: $url \n";
print OUT "File-Format: application/pdf\n";
print OUT "File-Restriction: Access to full text is restricted to AEA members. \n";
if (length $dataset > 1) {
	print OUT "File-URL: $dataset \n";
	print OUT "File-Format: application/zip \n";
	print OUT "File-Function: dataset accompanying article\n";
}
print OUT "Handle: RePEc:aea:aecrev:v:$vol:y:$pubyr:i:$iss:p:$pp1-$pp2 \n";
$nrtem++;
}
}
}
print " $nrtem templates processed \n";
exit;
__END__

