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