better implementation of change#3326; open(local $foo,...) now
[p5sagit/p5-mst-13.2.git] / win32 / config_sh.PL
1 # take a semicolon separated path list and turn it into a quoted
2 # list of paths that Text::Parsewords will grok
3 sub mungepath {
4     my $p = shift;
5     # remove leading/trailing semis/spaces
6     $p =~ s/^[ ;]+//;
7     $p =~ s/[ ;]+$//;
8     $p =~ s/'/"/g;
9     my @p = map { $_ = "\"$_\"" if /\s/ and !/^".*"$/; $_ } split /;/, $p;
10     return join(' ', @p);
11 }
12
13 my %opt;
14 while (@ARGV && $ARGV[0] =~ /^([\w_]+)=(.*)$/)
15  {
16   $opt{$1}=$2;
17   shift(@ARGV);
18  }
19
20 my $pl_h = '../patchlevel.h';
21
22 $opt{VERSION} = $];
23 $opt{INST_VER} =~ s|~VERSION~|$]|g;
24 if (-e $pl_h) {
25     open PL, "<$pl_h" or die "Can't open $pl_h: $!";
26     while (<PL>) {
27         if (/^#\s*define\s+(PERL_\w+)\s+([\d.]+)/) {
28             $opt{$1} = $2;
29         }
30     }
31     close PL;
32 }
33 elsif ($] =~ /^(\d+)\.(\d\d\d)?(\d\d)?$/) { # should always be true
34   $opt{PERL_REVISION} = $1;
35   $opt{PERL_VERSION} = int($2 || 0);
36   $opt{PERL_SUBVERSION} = $3;
37   $opt{PERL_APIVERSION} = $];
38 }
39 else {
40   die "Can't parse perl version ($])";
41 }
42
43 $opt{PERL_SUBVERSION} ||= '00';
44
45 $opt{'cf_by'} = $ENV{USERNAME} unless $opt{'cf_by'};
46 $opt{'cf_email'} = $opt{'cf_by'} . '@' . (gethostbyname('localhost'))[0]
47         unless $opt{'cf_email'};
48 $opt{'usemymalloc'} = 'y' if $opt{'d_mymalloc'} eq 'define';
49
50 $opt{libpth} = mungepath($opt{libpth}) if exists $opt{libpth};
51 $opt{incpath} = mungepath($opt{incpath}) if exists $opt{incpath};
52
53 while (<>)
54  {
55   s/~([\w_]+)~/$opt{$1}/g;
56   if (/^([\w_]+)=(.*)$/) {
57     my($k,$v) = ($1,$2);
58     # this depends on cf_time being empty in the template (or we'll get a loop)
59     if ($k eq 'cf_time') {
60       $_ = "$k='" . localtime(time) . "'\n" if $v =~ /^\s*'\s*'/;
61     }
62     elsif (exists $opt{$k}) {
63       $_ = "$k='$opt{$k}'\n";
64     }
65   }
66   print;
67  }
68