#!/usr/bin/perl -w

#----------------------------------------------------------------------
# copyright (C) 1999, 2000 e-smith, inc.
#		
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#		
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
# GNU General Public License for more details.
#		
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
# 
# Technical support for this program is available from e-smith, inc.
# For details, please visit our web site at www.e-smith.com or
# call us on 1 888 ESMITH 1 (US/Canada toll free) or +1 613 564 8000 
#----------------------------------------------------------------------

package esmith;

use strict;
use Errno;
use esmith::ConfigDB;
use esmith::AccountsDB;
use esmith::util;
use Net::LDAP;

my $c = esmith::ConfigDB->open_ro;
my $a = esmith::AccountsDB->open_ro;

my $l = $c->get('ldap');
my $status = $l->prop('status') || "disabled";
unless ($status eq "enabled" )
{
    warn "Not running action script $0, LDAP service not enabled!\n";
    exit(0);
}

my $hostname = $c->get('SystemName')
    || die("Couldn't determine system name");
 $hostname = $hostname->value;

my $domain = $c->get('DomainName')
    || die("Couldn't determine domain name");
 $domain = $domain->value;

my @accounts;
my $account;
my $event = shift || die "Event name must be specified";
if ($event eq 'ldap-update')
{
    @accounts = ($a->users);
}
else
{
    my $userName = shift;
    die "Username argument missing." unless defined ($userName);

    $account = $a->get($userName);
    die "Account $userName not found.\n" unless defined $account;
    my $type = $account->prop('type') || "unknown";

    die "Account $userName is not a user or group account; " .
	 "update LDAP entry failed.\n"
    unless ($type eq 'user');
    @accounts = ($account);
}

#------------------------------------------------------------
# Update LDAP directory entry. First read LDAP password
#------------------------------------------------------------
my $pw = esmith::util::LdapPassword();

#------------------------------------------------------------
# Update LDAP database entry.
#------------------------------------------------------------
my $base = esmith::util::ldapBase ($domain);

my $ldap = Net::LDAP->new('localhost')
    or die "$@";

$ldap->bind(
    dn => "cn=root,$base",
    password => $pw
);

foreach my $acct (@accounts)
{
    my $key = $acct->key;
    my $type = $acct->prop('type');
    next unless ($type eq 'user'); 
    my @attrs = ();

    my $fb = 'https://'.$hostname.'.'.$domain.'/horde/kronolith/fb.php?u='.$key.'@'.$domain;
    utf8::upgrade($fb);
    push @attrs, (calFBURL => $fb) unless $fb =~ /^\s*$/;

    my $dn = "uid=$key,ou=Users,$base";
    my %attrs = @attrs;
	
    $ldap->modify ($dn, add => {objectClass => 'calEntry'});
    my $result = $ldap->modify ($dn, replace => \%attrs);

    $result->code &&
      	warn "failed to modify entry for $dn: ", $result->error ;

}
$ldap->unbind;

exit (0);

