#!/usr/bin/perl

use strict;

my $USAGE = "USAGE: $0 TEX_FILE BIB_FILE\n";

if (scalar (@ARGV) != 2) {
    die $USAGE;
}

my $texfile = shift;
$texfile =~ s/\.tex$//i;
$texfile .= ".aux";
open (TEX, "$texfile") or die ("Cannot open $\texfile\n");
my %texcites;
while (<TEX>) {
    if (/\\citation{(.*)}/) {
	$texcites{$1}=1;
    }
}
close TEX;
my $bibfile = shift;
open (BIB, "$bibfile") or die ("Cannot open $\bibfile\n");
my %bibcites;
while (<BIB>) {
    if (/\@.*{(.*),/) {
	$bibcites{$1}=1;
    }
}
close BIB;

my %texminusbib;
my %bibminustex;

foreach my $key (keys %texcites) {
    if (!exists($bibcites{$key})) {
	$texminusbib{$key} = 1;
    }
}
foreach my $key (keys %bibcites) {
    if (!exists($texcites{$key})) {
	$bibminustex{$key} = 1;
    }
}

if (scalar %texminusbib) {
    print "Citations not found:\n";
    foreach my $key (keys %texminusbib) {
	print "  $key\n";
    }
    print "\n";
} else {
    print "All citations found\n";
}

if (scalar %bibminustex) {
    print "References not cited:\n";
    foreach my $key (keys %bibminustex) {
	print "  $key\n";
    }
    print "\n";
} else {
    print "All references cited\n";
}


