#!/kolab/bin/perl -w
use strict;
use Getopt::Long;
use URI;
use Net::LDAP;
use Net::LDAP::LDIF;
use Net::LDAP::Entry;
use Kolab;
use Kolab::DirServ;
use Kolab::LDAP;
use vars qw($name $mode);

GetOptions(
    "name=s"    => \$name,
    "mode=s"    => \$mode
); 

exit 0 if !defined $mode;

my $ldap;

$ldap = Kolab::LDAP::create(
    $Kolab::config{'ldap_ip'},
    $Kolab::config{'ldap_port'},
    $Kolab::config{'bind_dn'},
    $Kolab::config{'bind_pw'}
) || die;

my $mesg = $ldap->search(
    base    => $Kolab::config{'base_dn'},
    scope   => 'one',
    filter  => "(&(cn=*$name*)(mail=*))"
);
$mesg->code && die $mesg->error;

my $max = $mesg->count;
$max || die "Nothing to do!";

for (my $i = 0; $i < $max; $i++) {
    my $entry = $mesg->entry($i);
    if ($mode =~ /add/) {
        Kolab::DirServ::notifyNew($entry);
    }
    if ($mode =~ /del/) {
        Kolab::DirServ::notifyRemove($entry);
    }
    if ($mode =~ /upd/) {
        Kolab::DirServ::notifyModify($entry);
    }    
}
