84 lines
2.1 KiB
Perl
Executable File
84 lines
2.1 KiB
Perl
Executable File
#!/usr/bin/perl
|
|
|
|
use strict;
|
|
use Getopt::Long;
|
|
use Config::IniFiles;
|
|
|
|
my %opts;
|
|
my $result = GetOptions( \%opts, 'storage|s=s', 'apply|a=s', 'dir|d=s', 'ini|i=s',
|
|
'preserve|p' );
|
|
|
|
use Lemonldap::NG::Common::Conf::Constants;
|
|
|
|
$opts{dir} ||= '/etc/lemonldap-ng';
|
|
|
|
my $old = {
|
|
storage => $opts{storage} || "$opts{dir}/storage.conf",
|
|
apply => $opts{apply} || "$opts{dir}/apply.conf",
|
|
};
|
|
my $new = $opts{ini} || "$opts{dir}/lemonldap-ng.ini";
|
|
|
|
my $datas;
|
|
|
|
-r $old->{storage} or quit( 2, "$old->{storage} is not readeable" );
|
|
open F, $old->{storage};
|
|
while (<F>) {
|
|
next if (/^\s*(?:#.*)?$/);
|
|
my ( $k, $v ) = (/^(\w+)\s*=\s*(.*)$/)
|
|
or quit( 3, "bad line in $old->{storage}:$_" );
|
|
$datas->{configuration}->{$k} = $v;
|
|
}
|
|
close F;
|
|
if ( -e $old->{apply} ) {
|
|
open F, $old->{apply};
|
|
while (<F>) {
|
|
next if (/^\s*(?:#.*)?$/);
|
|
my ( $k, $v ) = (/^([\w\.\-]+)\s+(.*)$/)
|
|
or quit( 3, "bad line in $old->{apply}:$_" );
|
|
$datas->{apply}->{$k} = $v;
|
|
}
|
|
close F;
|
|
}
|
|
|
|
my $conf;
|
|
if ( -e $new ) {
|
|
-w $new or quit( 4, "$new is not writeable" );
|
|
$conf = Config::IniFiles->new( -file => $new )
|
|
or quit( 4,
|
|
"Unable to open $new:\n\t"
|
|
. join( "\n\t", @Config::IniFiles::errors ) );
|
|
}
|
|
else {
|
|
$conf = Config::IniFiles->new();
|
|
}
|
|
my @sections = $conf->Sections();
|
|
foreach (qw(configuration apply)) {
|
|
next unless ( ref $datas->{$_} );
|
|
$conf->AddSection($_) unless ( $conf->SectionExists($_) );
|
|
while ( my ( $k, $v ) = each %{ $datas->{$_} } ) {
|
|
if ( $conf->exists( $_, $k ) ) {
|
|
$conf->setval( $_, $k, $v );
|
|
}
|
|
else {
|
|
$conf->newval( $_, $k, $v );
|
|
}
|
|
}
|
|
}
|
|
if ( -e $new ) {
|
|
$conf->RewriteConfig();
|
|
}
|
|
else {
|
|
$conf->WriteConfig($new)
|
|
or quit( 5,
|
|
"Unable to create $new:\n\t"
|
|
. join( "\n\t", @Config::IniFiles::errors ) );
|
|
unless ( $opts{preserve} ) {
|
|
unlink $old->{storage}, $old->{apply};
|
|
}
|
|
}
|
|
|
|
sub quit {
|
|
print STDERR "$_[1]\n";
|
|
exit $_[0];
|
|
}
|