#!/usr/bin/perl
# massage IMF WP html page, access 'abstracts' where feasible from IMF website
print "\nIMFwpcvt v1.0\n\n";
print "C F Baum  Dec 1999\n\n";
use LWP::Simple;
use Text::Wrap qw(wrap $columns);
$columns=68;
$base="http://www.imf.org/EXTERNAL/PUBS/CAT/doctext.cfm?docno=WP/";
$infilename="IMFwp.html";
open(IN,$infilename) || die "Cannot open $infilename for input";
$nrtem=0;
$ttem=0;
$in=0;
$yro=-1;
$outfilename="NULL";
@auv={};
while($line=<IN>) {
	$line =~ s!\012!!ig;
	if($line =~ /Title:/ig){$in++; next};
	if ($in) {($title) = ($line =~ /^.+?>(.+?)</); $in=0;}
	if($line =~ /Author\/Editor:/ig){($au) = ($line =~ m!.+?\/b>(.+?)<!); 
# drop element of author line after last ; (usually dept affil)
		($au1)= ($au =~ /(.+);/);  
		(@auv)= split /;/, $au1; $auv=@auv;  next};
	if($line =~ /Series:/ig) {
		($yr,$wpn)=($line =~ m!WP/(.+?)/(.+?)$!); 
		if($yr!=$yro){
			if($nrtem>0){
				close OUT;
				print "\ntemplates created for $yro: $nrtem\n";
				$ttem=$ttem+$nrtem;
				$nrtem=0;
				}
			$outfilename=">imfwpa$yr.rdf";
			open(OUT,$outfilename) || die "Cannot open $outfilename for output";
			$yro=$yr;
		}
		$cwp = $yr.$wpn;
		if($wpn<10) {$cwp = $yr."0".$wpn;}
		$str=get "$base$yr/$wpn-EA";
		$abst="";
		if (!($str =~ /Sorry/ig)) {
			$str =~ s!\012!!ig;
#			($abst) = ($str =~ m!EAWP/$yr/$wpn(.+)!ig);
			($abst) = ($str =~ m!WP/$yr/$wpn(.+)!ig);
			$abst =~ s!-EAWP/$yr/$wpn!!ig;
			$abst =~ s!-EA WP/$yr/$wpn!!ig;
			$abst =~ s!-EA .!!ig;
			$abst =~ s!-EA!!ig;
#			$abst =~ s!Prepared by !!ig;
#			$abst =~ s!<BR> <BR>!<BR>!ig;
#			$abst =~ s!<BR><BR>!<BR>!ig;
#			$aulast= $auv[$auv-1]; 
#			$aulast =~ s/,.+//;
#			$abst =~ s!.+$aulast !!ig;
			$abst =~ s!<BR>!!ig; 
			$abst =~ s!</TD>.+</HTML>!!ig; 
			$abst =~ s!\015!!ig;
			$abst =~ s!\t!!ig;
			}
		if($auv) {  print OUT "\nTemplate-Type: ReDIF-Paper 1.0\n";
					print OUT "Title: $title\n";
					foreach(@auv) { if(!($_ =~ m/Department|Institute/i)) {print OUT "Author-Name: $_\n";
									print OUT "Author-Workplace: International Monetary Fund\n"}};
					if($abst) {print OUT "Abstract: ",wrap(""," ",$abst),"\n";}
					print OUT "Series: International Monetary Fund Working Papers\n";
					print OUT "Number: $yr/$wpn\n";
					print OUT "Creation-Date: 19$yr\n";
					if ($yr>96) {
							print OUT "File-URL: http://www.imf.org/external/pubs/ft/wp/19$yr/wp$cwp.pdf\n";
							print OUT "File-Format: application/pdf\n";
							print OUT "File-Function: main text\n";
							}
					print OUT "Handle: RePEc:boc:imfwpa:$cwp\n";
					$nrtem++;
				}
		}
	}
	close OUT;
	print "\ntemplates created for $yro: $nrtem\n";
	$ttem=$ttem+$nrtem;
	print "\ntotal templates created: $ttem\n\n";
exit;
