#!/usr/bin/perl
#
# generate sched-report "person" section
# from data in LDAP server
#
# Copyright 2001-2002 Jim Trocki
# Copyright 2001-2002 Transmeta 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
#
# $Id: sched-populate 1.2 Thu, 18 Jul 2002 10:33:50 -0400 trockij $

use strict;

use Getopt::Std;
use Net::LDAP;

sub usage;

my %opt;

getopts ('hs:', \%opt);

if ($opt{"h"})
{
    usage;
}

my $SERVER = "ldap";

if ($opt{"s"})
{
    $SERVER = $opt{"s"};
}

my $BASE = "o=Whatever Corporation,c=US";

if ($opt{"b"})
{
    $BASE = $opt{"b"};
}

#
# this may need to be customized for your site
#
my %attrs = (
    "Uid"	=> "username",
    "Cn"	=> "fullname",
    "Pager"	=> "pager",
    "Pagermail"	=> "pageremail",
    "Mail"	=> "email",
    "Telephonenumber"	=> "phone",
    "Mobile"		=> "cell",
    "Buildingname"	=> "building",
    "Roomnumber"	=> "room",
);

my %users = ();

my $ou = shift;

if ($ou eq "")
{
    die "must supply organizational unit number (U9200, etc.)\n";
}

my $ldap;

if (!defined ($ldap =  Net::LDAP->new($SERVER)))
{
    die "could not create object\n";
}

$ldap->bind () || die "could not bind\n";

my $search = $ldap->search (
	base   => $BASE,
	filter => "ou=$ou"
);

$search->code();

foreach my $entry ($search->all_entries)
{
    foreach my $attr (keys %attrs)
    {
	my $user = $entry->get ("uid");
	$users{$user->[0]}->{$attrs{$attr}} = $entry->get ($attr);
    }
}

my $d = localtime (time);

print <<EOF;
#
# generated by $0 on $d
#
EOF

foreach my $user (sort keys %users)
{
    print "begin person $user\n";
    print "   fullname $users{$user}->{fullname}->[0]\n";

    foreach my $attr (sort keys %{$users{$user}})
    {
    	next if ($attr eq "fullname");

	if (!defined $users{$user}->{$attr})
	{
	    next;
	}

	for (my $i = 0; $i < @{$users{$user}->{$attr}}; $i++)
	{
	    print "   $attr $users{$user}->{$attr}->[$i]\n";
	}
    }

    print "end person\n";
    print "\n";
}

exit 0;


sub usage
{
    print <<EOF;
usage: ldap-person-populate [-h] [-s host] [-b base] orgunit
Create the "person" section of a Schedule::Oncall configuration file
from people in an LDAP database.

    "orgunit" is "U9200", etc.

    -h		help
    -s host	connect to ldap server "host", or "ldap" by default
    -b base	the query base, e.g. 'o=Small Corporation,c=US'

EOF

    exit;
}

