#!/usr/local/bin/perl -w
#                (written using perl4)
# note:
# new converter. You can find it at 
# ftp://ftp.tex.ac.uk/tex-archive/biblio/bibtex/utils/isi2bibtex/isi2bibtex
# now availabel in my bin
#
#               ========================                       23 Jan  97
#               ====   bids2bib.p   ====     (c) David MacKay  24 June 95
#               ====   version 2    ====         mackay@mrao.cam.ac.uk
#               ========================         http://www.inference.phy.cam.ac.uk/
# usage: 
#               bids2bib.p file1 > file2
#
##########################################################################
#
#   BIDS to BIBTEX converter
#
#     ----- reads bids `download' format and outputs LaTeX/BibTeX bib style
#
#  *** this is free software as defined by the free software foundation ***
#
# Example:
#          if the file tmp contains the following (which you might get from 
# BIDS by selecting "P" to output marked items, then selecting `download'
# format [e.g. menu number 6])...
#
# TI- ALGEBRAIC-GEOMETRIC CODES AND ASYMPTOTIC PROBLEMS
# AU- TSFASMAN, MA
# JN- DISCRETE APPLIED MATHEMATICS
# PY- 1991
# VO- 33
# NO- 1-3
# PG- 241-256
#
# TI- BAYESIAN INTERPOLATION
# AU- MACKAY, DJC
# NA- CALTECH 139-74,COMPUTAT & NEURAL SYST,PASADENA,CA,91125
# JN- NEURAL COMPUTATION
# PY- 1992
# VO- 4
# NO- 3
# PG- 415-447
#
# ...then the command:
#                          bids2bib.p tmp
#
# will output this to stdout: 
#
# @article{Tsfasman1991,
#   title={Algebraic-Geometric Codes and Asymptotic Problems},
#   author={Tsfasman, M. A.},
#   journal={Discrete Applied Mathematics},
#   year={1991},
#   volume={33},
#   number={1-3},
#   pages={241-256}
# }
#
# @article{Mackay1992,
#   title={Bayesian Interpolation},
#   author={Mackay, D. J. C.},
#   journal={Neural Computation},
#   year={1992},
#   volume={4},
#   number={3},
#   pages={415-447}
# }
#
# Notes:
#
# 1] Notice that some subtle changes are made:
#
#    a) selective decapitalization
#    b) addition of "." between initials
#    c) inclusion of a key made of the first author's name and the year
#
# 2] The file that is read may contain many such items. Each time the program 
# reaches a title "TI-", it assumes that a new item has started. 
#
# 3] Anything that isn't recognized is junked. 
# So for example, when you receive your output by email from bids,
# there is no need to remove the email header. Just shove it straight
# into bids2bib.p
#
# Additions in version 2:
# Instead of " " surrounding fields, { } is used.
#
# version 1 used " as the delimiters instead: 
#
# @article{Tsfasman1991,
#   title="Algebraic-Geometric Codes and Asymptotic Problems",
#   author="Tsfasman, M. A.",
#   journal="Discrete Applied Mathematics",
#   year="1991",
#   volume="33",
#   number="1-3",
#   pages="241-256"
# }
#
# @article{Mackay1992,
#   title="Bayesian Interpolation",
#   author="Mackay, D. J. C.",
#   journal="Neural Computation",
#   year="1992",
#   volume="4",
#   number="3",
#   pages="415-447"
# }
#

$allowspace = 1 ; 
if ( 2 ) {
    $left = '{' ;
    $right = '}' ;
} else {
    $left = '"' ;
    $right = $left ; 
}
$writing = 0 ; 
$stillgoing = 0 ; 

&readline();
do {
    if ( /^\w\w\-/ || ( $allowspace && /^\s*\w\w\-/ ) ) {
	# we are entering a new field
	if ( /^\s*TI/ ) {
	    # it's a new item
	    &close_item() ;
	    &open_item() ;
	    &do_title() ; 
	}
	else {
	    if ( /^\s*AU/ ) {
		&continue_item();
		&do_author() ;
	    }
	    elsif ( /^\s*JN/ ) {
		&continue_item();
		&do_jn() ;
	    }
	    elsif ( /^\s*PY/ ) {
		&continue_item();
		&do_year() ;
	    }
	    elsif ( /^\s*VO/ ) {
		&continue_item();
		&do_vo() ;
	    }
	    elsif ( /^\s*NO/ ) {
		&continue_item();
		&do_no() ;
	    }
	    elsif ( /^\s*PG/ ) {
		&continue_item();
		&do_pg() ;
	    }
	    elsif ( /^\s*AB/ ) {
		&continue_item();
		&do_ab() ;
	    }
	    else { # it is a field of an unknown type
		if ( $stillgoing ) { &readline(); } 
	    }
	}
    }
    else { if ( $stillgoing ) { &readline(); } }
} while ( $stillgoing ) ; 
&close_item() ; 
exit 0 ;

