#!/usr/bin/perl -w

#----------------------------------------------------------------------
# copyright (C) 2002 Mitel Networks Corporation
# 
# 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 Mitel Networks 
# Please visit our web site www.mitel.com/sme/ for details.
#----------------------------------------------------------------------

package esmith;

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

my $c = esmith::ConfigDB->open_ro;
my $a = esmith::AccountsDB->open_ro;
my $domain = $c->get('DomainName')
    || die("Couldn't determine domain name");
$domain = $domain->value;
my $slapadd = "/usr/sbin/slapadd";

#------------------------------
# Remove existing LDAP database
#------------------------------
system ("/bin/rm -f /var/lib/ldap/*");

#---------------------------------------------------------------------
# Replace LDAP database either from backup, or from newly created data
#---------------------------------------------------------------------
unless (open(STDIN, "</home/e-smith/db/ldap/$domain.ldif"))
{
    # if we can't read a backup file, we fork, and feed new
    # data on stdin
    my $pid = open(LDAP, "|-");
    die "Fork failed: $!" unless defined $pid;

    $SIG{ALRM} = sub { die "whoops, $slapadd pipe broke" };
    if ($pid)
    {
	my $ldapBase = esmith::util::ldapBase ($domain);
	#------------------------------------------------------------
	# Create LDIF import file and rebuild LDAP database.
	#------------------------------------------------------------

	print LDAP "dn: $ldapBase\n";
	print LDAP "objectClass : organization\n";

	foreach my $user ($a->users)
	{
	    my $key = $user->key;
	    my $name = $user->prop('FirstName') . " " . $user->prop('LastName');
	    my $first = $user->prop('FirstName') || '';
	    my $last = $user->prop('LastName') || '';
	    my $phone = $user->prop('Phone') || '';
	    my $company = $user->prop('Company') || '';
	    my $dept = $user->prop('Dept') || '';
	    my $city = $user->prop('City') || '';
	    my $street = $user->prop('Street') || '';

	    print LDAP "\n";
	    print LDAP "dn: uid=$key,$ldapBase\n";
	    print LDAP "objectClass: person\n";
	    print LDAP "uid: "             . $key . "\n";
	    print LDAP "cn: $name\n" unless ($name =~ /^\s*$/);
	    print LDAP "givenName: $first\n" unless $first =~ /^\s*$/;
	    print LDAP "sn: $last\n" unless $last =~ /^\s*$/;
	    print LDAP "mail: $key\@$domain\n";
	    print LDAP "telephoneNumber: $phone\n" unless $phone =~ /^\s*$/;
	    print LDAP "o: $company\n" unless $company =~ /^\s*$/;
	    print LDAP "ou: $dept\n" unless $dept =~ /^\s*$/;
	    print LDAP "l: $city\n" unless $city =~ /^\s*$/;
	    print LDAP "street: $street\n" unless $street =~ /^\s*$/;
	}
	my $ldap = $c->get('ldap');
	my $phone = $ldap->prop('defaultTelephoneNumber') || '';
	my $company = $ldap->prop('defaultCompany') || '';
	my $dept = $ldap->prop('defaultDepartment') || '';
	my $city = $ldap->prop('defaultCity') || '';
	my $street = $ldap->prop('defaultStreet') || '';
	foreach my $group ($a->groups)
	{
	    my $key = $group->key;
	    my $desc = $group->prop('Description') || '';

	    print LDAP "\n";
	    print LDAP "dn: uid=$key,$ldapBase\n";
	    print LDAP "objectClass: group\n";
	    print LDAP "uid: " . $key . "\n";
	    print LDAP "cn: $desc\n" unless $desc =~ /^\s*$/;
	    print LDAP "mail: $key\@$domain\n";
	    print LDAP "telephoneNumber: $phone\n" unless $phone =~ /^\s*$/;
	    print LDAP "o: $company\n" unless $company =~ /^\s*$/;
	    print LDAP "ou: $dept\n" unless $dept =~ /^\s*$/;
	    print LDAP "l: $city\n" unless $city =~ /^\s*$/;
	    print LDAP "street: $street\n" unless $street =~ /^\s*$/;
	}
	close LDAP || die "slapadd exited $?";
	exit (0);
    }
}

# Become 'ldap' to run the slapadd program
$) = getgrnam('ldap');
$> = getpwnam('ldap');
exec($slapadd) || die "Could not exec $slapadd: $!";
# NOTREACHED
exit (1);
