#!/usr/bin/perl
#
# $Id: aha2bib,v 1.2 1999/05/21 09:44:17 potse Exp $
#
# Convert American Heart Assoctiation abstract (persist.ini)
# to BibTeX format.
#

$usage = "\n  USAGE: aha2bib persist.ini abstract.bib\n\n";

$argc = $#ARGV + 1;
if($argc != 2){
  print $usage;
}
$infile = shift;
$outfile = shift;

open INFILE, $infile or die "Can't open $infile for reading";
open OUTFILE, ">$outfile" or die "Can't open $outfile for writing";

while(<INFILE>){
    if(/Proj=(.*)/){ $project = $1; }
       
    if(/P3..LNAM=(.*)/){ $first = $1; }
    
    if(/P3..DEPT=(.*)/){ $department .= "$1," ; }
    if(/P3..INST=(.*)/){ $institution .= "$1,"; }
    if(/P3..ADD.=(.*)/){ $address .= $1; }
    if(/P3..CITY=(.*)/){ $city .= "$1,"; }
    if(/P3..CTRY=(.*)/){ $country .= $1; }
    if(/P3..POST=(.*)/){ $address .= $1; }
    if(/P3..EMAI=(.*)/){ $email = $1; }
    
    if(/P4..CATE=[\w\.]+\s(.*)/){ $keywords .= $1; }
    if(/P4..KEY.=[\w\.]+\s(.*)/){ $keywords .= $1; }

    if(/P6..AUTO=([^\|]+)\|
                 ([^\|]*)\|
                 ([^\|]+)\|
                 ([^\|]*)\|
                 ([^\|]*)\|
                 ([^\|]*)\|
                 ([^\|]*)\|
                 ([^\|]*)\|
                 ([^\|]*)\|
                 ([^\|]+)\|
                 /x){
	if(length($4)>0){
	    push @authors, "$3, $4, $1 $2";
	}else{
	    push @authors, "$3,  $1 $2";
	}	    
    }
    
    if(/P7..TITL..=(.*)/){ $title .= $1; }
}

$label = "$first:$project";
$label =~  tr /[A-Z]/[a-z]/ ;

$author = join " and\n            ", @authors;
if($project =~ /([\d]+)/){
    $year = $1;
}else{
    $year = "?";
}

print OUTFILE "\@inproceedings{$label\n";
print OUTFILE "  author = {$author},\n";
print OUTFILE "  title = {$title},\n";
print OUTFILE "  booktitle = {$project},\n";
print OUTFILE "  year = {$year},\n";
print OUTFILE "  institution = {$department $institution $city $country},\n";
print OUTFILE "  e-mail = {$email}\n";
print OUTFILE "}\n\n";
