#!/usr/bin/perl

$t=`cat $0`; $i=0;  $bib="";  $nb="";
foreach(split('\n',$t)){ 
  #  print STDERR "i=$i $_\n";
  if(/^__END__$/){$i=1;} if($i==0){next;}
  if(/^\s*$/){ next; }  #.... blank lines
  #  print STDERR "i=$i $_\n";
  if(/doi:\s*(.+)\s*$/i){  $doi=$1;
     print STDERR "-------------- found doi: $doi ---------------\n";
     #  print STDERR "curl -LH \"Accept: application/x-bibtex;q=1\" 'http://dx.doi.org/$doi'\n";
     $x=`curl -LH "Accept: application/x-bibtex;q=1" 'http://dx.doi.org/$doi'`;
     $x=~s/\$\\upmu/\$\\mu/g;
     $x=~s/July/\{July\}/g;
     $x=~s/September/\{September\}/g;
     $x=~s/n\/a\-\-n\/a/1/;  #... no page number given
     #  $x=~s/n\/a\-\-n\/a/doi:$doi/;
     print STDERR "$x\n";
     if($x=~/year =/){ $bib="$bib$x\n"; } else{  $nb="$nb$_\n\n"; }
     sleep(3);
  }
  else{  print STDERR "no doi: $_\n\n"; $nb="$nb$_\n\n"; }
} 
dumpf($bib,"tmp.bib"); print $bib;
dumpf($nb,"tmp.nb");

sub dumpf{ local($c,$f)=@_; open(DF,">$f"); print DF $c; close(DF); }
__END__

DOI: 10.1029/2011GM001200 
doi:10.1088/1742-6596/767/1/012021
DOI:10.1029/2012JA017972
doi:10.1038/ncomms8019


