#!/usr/bin/perl -w

#
# $Id: tel 7378 2013-01-29 10:41:46Z joergs $
#

use strict;
use encoding ':locale';
use Data::Dumper;
use Unix::Syslog qw(:macros);
use Log::Log4perl;
use Env qw($USER $HOME);
use Net::LDAP;
use SmartClient::Workstation qw(ldap_to_syslog);
use SmartClient::Config;
use SmartClient::LdapUtil;

=head1 NAME

C<tel>

=head1 SYNOPSIS

    tel [USERID]


=head1 DESCRIPTION

=head2 Allg. Beschreibung und Aufgaben

sc_create_signature.pl erzeugt aus den Daten im LDAP eine Email Signature in der Datei ".signature.ldap"

=head2 Funktionsweise

=head2 Voraussetzungen

=over 4

=item Konfigurationsdateien:

Die Konfigurationdatei /etc/smartclient/base 
und die Logging Konfigurationsdatei /etc/smartclient/log4perl.noninteractive
(Paket: sc_base) müssen existieren.


=item Verwendete LDAP Einträge:

Benutzer Eintrag:
inetOrgPerson
    [postalAddress]
    telephoneNumber
    [facsimileTelephoneNumber]
    [labeledUri]

Locations Eintrag:
scLocation
    description
    [ou]
    postalAddress
    [phoneNumber]
    facsimileTelephoneNumber
    labeledUri

=back

=cut


=head1 AUTHOR

Jörg Steffens,
mailto: smartclient@dass-it.de


=head1 SEE ALSO



=head1 Konstanten

=cut



=head1 Globale Variablen

=cut

# global variables

my $user;
my $location;

my $Exit_Code = 0;


#
# Programmcode
#
sub get( $;$ )
{
    my $key = shift;
    my $default = shift || "";

    my $user_value;   
    if( ref( $user ) ) {
        $user_value = $user->get_value( $key );
    }

    my $location_value;
    if( ref( $location ) ) {
        $location_value = $location->get_value( $key );
    }
    
    my $value = $user_value || $location_value || $default;
    # hack, required for string conversion. 
    # Otherwise sprintf string formating delivers wrong results.
    $value = $value . "";
    return $value;
}

sub location_get( $;$ )
{
    my $key = shift;
    my $default = shift || "";

    my $location_value;
    if( ref( $location ) ) {
        $location_value = $location->get_value( $key );
    }
 
    return ( $location_value || $default );
}

#
# main
#

Log::Log4perl::init("/etc/smartclient/log4perl.interactive");
my $logger = Log::Log4perl->get_logger();


# load the config file
my $config = SmartClient::Config->new();
if( ! defined $config ) {
    $logger->logdie( "config file not found");
}


# get settings for first parameter or current user
my $arg = $ARGV[0] || $USER;
my $sv = '*'.$arg."*";

# get configuration
my $Ldap_Host = $config->{'ALL_LDAPHOST'};
my $Ldap_Version = $config->{'ALL_LDAPVERSION'} || 3;
my $Ldap_Base = $config->{'ALL_LDAPBASE'};

# connect to LDAP server
my $Ldap_Conn = Net::LDAP->new($Ldap_Host, version => $Ldap_Version);
if ( ! $Ldap_Conn ) {
    $logger->logdie( "connect to LDAP server $Ldap_Host ($Ldap_Base) failed" );
}


# bind to LDAP-Server
my $line = __LINE__;
my $result_bind = $Ldap_Conn->bind();
ldap_to_syslog( $result_bind, "bind to LDAP server $Ldap_Host failed", $line);

# get user LDAP entry
$line = __LINE__;
my $result = $Ldap_Conn->search(   base => $Ldap_Base,
                                    scope => 'sub',
                                    filter => "(&(objectclass=inetOrgPerson)(|(uid=$sv)(sn=$sv)(givenName=$sv)(mail=$sv)(ou=$sv)(l=$sv)))" );
ldap_to_syslog( $result, "search at LDAP server $Ldap_Host failed", $line);

for my $entry ( $result->entries() ) {

    # needed to be global variable
    $user = $entry;

    # get location LDAP entry
    $location = ldap_get_location_entry( LDAP => $Ldap_Conn, STARTDN => $user->dn );
#     if( !$location ) {
#         $logger->warn( "can't find location for " . $user->dn ); 
#     }
    
    # location name
    my $location_name = "";
    if( $location ) {
        $location_name = location_get('description') || location_get('ou') || "Landesamt für Vermessung und Geobasisinformation Rheinland-Pfalz";
    }
    
    # name
    my $givenname = get('givenName');
    my $surname = get('sn');
    my $uid = get('uid');
    my $mail = get('mail');
    my $job = get('title');
    
    # address
    my $address = get('postalAddress');
    my $street = get('street');
    my $plz = get('postalCode');
    my $city = get('l');

    # telephone
    my $telephone = $user->get_value('telephoneNumber') || location_get('telephoneNumber') || "";
    
    # telefax
    my $telefax = get('facsimileTelephoneNumber');
    
    # url
    my $url = get('labeledUri', "http://www.lvermgeo.rlp.de" );
    
    my $name = "$givenname $surname";
    
    printf "%-40s %s\n", "$name ($uid)", $mail;
    printf "Telefon: %-30s  Telefax: %s\n", $telephone, $telefax; 
    printf "%-40s %s %s\n", $street, $plz, $city;


    if( $location_name ) {
        printf "$location_name\n";
    }
    printf "$url\n";
    printf "\n";

}


exit $Exit_Code;
