#!/usr/bin/perl

eval 'exec /usr/bin/perl  -S $0 ${1+"$@"}'
    if 0; # not running under some shell

use Getopt::Std;
use Lemonldap::NG::Common::Conf;
use File::Basename;
use strict;

# Get ARGS
my %opts;
getopts( 'b:c', \%opts );

&usage("configuration file required")
    unless ( $ARGV[0] );
&usage(qq#$ARGV[0] is not readable#)
    unless ( -r $ARGV[0] );
&usage("configuration branch required")
    unless ( $opts{b} );

# Internal varibales
my $branch = $opts{b};
my ( $branchname ) = ( $branch =~ /^ou=(.+?),/ );
my ($filename, $directories, $suffix) = fileparse ( $ARGV[0] );


# LDIF header
print "# LemonLDAP::NG configuration (converted from files)\n";

# Create configuration branch
if($opts{c}) {
    print "dn: $branch
objectClass: top
objectClass: organizationalUnit
ou: $branchname\n\n";
}

# Create configuration entry
open FILE, $ARGV[0];
$/ = "";

print "dn: cn=$filename,$branch
objectClass: top
objectClass: applicationProcess
cn: $filename\n";

while (<FILE>) {
    my ( $k, $v ) = split /\n\s+/;
    chomp $k;
    next unless($k);
    $v =~ s/\n*$//;
    print "description: {$k}$v\n";
}

print "\n";

sub usage {
    print STDERR shift;
    print STDERR "\nusage: $0 <options> file
Options:
        -b branch : name of the configuration branch
        -c : add 'create table' instruction\n\n";
    exit 1;
}
1;
__END__

=head1 NAME

lmConfig_File2LDIF - Perl utility to convert Lemonldap::NG configuration file
into LDIF file.

=head1 SYNOPSIS

  lmConf_File2LDIF <options> /path/to/lmConf-1

=head1 DESCRIPTION

Use this software to convert Lemonldap::NG configuration file into LDIF.

=head2 Options

=over

=item * -b : name of the configuration branch

=item * -c : create the configuration branch (organizationalUnit)

=back

=head1 SEE ALSO

Lemonldap::NG::Manager

=head1 AUTHOR

Clement Oudot, E<lt>clement@oodo.netE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2007 by Xavier Guimard
Copyright (C) 2009 by Clement Oudot

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.8 or,
at your option, any later version of Perl 5 you may have available.

=cut
