scripts/din2d
changeset 23 0d1bd8c1cf85
parent 20 3f164cb8d543
--- a/scripts/din2d	Mon Aug 24 21:41:18 2015 +0200
+++ b/scripts/din2d	Tue Aug 25 15:30:13 2015 +0200
@@ -1,22 +1,49 @@
 #!/usr/bin/perl
 # Dockerfile.in -> Dockerfile
 # currently just resolve .include_if_exists ..
+#                        .include
+#                        .platform
+#                        <platform>
 
 use strict;
 use warnings;
+use 5.14.0;
 use Cwd qw(abs_path);
-use File::Basename qw(dirname);
+use File::Basename qw(basename dirname);
 use Getopt::Long;
 
 while (<>) {
-    /^\.include_if_exists\s+(.+?)\s*$/ or next;
-    $_ = "# from $1 {{\n" . do { 
-	my $file = abs_path(dirname($ARGV) . "/$1");
-	open(my $in, '<', $file) or die "Can't open $file: $!\n";
-	local $/ = undef;
-	<$in>;
-    } 
-    . "# }}\n";
+    state $platform;
+
+    if (/^.platform\s+(\S+)\s*/) {
+	$platform = $1;
+	$_ = "# platform is $1\n";
+	next;
+    }
+
+    if (/^(?<cmd>.include(?<try>_if_exists)?)\s+(?<file>.+?)\s*$/) {
+	$_ = "# $+{cmd} from $+{file} {{\n" . join('' => do { 
+	    my @yield;
+	    my @files = glob abs_path(dirname($ARGV)) . "/$+{file}";
+	    foreach my $file (@files) {
+		push @yield, "# from $file\n";
+		if (open(my $in, '<', $file)) {
+		    local $/ = undef;
+		    push @yield, <$in>;
+		}
+		else {
+		    die "$file: $!\n" if not $+{try};
+		}
+	    }
+	    @yield;
+	}) . "# }}\n";
+    }
+
+    if (/<platform>/) {
+	die "$0:$ARGV platform not known yet!\n"
+	    if not defined $platform;
+	s/<platform>/$platform/g;
+    }
 }
 continue {
     print if defined;