1*7e90fac2SAndrew Rist#**************************************************************
2*7e90fac2SAndrew Rist#
3*7e90fac2SAndrew Rist#  Licensed to the Apache Software Foundation (ASF) under one
4*7e90fac2SAndrew Rist#  or more contributor license agreements.  See the NOTICE file
5*7e90fac2SAndrew Rist#  distributed with this work for additional information
6*7e90fac2SAndrew Rist#  regarding copyright ownership.  The ASF licenses this file
7*7e90fac2SAndrew Rist#  to you under the Apache License, Version 2.0 (the
8*7e90fac2SAndrew Rist#  "License"); you may not use this file except in compliance
9*7e90fac2SAndrew Rist#  with the License.  You may obtain a copy of the License at
10*7e90fac2SAndrew Rist#
11*7e90fac2SAndrew Rist#    http://www.apache.org/licenses/LICENSE-2.0
12*7e90fac2SAndrew Rist#
13*7e90fac2SAndrew Rist#  Unless required by applicable law or agreed to in writing,
14*7e90fac2SAndrew Rist#  software distributed under the License is distributed on an
15*7e90fac2SAndrew Rist#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*7e90fac2SAndrew Rist#  KIND, either express or implied.  See the License for the
17*7e90fac2SAndrew Rist#  specific language governing permissions and limitations
18*7e90fac2SAndrew Rist#  under the License.
19*7e90fac2SAndrew Rist#
20*7e90fac2SAndrew Rist#**************************************************************
21*7e90fac2SAndrew Rist
22*7e90fac2SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweiruse warnings;
25cdf0e10cSrcweiruse strict;
26cdf0e10cSrcweiruse diagnostics;
27cdf0e10cSrcweir
28cdf0e10cSrcweirsub trim;
29cdf0e10cSrcweirsub readIncVersions($);
30cdf0e10cSrcweirsub processLine($$);
31cdf0e10cSrcweirsub checkName($);
32cdf0e10cSrcweirsub incrementNewVersion($);
33cdf0e10cSrcweirsub incrementOldVersion($);
34cdf0e10cSrcweirsub incrementPolicyVersion($);
35cdf0e10cSrcweirmy $usage =
36cdf0e10cSrcweir"The tool increments the minor version of assemblies and the major version of ".
37cdf0e10cSrcweir"the respective policy files. This is only done if new uno types have been added since".
38cdf0e10cSrcweir"the last product upate. This information is obtained from the file which is passed as ".
39cdf0e10cSrcweir"argument changedTypes. The names in the version file must have a particular form. ".
40cdf0e10cSrcweir"They must end on one of folling terms: NEW_VERSION, OLD_VERSION, POLICY_VERSION\n".
41cdf0e10cSrcweir"If no new published types habe been added then no output, argument  newVersions, is written".
42cdf0e10cSrcweir"Usage is: \n increment_version.pl oldVersions  incVersions newVersions changedTypes\n\n".
43cdf0e10cSrcweir"oldVersion: Contains name value pairs, which are used for forming the config files of ".
44cdf0e10cSrcweir"the policy assemblies, for building the assemblies. \n\n".
45cdf0e10cSrcweir"incVersions: File containing the names of which the versions are to be incremented. ".
46cdf0e10cSrcweir"Every line may only contain one name. The names must exactly match those from the ".
47cdf0e10cSrcweir"oldVersion file.\n\n".
48cdf0e10cSrcweir"newVersions: Contains all entries from oldVersions, but the values of the names,".
49cdf0e10cSrcweir"which occur in selection, have been incremented.\n\n".
50cdf0e10cSrcweir"changedTypes: File that contains the information if new published types have been added ".
51cdf0e10cSrcweir"since the last product update.\n\n" ;
52cdf0e10cSrcweir
53cdf0e10cSrcweirmy $sNameForm =
54cdf0e10cSrcweir"The names must end on one of these names: NEW_VERSION, OLD_VERSION, POLICY_VERSION\n".
55cdf0e10cSrcweir"For example, valid names are: \n".
56cdf0e10cSrcweir"CLI_URETYPES_NEW_VERSION\nCLI_URETYPES_OLD_VERSION\nCLI_URETYPES_POLICY_VERSION\n";
57cdf0e10cSrcweir
58cdf0e10cSrcweirif (scalar @ARGV < 4) {
59cdf0e10cSrcweir   print $usage;
60cdf0e10cSrcweir   exit -1;
61cdf0e10cSrcweir}
62cdf0e10cSrcweir
63cdf0e10cSrcweir-e "$ARGV[0]" or die "Error: wrong arguments. \n".$usage;
64cdf0e10cSrcweir-e "$ARGV[1]" or die "Error: wrong arguments. \n".$usage;
65cdf0e10cSrcweir-e "$ARGV[3]" or die "Error: wrong arguments. \n".$usage;
66cdf0e10cSrcweir
67cdf0e10cSrcweir#check if new types have been added since last release.
68cdf0e10cSrcweir#If not, then there is nothing to be done.
69cdf0e10cSrcweir#read in oldVersions line by line and apply the increment operation
70cdf0e10cSrcweiropen TYPES, "$ARGV[3]" or die "Cannot open to $ARGV[3] $!";
71cdf0e10cSrcweir
72cdf0e10cSrcweirmy $newTypes;
73cdf0e10cSrcweir
74cdf0e10cSrcweir#We look for the line that contains the number of new types
75cdf0e10cSrcweirwhile(<TYPES>)
76cdf0e10cSrcweir{
77cdf0e10cSrcweir    if (/New and published types/i)
78cdf0e10cSrcweir    {
79cdf0e10cSrcweir	$_ =~ /=\s*(\d+)/;
80cdf0e10cSrcweir	if ( ! defined $1)
81cdf0e10cSrcweir	{
82cdf0e10cSrcweir	    print "\n###$ARGV[3] contains an invalid entry for 'New and published types'.  \n\n";
83cdf0e10cSrcweir	    exit -1;
84cdf0e10cSrcweir	}
85cdf0e10cSrcweir	$newTypes = $1;
86cdf0e10cSrcweir    }
87cdf0e10cSrcweir}
88cdf0e10cSrcweir
89cdf0e10cSrcweir#Check if changeTypes contained the line we are looking for
90cdf0e10cSrcweirif (! defined $newTypes)
91cdf0e10cSrcweir{
92cdf0e10cSrcweir    print "\n###$ARGV[3] does not contain entry about the new types ".
93cdf0e10cSrcweir	"or we are looking for the wrong string! \n\n";
94cdf0e10cSrcweir    exit -1;
95cdf0e10cSrcweir}
96cdf0e10cSrcweir
97cdf0e10cSrcweirif ( $newTypes == 0)
98cdf0e10cSrcweir{
99cdf0e10cSrcweir    print "\nNo new UNO types since las product update.\n";
100cdf0e10cSrcweir    exit 0;
101cdf0e10cSrcweir}
102cdf0e10cSrcweirelse
103cdf0e10cSrcweir{
104cdf0e10cSrcweir    print "\nNew UNO types were addes since last release. The version will be increased.\n\n";
105cdf0e10cSrcweir}
106cdf0e10cSrcweir
107cdf0e10cSrcweir#read in incVersions in a list
108cdf0e10cSrcweirmy @incVersions = readIncVersions($ARGV[1]);
109cdf0e10cSrcweir#print "@incVersions";
110cdf0e10cSrcweir
111cdf0e10cSrcweir#read in oldVersions line by line and apply the increment operation
112cdf0e10cSrcweiropen OLDVERSION, "$ARGV[0]" or die "Cannot open to $ARGV[0] $!";
113cdf0e10cSrcweir
114cdf0e10cSrcweir#open file we want to write to
115cdf0e10cSrcweiropen NEWVERSION, "> $ARGV[2]" or die "Cannot write to $ARGV[2] $!";
116cdf0e10cSrcweir
117cdf0e10cSrcweirprint NEWVERSION processLine($_, @incVersions) while(<OLDVERSION>);
118cdf0e10cSrcweir
119cdf0e10cSrcweirclose NEWVERSION;
120cdf0e10cSrcweirclose OLDVERSION;
121cdf0e10cSrcweir
122cdf0e10cSrcweirexit 0;
123cdf0e10cSrcweir
124cdf0e10cSrcweirsub processLine($$)
125cdf0e10cSrcweir{
126cdf0e10cSrcweir    my $line = $_[0];
127cdf0e10cSrcweir    #skip empty lines
128cdf0e10cSrcweir    my $trimmed;
129cdf0e10cSrcweir    return $line if (length($trimmed = trim($line)) == 0);
130cdf0e10cSrcweir    #Skip comment symbol: #
131cdf0e10cSrcweir    return $line if ($trimmed =~ /^#/);
132cdf0e10cSrcweir
133cdf0e10cSrcweir    #Get the left part of '='
134cdf0e10cSrcweir    my $i = index($line, "=");
135cdf0e10cSrcweir    if( $i == -1)
136cdf0e10cSrcweir    {
137cdf0e10cSrcweir	print "Error: No '=' found in line:,: \n $line \n";
138cdf0e10cSrcweir	exit -1;
139cdf0e10cSrcweir    }
140cdf0e10cSrcweir    my $name = substr($line, 0, $i);
141cdf0e10cSrcweir    $name = trim($name);
142cdf0e10cSrcweir    #We do not check the names here because the file can contain
143cdf0e10cSrcweir    #other names, e.g. CLI_URETYPES_POLICY_ASSEMBLY
144cdf0e10cSrcweir    if (length($name) == 0) {
145cdf0e10cSrcweir	print "Wrong line in $ARGV[0]\n", $sNameForm;
146cdf0e10cSrcweir	exit -1;
147cdf0e10cSrcweir    }
148cdf0e10cSrcweir    my $value = substr($line, $i + 1);
149cdf0e10cSrcweir    $value = trim($value);
150cdf0e10cSrcweir
151cdf0e10cSrcweir    #Check if the entry shall be incremented, this information is in the second
152cdf0e10cSrcweir    #argument
153cdf0e10cSrcweir    my $found;
154cdf0e10cSrcweir    for(@incVersions) {
155cdf0e10cSrcweir	if ($_ eq $name) {
156cdf0e10cSrcweir	    $found = 1;
157cdf0e10cSrcweir	    last;
158cdf0e10cSrcweir	}
159cdf0e10cSrcweir    }
160cdf0e10cSrcweir    if ( ! defined($found)) {
161cdf0e10cSrcweir	 return $line;
162cdf0e10cSrcweir    }
163cdf0e10cSrcweir
164cdf0e10cSrcweir    #Check if the name represents a version we need to change
165cdf0e10cSrcweir    if ($name =~ /NEW_VERSION$/)
166cdf0e10cSrcweir    {
167cdf0e10cSrcweir	$value = incrementNewVersion($value);
168cdf0e10cSrcweir    }
169cdf0e10cSrcweir    elsif ($name =~ /OLD_VERSION$/)
170cdf0e10cSrcweir    {
171cdf0e10cSrcweir	$value = incrementOldVersion($value);
172cdf0e10cSrcweir    }
173cdf0e10cSrcweir    elsif ($name =~ /POLICY_VERSION$/)
174cdf0e10cSrcweir    {
175cdf0e10cSrcweir	$value = incrementPolicyVersion($value);
176cdf0e10cSrcweir    }
177cdf0e10cSrcweir    else
178cdf0e10cSrcweir    {
179cdf0e10cSrcweir	#other name which we ignore
180cdf0e10cSrcweir	return $line;
181cdf0e10cSrcweir    }
182cdf0e10cSrcweir    return "${name}=${value}\n";
183cdf0e10cSrcweir}
184cdf0e10cSrcweir
185cdf0e10cSrcweir#The value of a new version has the form x.x.x.x
186cdf0e10cSrcweir#We increment the third position from the left.
187cdf0e10cSrcweir#Te argument must already be trimmed.
188cdf0e10cSrcweirsub incrementNewVersion($)
189cdf0e10cSrcweir{
190cdf0e10cSrcweir   my @parts = split /\./,$_[0];
191cdf0e10cSrcweir   if (scalar @parts != 4)
192cdf0e10cSrcweir   {
193cdf0e10cSrcweir       print "Error, no valid version given in $ARGV[0]\n. A 'new version' has four parts.";
194cdf0e10cSrcweir       exit -1;
195cdf0e10cSrcweir   }
196cdf0e10cSrcweir   $parts[2]++;
197cdf0e10cSrcweir   #build the version string and return
198cdf0e10cSrcweir   return "$parts[0].$parts[1].$parts[2].$parts[3]";
199cdf0e10cSrcweir}
200cdf0e10cSrcweir
201cdf0e10cSrcweir#The value of a new version has the form x.x.x.x-x.x.x.x
202cdf0e10cSrcweir#We increment the third position of the second part.
203cdf0e10cSrcweir#Te argument must already be trimmed.
204cdf0e10cSrcweirsub incrementOldVersion($)
205cdf0e10cSrcweir{
206cdf0e10cSrcweir    my @parts = split /[\.-]/,$_[0];
207cdf0e10cSrcweir    if (scalar @parts != 8)
208cdf0e10cSrcweir    {
209cdf0e10cSrcweir	print "Error, no valid version given in $ARGV[0]\n. A 'old version' has the form
210cdf0e10cSrcweirx.x.x.x-x.x.x.x\n.";
211cdf0e10cSrcweir	exit -1;
212cdf0e10cSrcweir    }
213cdf0e10cSrcweir    $parts[6]++;
214cdf0e10cSrcweir    return "$parts[0].$parts[1].$parts[2].$parts[3]-$parts[4].$parts[5].$parts[6].$parts[7]";
215cdf0e10cSrcweir    return $_[0];
216cdf0e10cSrcweir}
217cdf0e10cSrcweir
218cdf0e10cSrcweirsub incrementPolicyVersion($)
219cdf0e10cSrcweir{
220cdf0e10cSrcweir   my @parts = split /\./,$_[0];
221cdf0e10cSrcweir   if (scalar @parts != 4)
222cdf0e10cSrcweir   {
223cdf0e10cSrcweir       print "Error, no valid version given in $ARGV[0]\n. A 'policy version' has four parts.";
224cdf0e10cSrcweir       exit -1;
225cdf0e10cSrcweir   }
226cdf0e10cSrcweir   $parts[0]++;
227cdf0e10cSrcweir   #build the version string and return
228cdf0e10cSrcweir   return "$parts[0].$parts[1].$parts[2].$parts[3]";
229cdf0e10cSrcweir}
230cdf0e10cSrcweir
231cdf0e10cSrcweir
232cdf0e10cSrcweirsub readIncVersions($)
233cdf0e10cSrcweir{
234cdf0e10cSrcweir    open INC, $_[0] or die "Could not open $_[0] $!";
235cdf0e10cSrcweir    my $arg = $_[0];
236cdf0e10cSrcweir    my @names;
237cdf0e10cSrcweir
238cdf0e10cSrcweir    while(<INC>)
239cdf0e10cSrcweir    {
240cdf0e10cSrcweir	chomp;
241cdf0e10cSrcweir	#Skip empty lines
242cdf0e10cSrcweir	my $line;
243cdf0e10cSrcweir	if (length($line = trim($_)) == 0) {
244cdf0e10cSrcweir	    next;
245cdf0e10cSrcweir	}
246cdf0e10cSrcweir	#Skip comment symbol: #
247cdf0e10cSrcweir	if ($line =~ /^#/) {
248cdf0e10cSrcweir	    next;
249cdf0e10cSrcweir	}
250cdf0e10cSrcweir	if (!checkName($line)) {
251cdf0e10cSrcweir	    print "Wrong entry in file $_[0]\n", $sNameForm;
252cdf0e10cSrcweir	    exit -1;
253cdf0e10cSrcweir	}
254cdf0e10cSrcweir	push @names, $line;
255cdf0e10cSrcweir    }
256cdf0e10cSrcweir    print "No entries found in $arg\n" if(scalar @names == 0);
257cdf0e10cSrcweir    return @names;
258cdf0e10cSrcweir}
259cdf0e10cSrcweir
260cdf0e10cSrcweir#The argument must already be trimmed
261cdf0e10cSrcweir#returns 1 if ok
262cdf0e10cSrcweirsub checkName($)
263cdf0e10cSrcweir{
264cdf0e10cSrcweir    my $name = $_[0];
265cdf0e10cSrcweir    if ( $name !~/NEW_VERSION$|OLD_VERSION$|POLICY_VERSION$/) {
266cdf0e10cSrcweir	return 0;
267cdf0e10cSrcweir    }
268cdf0e10cSrcweir    return 1;
269cdf0e10cSrcweir}
270cdf0e10cSrcweir
271cdf0e10cSrcweirsub trim($)
272cdf0e10cSrcweir{
273cdf0e10cSrcweir	my $string = shift;
274cdf0e10cSrcweir	$string =~ s/^\s+//;
275cdf0e10cSrcweir	$string =~ s/\s+$//;
276cdf0e10cSrcweir	return $string;
277cdf0e10cSrcweir}
278