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