#!/usr/local/bin/perl
# mod of Michael Sandfort's wp.cgi, using LWP to obviate need for temp file I/O
# C F Baum 9A12

# Create a user agent object (following perldoc LWP)
use LWP::UserAgent;
$ua = new LWP::UserAgent;
$ua->agent("AgentName/0.1 " . $ua->agent);
$req = HTTP::Request->new(GET => 'ftp://anonymous:baum@ftp.bc.edu/pub/user/baum/RePEc/boc/bocoec/bocoecy.rdf');
# Pass request to the user agent and get a response back
my $res = $ua->request($req);
# Check the outcome of the response
if ($res->is_success) {  $file = $res->content} else { die  "Bad luck this time\n";}

open(OUT,">test.out") || die "Cannot open test.out";

(@lines)=split /\n/, $file;
$lines=@lines;

$all=@lines[0];
foreach (@lines) {

    if ( /^\s+/ ) {
	$all.=$_;
    }
    elsif ( /^\w/ ) {
		($_,$all)=($all,$_);

	if ( s/^Template-Type:\s+//i ) {
	    $Doc_Type=$_;
	}
	elsif ( s/^Author-Name:\s+//i ) {
	    push(@Authors,$_);
	}
	elsif ( s/^Author-Workplace-Name:\s+//i ) {
	    push(@Author_Affil,$_);
	}
	elsif ( s/^Author-Email:\s+//i ) {
	    $Contact=$_;
	}
	elsif ( s/^Title:\s+//i ) {
	    $Title=$_;
	}
	elsif ( s/^Abstract:\s+//i ) {
	    $Abstract=$_;
	}
	elsif ( s/^Classification-JEL:\s+//i ) {
	    $Codes=$_;
	}
	elsif ( s/^Keywords:\s+//i ) {
	    $Keywords=$_;
	}
	elsif ( s/^Length:\s+//i ) {
	    $Length=$_;
	}
	elsif ( s/^File-URL:\s+//i ) {
	    $URL=$_;
	}
	elsif ( /^Number: (\w+)/i ) {
	    $WP_ID=$1;
	}
	elsif ( /^Handle:/i ) {
	    &write_html;
	    @Authors=(); @Author_Affil=();$Abstract="";
	}
    }
}

sub write_header
{
    print OUT "Content-type:  text/html", "\n\n";
    print OUT "<html>", "\n";
    print OUT "<head><title>SMU Department of Economics</title></head>", "\n";
    print OUT "<body bgcolor=\"ffffff\">", "\n";
    print OUT <<End_of_Form;
<center>
<table border=0 width="100%">
<tr>
<td rowspan=3 align=center><img src="/smu_web/gifs/tseal.gif" href="http://www.smu.edu"></td>
</tr>
<tr>
<td colspan=2 align=center bgcolor="eeeeee"><font size=12>Department of Economics</font>
</tr>
<tr>
<td colspan=2 align=center><font size=5>Dedman College</font></td>
</tr>
</table>
</center>

<center>
<hr width="100%">
<p><br>
<p>
<h2>Working Papers-$wp_year</h2>
<p><br>
<p><br>
<p>
</center>
End_of_Form
}

sub write_html
{
    local($Author_list); $Author_list='';
    foreach $author (@Authors) {
	$Author_list.="$author; ";
    }
    chop($Author_list); chop($Author_list);
    $Abstract=~s/\n/<br>/g;
    $Title=~s/\n/<br>/g;
    print OUT <<End_of_Form;

<font color="0000ff"><h2><a href="$URL">$WP_ID</a></h2></font>
<h3>$Title</h3>
<b>$Author_list</b><br>
<p>
<b>Abstract:</b><br>$Abstract<br>
<p>
End_of_Form

}
exit;
