1*b1cdbd2cSJim Jagielski#**************************************************************
2*b1cdbd2cSJim Jagielski#
3*b1cdbd2cSJim Jagielski#  Licensed to the Apache Software Foundation (ASF) under one
4*b1cdbd2cSJim Jagielski#  or more contributor license agreements.  See the NOTICE file
5*b1cdbd2cSJim Jagielski#  distributed with this work for additional information
6*b1cdbd2cSJim Jagielski#  regarding copyright ownership.  The ASF licenses this file
7*b1cdbd2cSJim Jagielski#  to you under the Apache License, Version 2.0 (the
8*b1cdbd2cSJim Jagielski#  "License"); you may not use this file except in compliance
9*b1cdbd2cSJim Jagielski#  with the License.  You may obtain a copy of the License at
10*b1cdbd2cSJim Jagielski#
11*b1cdbd2cSJim Jagielski#    http://www.apache.org/licenses/LICENSE-2.0
12*b1cdbd2cSJim Jagielski#
13*b1cdbd2cSJim Jagielski#  Unless required by applicable law or agreed to in writing,
14*b1cdbd2cSJim Jagielski#  software distributed under the License is distributed on an
15*b1cdbd2cSJim Jagielski#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b1cdbd2cSJim Jagielski#  KIND, either express or implied.  See the License for the
17*b1cdbd2cSJim Jagielski#  specific language governing permissions and limitations
18*b1cdbd2cSJim Jagielski#  under the License.
19*b1cdbd2cSJim Jagielski#
20*b1cdbd2cSJim Jagielski#**************************************************************
21*b1cdbd2cSJim Jagielski
22*b1cdbd2cSJim Jagielski
23*b1cdbd2cSJim Jagielski
24*b1cdbd2cSJim Jagielskiuse warnings;
25*b1cdbd2cSJim Jagielskiuse strict;
26*b1cdbd2cSJim Jagielskiuse diagnostics;
27*b1cdbd2cSJim Jagielski
28*b1cdbd2cSJim Jagielskisub trim;
29*b1cdbd2cSJim Jagielskisub readRedirectionValues($);
30*b1cdbd2cSJim Jagielski
31*b1cdbd2cSJim Jagielskimy $usage =
32*b1cdbd2cSJim Jagielski   "Usage is: \n subst_template.pl configTemplate redirections policyConfig
33*b1cdbd2cSJim Jagielski
34*b1cdbd2cSJim Jagielski   configTemplate: The config file which is used for the policy assembly. It
35*b1cdbd2cSJim Jagielski   contains place holders for the binding redirection.
36*b1cdbd2cSJim Jagielski
37*b1cdbd2cSJim Jagielski   redirections: file containing the values for oldVersion and newVersion tags
38*b1cdbd2cSJim Jagielski   which are used in the BindingRedirect element of the config files.
39*b1cdbd2cSJim Jagielski
40*b1cdbd2cSJim Jagielski   policyConfig: Name of the file in which we want to write the config file.
41*b1cdbd2cSJim Jagielski";
42*b1cdbd2cSJim Jagielski
43*b1cdbd2cSJim Jagielski
44*b1cdbd2cSJim Jagielskiif (scalar @ARGV < 3) {
45*b1cdbd2cSJim Jagielski   print $usage;
46*b1cdbd2cSJim Jagielski   exit -1;
47*b1cdbd2cSJim Jagielski}
48*b1cdbd2cSJim Jagielski
49*b1cdbd2cSJim Jagielski
50*b1cdbd2cSJim Jagielskimy %redirectionValue = readRedirectionValues($ARGV[1]);
51*b1cdbd2cSJim Jagielski#print "|$_|  |$redirectionValue{$_}|\n",  for keys %redirectionValue;
52*b1cdbd2cSJim Jagielski
53*b1cdbd2cSJim Jagielski
54*b1cdbd2cSJim Jagielski#Read config file in which we will replace the versions
55*b1cdbd2cSJim Jagielski$/ = undef;
56*b1cdbd2cSJim Jagielskiopen TEMPLATE, $ARGV[0] or die $!;
57*b1cdbd2cSJim Jagielskimy $templ = <TEMPLATE>;
58*b1cdbd2cSJim Jagielski
59*b1cdbd2cSJim Jagielski#Open the config file we are goint to write to
60*b1cdbd2cSJim Jagielskiopen CONFIG, "> $ARGV[2]" or die "Cannot write to $ARGV[2] $!";
61*b1cdbd2cSJim Jagielski
62*b1cdbd2cSJim Jagielski#No substitute the place holders for oldVersion and new Version in the config template with
63*b1cdbd2cSJim Jagielski#the values obtained from the redirections file
64*b1cdbd2cSJim Jagielskifor (keys %redirectionValue) {
65*b1cdbd2cSJim Jagielski    $templ=~ s/\b$_\b/$redirectionValue{$_}/;
66*b1cdbd2cSJim Jagielski}
67*b1cdbd2cSJim Jagielski#Write the config file
68*b1cdbd2cSJim Jagielskiprint CONFIG $templ;
69*b1cdbd2cSJim Jagielski
70*b1cdbd2cSJim Jagielski#Reads the key value pairs from the files, which name must be passed in
71*b1cdbd2cSJim Jagielski#the parameter. The file contains lines of the form name=value, for example
72*b1cdbd2cSJim Jagielski#CLI_TYPES_OLD_VERSION=1.1.0.0-1.1.1.0
73*b1cdbd2cSJim Jagielskisub readRedirectionValues($)
74*b1cdbd2cSJim Jagielski{
75*b1cdbd2cSJim Jagielski    #Read in the values for the version redirection
76*b1cdbd2cSJim Jagielski    open REDIR, $_[0] or die $!;
77*b1cdbd2cSJim Jagielski
78*b1cdbd2cSJim Jagielski    my %redirectionValues;
79*b1cdbd2cSJim Jagielski
80*b1cdbd2cSJim Jagielski    while (<REDIR>)
81*b1cdbd2cSJim Jagielski    {
82*b1cdbd2cSJim Jagielski        chomp;
83*b1cdbd2cSJim Jagielski	my $trimmed;
84*b1cdbd2cSJim Jagielski        #Skip empty lines
85*b1cdbd2cSJim Jagielski        if (length($trimmed = trim($_)) == 0) {
86*b1cdbd2cSJim Jagielski            next;
87*b1cdbd2cSJim Jagielski        }
88*b1cdbd2cSJim Jagielski
89*b1cdbd2cSJim Jagielski	#Skip comment symbol: #
90*b1cdbd2cSJim Jagielski	if ($trimmed =~ /^#/) {
91*b1cdbd2cSJim Jagielski	    next;
92*b1cdbd2cSJim Jagielski	}
93*b1cdbd2cSJim Jagielski
94*b1cdbd2cSJim Jagielski        my @lineParts = split /=/,$_;
95*b1cdbd2cSJim Jagielski
96*b1cdbd2cSJim Jagielski        #Check if we have valid name value pairs.
97*b1cdbd2cSJim Jagielski        if (scalar @lineParts != 2) {
98*b1cdbd2cSJim Jagielski            print "Error: Values in $ARGV[1] are not correct (Entries must have the form name=value). Invalid line: \n$_\n";
99*b1cdbd2cSJim Jagielski            exit -1;
100*b1cdbd2cSJim Jagielski        }
101*b1cdbd2cSJim Jagielski
102*b1cdbd2cSJim Jagielski        #Trim the strings and check if they still contain characters
103*b1cdbd2cSJim Jagielski        my $name = trim($lineParts[0]);
104*b1cdbd2cSJim Jagielski        my $value = trim($lineParts[1]);
105*b1cdbd2cSJim Jagielski        if (length($name) == 0 || length($value) == 0) {
106*b1cdbd2cSJim Jagielski            print "Error: Values in $ARGV[1] are not correct. Invalid line: \n$_\n";
107*b1cdbd2cSJim Jagielski            exit -1;
108*b1cdbd2cSJim Jagielski        }
109*b1cdbd2cSJim Jagielski
110*b1cdbd2cSJim Jagielski        #Check if we have duplicate key names
111*b1cdbd2cSJim Jagielski        for (keys %redirectionValues) {
112*b1cdbd2cSJim Jagielski            if ( $name eq $_) {
113*b1cdbd2cSJim Jagielski                print "Error: Values in $ARGV[1] are not correct. The name $_ is not unique.\n";
114*b1cdbd2cSJim Jagielski                exit -1;
115*b1cdbd2cSJim Jagielski            }
116*b1cdbd2cSJim Jagielski        }
117*b1cdbd2cSJim Jagielski
118*b1cdbd2cSJim Jagielski        $redirectionValues{$name} = $value;
119*b1cdbd2cSJim Jagielski    }
120*b1cdbd2cSJim Jagielski    return %redirectionValues;
121*b1cdbd2cSJim Jagielski}
122*b1cdbd2cSJim Jagielski
123*b1cdbd2cSJim Jagielskisub trim($)
124*b1cdbd2cSJim Jagielski{
125*b1cdbd2cSJim Jagielski	my $string = shift;
126*b1cdbd2cSJim Jagielski	$string =~ s/^\s+//;
127*b1cdbd2cSJim Jagielski	$string =~ s/\s+$//;
128*b1cdbd2cSJim Jagielski	return $string;
129*b1cdbd2cSJim Jagielski}
130