Fixed: converting long paths names works now.
author<hs@schlittermann.de>
Tue, 19 Nov 2013 09:31:53 +0000
changeset 2 1816ff04e91c
parent 1 9bdcb51cb057
child 4 01d73aa9d30d
Fixed: converting long paths names works now.
schema2ldif
--- a/schema2ldif	Mon Nov 11 22:08:45 2013 +0100
+++ b/schema2ldif	Tue Nov 19 09:31:53 2013 +0000
@@ -10,6 +10,7 @@
 use if $ENV{DEBUG} => 'Smart::Comments';
 use English qw(-no_match_vars);
 use File::Temp;
+use File::Basename;
 use File::Spec::Functions;
 use Getopt::Long;
 use Pod::Usage;
@@ -30,7 +31,7 @@
 
     open(my $f, '<', $ARGV[0]) or die "$PROGRAM_NAME: $ARGV[0]: $!\n";
     while (<$f>) {
-	if (/^#schema2ldif:include\s+(\S+)/) {
+	if (/^#\s*schema2ldif:include\s+(\S+)/) {
 	    push @opt_additional_schemas, $1;
 	}
     }
@@ -56,25 +57,35 @@
 ### @schemas
 
 (my $name = $ARGV[0]) =~ s/\.schema$//xms;
+my $schema_name = basename $name;
 my $cf = File::Temp->new();
 my $cd = File::Temp->newdir();
 
 # create a short temp config
 $cf->print("include $_\n") foreach @schemas, $ARGV[0];
 close $cf or die "Can't close $cf: $!\n";
-# ... convert it
+# run slaptest on the temp config, this should create
+# the cn=config style structure.
 system slaptest => ( -f => $cf ),
 		   ( -F => $cd );
 die "slaptest failed, exit\n" if $?;
 
+# find the generated file
+$_ = do { 
+	my $dir = "$cd/cn=config/cn=schema";
+	$_ = [glob("$dir/*$schema_name.ldif")];
+	die "$0: no ldif found in $dir\n" if not @$_;
+	die "$0: more than one ldif found in $dir: @$_\n" if @$_ > 1;
+	$_->[0];
+};
+
 # slurp the generated file
-# FIXME: what, if we had multiple preconditions
-open(my $ldif, '<', $_ = "$cd/cn=config/cn=schema/cn={1}$name.ldif")
-    or die "Can't open $_: $!\n";
+open(my $ldif, '<', $_) or die "Can't open $_: $!\n";
 $_ = do { local $RS = undef; <$ldif> };
 close($ldif) or die "Can't close ldif generated ldif file: $!\n";
 
-# some modifications
+# some modifications according to the above mentioned
+# zytrax url
 s/\n\s+//xgms;
 s/^(?!(?:olc|dn|objectClass|cn)).*?\n//xmsg;
 s/(?<=^dn:\scn=){\d+}(\w+)/$1,cn=schema,cn=config/xms;
@@ -113,13 +124,13 @@
 =item B<--additional-schema> I<schema>
 
 Additional schema to be included (preconditions for your self 
-defind schema). The F<.schema> extension may be omitted. First it's
+defined schema). The F<.schema> extension may be omitted. First it's
 searched in the local directory, then in the directories given in
 B<--schema-dir> option(s). (default: none)
 
 NOTE: A pseudo comment 
 
-    #include core.schema
+    #schema2ldif:include core.schema
 
 may be included in the schema to convert. It works the same way.