#!/usr/bin/perl -w

# marius aamodt eriksen (marius@umich.edu)

use Net::LDAP;
use strict;

my $q = $ARGV[0];
my $results = 0;

# first try abook

my $addr = `/usr/local/bin/abook --mutt-query $q`;

if ($addr !~ /Not found/) {
	print $addr;
	$results++;
}

my $ldap = Net::LDAP->new('slaves.dir.itd.umich.edu') or die "$@";
$ldap->bind;

my $mesg = $ldap->search ( base   => "o=University of Michigan, c=US",
						   filter => "(|(cn=$q) (uid=$q) (sn=$q))" );

$results += $mesg->count;

if (!$results) {
	print "Not found ($q)\n";
	exit;
}

if ($results == $mesg->count) { print "\n"; }
my ($entry, @ref);
foreach $entry ($mesg->all_entries) { 
	@ref = $entry->get_value('mail') ;
	if (!$ref[0]) { next; }
	print lc($ref[0])."\t";
	@ref = $entry->get_value('cn') ;
	$ref[0] =~ s/(.*)1$/$1/;
	print lc($ref[0])."\n";
}

$ldap->unbind;   # bye bye