sub open_item {
    $writing = 1 ; 
    $item = "" ;
    $item .= "\n\@article{REPLACE-AUTH-YEAR,\n" ;
}

sub close_item  {
    if ( $writing ) {
	$writing = 0 ; 
#	print $item ;
	$item =~ s/REPLACE-AUTH-YEAR/$author$year/ ; 
	$item .= "\n\}\n" ; 
	print $item ; 
    }
}

sub continue_item  {
    $item .= ",\n" ; 
}

sub do_title  {
    s/\s*TI\- /  title=$left/ ;
    do { 
	&decap () ;
	$item .= $_ ; 
    } while ( &readline() ) ; 
    $item =~ s/\n$//;
    $item .= $right ;
}
sub do_ab  {
    s/\s*AB\- /  abstract=/ ;
    do { 
	s/\"/\'/g;
	$item .= $_ ; 
    } while ( &readline() ) ; 
    $item =~ s/tract\=/tract=$left/;
    $item =~ s/\n$//;
    $item .= $right ;
}
sub do_author  {
    s/\s*AU\- // ;
    $item .= '  author='.$left ;
    $first = 1 ; 
    do { 
	s/(\, [A-Z])/$1\./g ;               # adds a . after 1st initial
	for ( $i = 1 ; $i <= 3 ; $i ++ ) { 
# this adds a . after 2nd,3rd,4th initials
	    s/\.([A-Z])([^.])/. $1.$2/g ;
	}
	s/[;]/ and /g ;
	&decap () ;
	$item .= $_ ; 
	if ( $first ) { ( $author )  =      /^\s*([^ ,]*)/ ; $first -- ; }
    } while ( &readline() ) ; 
    $item =~ s/\n$//;
    $item .= $right ;

}
sub do_jn  {
    s/\s*JN\- // ;
    $item .= '  journal='.$left ;

    do { 
	&decap () ;
	$item .= $_ ; 
    } while ( &readline() ) ; 
    $item =~ s/\n$//;
    $item .= $right ;
}
sub do_year  {
    s/\s*PY\- // ;
    $item .= '  year='.$left ;
    s/\n$//;
    s/\s//g;
    $year = $_ ;
    do { 
	$item .= $_ ; 
    } while ( &readline() ) ; 
    $item .= $right ;
}
sub do_vo  {
    s/\s*VO\- // ;
    $item .= '  volume='.$left ;
    s/\n$//;    s/\s//g;
    do { 
	$item .= $_ ; 
    } while ( &readline() ) ; 
    $item .= $right ;
}

sub do_no  {
    s/\s*NO\- // ;
    $item .= '  number='.$left ;
    s/\n$//;    s/\s//g;
    do { 
	$item .= $_ ; 
    } while ( &readline() ) ; 
    $item .= $right ;
}

sub do_pg  {
    s/\s*PG\- // ;
    $item .= '  pages='.$left ;
    s/\n$//;    s/\s//g;
    do { 
	$item .= $_ ; 
    } while ( &readline() ) ; 
    $item .= $right ;
}

sub readline {
    $ans = 0 ; 
    if ( $_ = <>  ) {
	$stillgoing = 1 ;
	if ( !( (/^\s*\w\w\-/) || /^\s*$/ ) ) {  $ans = 1 ; }
    }
    else { $stillgoing = 0 ; }
    return ( $ans ) ; 
}

sub decap  {
    s/([A-Z])([A-Z]*)/"$1\L$2"/eg ; 
    s/ And / and /g;
    s/ An / an /g;
    s/ A / a /g;
    s/ To / to /g;
    s/ With / with /g;
    s/ By / by /g;
    s/ Be / be /g;
    s/ Can / can /g;
    s/ The / the /g;
    s/ For / for /g;
    s/ Of / of /g;
    s/ On / on /g;
    s/ In / in /g;
    s/Dna/DNA/g;
    s/Nad\+/NAD+/g;
    s/Nmr/NMR/g;
    s/Ieee/IEEE/g;
    s/Iee/IEE/g;
    s/\&/and/g; # added march 97
}








