#!/usr/bin/env perl
# $Id: extractbib,v 1.3 2008/04/19 05:30:58 geni Exp $
#
# extractbib	extract from a big bib file the entries cited in *.bbl
# Author	Huidae Cho
# Since		March 25, 2008
# 
# Copyright (c) 2008 Huidae Cho
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.

use strict;

if($#ARGV < 2){
	print "extractbib bbl bigbib smallbib\n";
	exit;
}

open BBL, $ARGV[0] or die "Failed to open $ARGV[0]";
open BBIB, $ARGV[1] or die "Failed to open $ARGV[1]";
die "$ARGV[2] already exist!" if(-f $ARGV[2]);
open SBIB, ">$ARGV[2]" or die "Failed to open $ARGV[2]";

my @item = ();
my $i = -1;
my $found = 0;
while(<BBL>){
	s/\n//;
	if(m/^\\bibitem/){
		$found = 1;
		$item[++$i] = $_;
		next;
	}
	next unless($found);
	if($_ eq ""){
		$found = 0;
		$item[$i] =~ s/\[[^\]]*\]//g;
		$item[$i] =~ s/^\\bibitem\{(.*?)\}.*$/\1/;
		next;
	}
	$item[$i] .= $_;
}
my $regex = "";
foreach(@item){
	$regex .= "|" unless($regex eq "");
	$regex .= $_;
}
$regex =~ s/\./\\./g;
$/ = "\n}\n";
print SBIB "Generated by extractbib @ARGV\n\n";
while(<BBIB>){
	if(m/^@.*\{(?:$regex),$/om){
		s/^.*^@/@/ms;
		s/\n\t/ /mg;
		s/^  (?:comment|abstract|keywords|owner|pdf|review|timestamp).*\n//mg;
		s/,\n\}\n$/\n\}\n/;
		print SBIB "$_\n";
	}
}

close BBL;
close BBIB;
close SBIB;
