#!/usr/bin/perl

use strict;
use warnings;
use lib (qw(lib));

use LiCoM::Config (qw(get_config));
use LiCoM::Person;

our $Config = get_config ();

die unless (defined ($Config->{'uri'}) and defined ($Config->{'bind_dn'})
	and defined ($Config->{'password'}));

$Config->{'base_dn'} = $Config->{'bind_dn'} unless (defined ($Config->{'base_dn'}));

our @Patterns = ();
for (@ARGV)
{
	my $temp = $_;
	$temp =~ s/[^\.\*\w\s]//g;

	next unless ($temp);

	$temp =~ s/\**$/*/;
	push (@Patterns, [[lastname => $temp], [firstname => $temp], [mail => $temp]]);
}

die ('No (valid) patterns found.') unless (@Patterns);

our @Matches = LiCoM::Person->search (@Patterns, [[mail => '*']]);

print STDOUT scalar (@Matches), ' ', (scalar (@Matches) == 1 ? 'entry' : 'entries'), " found.\n";

for (@Matches)
{
	my $person = $_;
	my $cn = $person->name ();
	my @mail = $person->get ('mail');

	for (@mail)
	{
		print "$_\t$cn\tFound by LiCoM\n";
	}
}

LiCoM::Person->disconnect ();

exit (0);
