#!/usr/local/bin/perl
#
# READS FROM THE FILE CONTAINING THE CORRECT ORDERING
# OF THE BIBITEM LABELS AND EXTRACTS FROM THE SECOND
# ARGUMENT THOSE REFERENCES THAT CORRESPOND TO THE FIRST
# LIST.
# 
# USAGE: 
# % extr_cits_latex the_orden database > out
#
# READS FROM the_orden, EXTRACTS FROM database ...
#

open(LEE_FILE, "$ARGV[0]");

$i = 1;
while (<LEE_FILE>) {
   s/\'//g;
   s/\`//g;
   chop;
   $orden{$i}  = $_;
   $i++;
}
close(LEE_FILE);

$/ = "";
$* = 1;
open(EXT_FILE, "$ARGV[1]"); 
while (<EXT_FILE> {
	@a = split(' ',$_,2);
	$b = shift(@a);
	@d = split(/\{/,$b);
	chop(@d);
	$b = $d[1];
#	print "@a";
	if ( $c{$b} ne "" ) { 
		next;
	}
	else {
		$c{$b} = join(' ',@a);
	}
}

for ($j = 1; $j < $i; $j++) {
	print '\bibitem{',$orden{$j},'} ',$c{$orden{$j}};
}

print '\end{thebibliography}',"\n\n";
print '%=============================================================',"\n";
print '%		*********************************** ',"\n";
print '%=============================================================',"\n\n";
print '\vfill',"\n";
print '\eject',"\n\n";
print '\end{document}',"\n";

exit 0;