#!/usr/bin/perl
#
# Author:      Norbert Klasen
# E-mail:      klasen@zdv.uni-tuebingen.de
# 
# $Id: l2b.pl,v 1.4 2001/04/06 18:07:38 zrdkn01 Exp $
#
# Description: queries ldap server for bibtex entries references in aux file
#              
# Redistribution and use in source and binary forms are permitted
# provided that the above copyright notice and this paragraph are
# duplicated in all such forms and that any documentation,
# advertising materials, and other materials related to such
# distribution and use acknowledge that the software was developed
# by Pierangelo Masarati.
#
# THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
# WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#

use Getopt::Std;			# To parse command line arguments.
use Net::LDAP;

use strict;
use vars qw($opt_b $opt_h $opt_p $opt_D $opt_w);

if (!getopts('b:h:p:D:w:'))
{
   print "usage: [-h host] [-p port] -b base [-D bindDN [-w passwd]] AUXFILE\n";
   exit;
}

$opt_h = 'localhost' unless $opt_h;
$opt_p = 389 unless $opt_p;


my @files = ();
my %citations = ();
my @attrs = qw ( cn labeledUri objectclass bibtexString bibtexAddress bibtexAuthor bibtexBookTitle bibtexChapter bibtexCrossRef bibtexEdition bibtexEditor bibtexHowPublished bibtexInstitution bibtexJournal bibtexKey bibtexMonth bibtexNote bibtexNumber bibtexOrganization bibtexPages bibtexPublisher bibtexSchool bibtexSeries bibtexTitle bibtexType bibtexVolume bibtexYear ); 
#print "attrs: @attrs\n";

push @files, shift;
my $file;
while ( $file = shift @files ) {
	parse_auxfile($file);
}

my $ld = Net::LDAP->new($opt_h, port => $opt_p, die => "$@");
if ( $opt_D ) {
	$ld->bind ( dn => $opt_D, password => $opt_w);
}
my $citation;
foreach $citation ( sort keys %citations ) {
	get_citation( $citation );
#	print "$citation\n";
}

exit( 0 );


#subroutines

sub parse_auxfile()
{
	my $file = $_[0];
	open(AUX, "<$file") or die $!;
	while (<AUX>) {
		if ( /^\\\@input\{(.*\.aux)\}/ ) {
			push @files, $1;
			next;
		}
		if ( /^\\citation\{(.*)\}/ ) {
			#\citation{RFC2829}
			$citations{$1} = 1;
			next;
		}
	}
	close(AUX);
}

sub get_citation() {
	my $cn = $_[0];
	my $mesg = $ld->search( base => $opt_b, scope => 'sub',
		filter => "(&(objectclass=bibtexEntry)(cn=$cn))", attrs => \@attrs );
	$mesg->code && die $mesg->error;
	if ( $mesg->count == 0 ) {
		print STDERR "no entry for $cn\n";
		return;
	}
	if ( $mesg->count > 1 ) {
		print STDERR "more than one entry for $cn, using the first one\n";
	}
	my $entry = $mesg->entry(0);

	my $oc;
	foreach $oc ( $entry->get_value("objectclass") ) {
		next if $oc eq "bibtexEntry";
		if ( $oc =~ /^bibtex(.*)$/ ) {
			print "@".lc($1)."{$cn,\n";
		}	
	}
	
	my @attributes = $entry->attributes( nooptions => "TRUE" );
	my ($attribute, $value, $field, $url, $label, $title);
	foreach $attribute ( sort @attributes ) {
		if ( $attribute =~ /labeledUri/i ) {
			($url, $label) = split / /, $entry->get_value( "labeledUri") ;	
		}
		if ( $attribute =~ /^(bibtex(.*))/i ) {
			$field = $2;
			if ( $entry->exists("$1;lang-x-tex") ) {
				$value = $entry->get_value( "$1;lang-x-tex" );	
			} else {
				$value = $entry->get_value( $1 );
			}
			if ( $field =~ /title/i ) {
				$title = $value;
				next; #test for url later
			}
			print "\t".lc($field)." = ";
			if ( $field =~ /^(year|month)$/i ) { #or string
				print "$value,\n";
			} else {
				print "\"$value\",\n";	
			}
		}
	}
	if ( $url ) {
		print "\ttitle = \"\\href\{$url\}\{$title\}\",\n";
	} else {
		print "\ttitle = \"$title\",\n";
	}
	print "}\n\n"
}


