Convenience function to check for 1/0/true/false/on/off/yes/no
[koha.git] / misc / buildrelease
1 #!/usr/bin/perl
2
3 sub guess_kohahtmldir ($;$);
4 #----------------------------------------------------------
5 # DIRECTORIES where source code is located
6 #----------------------------------------------------------
7 my $kohadir=`pwd`;
8 chomp $kohadir;
9 my $kohahtmldir=guess_kohahtmldir($kohadir, "/koha/koha/koha-html/");
10 my $roothomedir=(getpwuid(0))[7];       # ~root is traditionally just /
11 $roothomedir='/root' unless defined $roothomedir;
12
13 my $has_kohaautobuild_conf = 0;
14
15 if (-e "$roothomedir/.kohaautobuild.conf") {
16     open C, "<$roothomedir/.kohaautobuild.conf";
17     while (<C>) {
18         chomp;
19         if (/kohadir=(.*)/) {
20             $kohadir=$1;
21         }
22         if (/kohahtmldir=(.*)/) {
23             $kohahtmldir=$1;
24         }
25     }
26     $has_kohaautobuild_conf = 1;
27 }
28
29 my $input;
30
31 print qq |
32 ***************************************
33 * Welcome to the Koha Release Builder *
34 ***************************************
35
36 This script will automatically build a release tarball.
37
38 The script assumes that you already have the koha and koha-html modules checked
39 out for the release that you want to build, although it will update the modules
40 before building.
41
42 |;
43 print "\nWhere is the 'koha' cvs module located [$kohadir]: ";
44 chomp($input = <STDIN>);
45 if ($input) {
46     $kohadir=$input;
47     unless ($has_kohaautobuild_conf) {
48         $kohahtmldir=guess_kohahtmldir($kohadir, $kohahtmldir);
49     }
50 }
51
52
53 print "\nWhere is the 'koha-html' cvs module located [$kohahtmldir]: ";
54 chomp($input = <STDIN>);
55 if ($input) {
56     $kohahtmldir=$input;
57 }
58
59 open (C, ">$roothomedir/.kohaautobuild.conf");
60 print C qq|
61 kohadir=$kohadir
62 kohahtmldir=$kohahtmldir
63 |;
64
65 #----------------------------------------------------------
66 # which VERSION are we building ?
67 #----------------------------------------------------------
68 print "\n\nGuessing at next release version.  You may need to enter your SourceForge password...\n";
69 chdir $kohadir;
70 open (CVSLOG, "cvs log misc/buildrelease|");
71 my $symbolicnamessection=0;
72 my $symbolicnames;
73 my $highestversion;
74 my $highestrc;
75 my $released;
76 my $majorversion;
77 my $majorversionrc;
78 while (<CVSLOG>) {
79     if (/^symbolic names:/) {
80         $symbolicnamessection=1;
81     }
82     if ($symbolicnamessection && (/^\s+([^:]*):/)) {
83         my $tag=$1;
84         if ($tag=~/R_(.*)/) {
85             my $version='';
86             my $rc=0;
87             my $id=$1;
88             $id=~s/-/\./g;
89             if ($id =~/(.*)RC(.*)/) {
90                 $version=$1;
91                 $rc=$2;
92                 $version=~m#(\d+\.\d+)\.#;
93                 if (versioncompare($version, $majorversion->{$1})) {
94                     $majorversion->{$1}=$version;
95                     $majorversionrc->{$1}=$rc;
96                 }
97                 if (versioncompare($version, $highestversion)) {
98                     $highestversion=$version;
99                     $released=0;
100                     $highestrc=$rc;
101                 }
102             } else {
103                 $version=$id;
104                 $version=~m#(\d+\.\d+)\.#;
105                 if (versioncompare($version, $majorversion->{$1})) {
106                     $majorversion->{$1}=$version;
107                     $majorversionrc->{$1}=0;
108                 }
109                 if (versioncompare($version, $highestversion)) {
110                     $highestversion=$version;
111                     $released=1;
112                     $highestrc=0;
113                 }
114             }
115             $symbolicnames->{$version}->{$rc}=1;
116         }
117     }
118 }
119
120
121 my $releaseversion='';
122 my $currentversion='';
123
124 my $branchdata=`grep buildrelease CVS/Entries`;
125 chomp $branchdata;
126 my $branch=(split(m#/#, $branchdata))[5];
127
128 if ($branch eq 'Trel-1-2') {
129     $highestversion=$majorversion->{'1.2'};
130     $highestrc=$majorversionrc->{'1.2'};
131     ($highestrc) ? ($released=0) : ($released=1);
132 }
133
134 if ($released) {
135     my @components=split(/\./, $highestversion);
136     $components[$#components]++;
137     $nexthighestversion=join '.', @components;
138     my $minornumber=(split(/\./, $highestversion))[1];
139     if ($minornumber/2 == int($minornumber/2)) {
140         $releaseversion=$nexthighestversion."RC1";
141     } else {
142         $releaseversion=$nexthighestversion;
143     }
144     $currentversion=$highestversion;
145 } else {
146     $releaseversion=$highestversion."RC".($highestrc+1);
147     $currentversion=$highestversion."RC$highestrc";
148 }
149
150
151 print "Current release tag is $currentversion.\n";
152 print "\nWould you like to bump that up to $releaseversion (or manually enter version)?  [Y]/N: ";
153 chomp($input = <STDIN>);
154 my $tagging_needs_confirmation = 0;
155 if ($input =~ /^n/i) {
156     print "\nWould you like to rebuild the $currentversion tarball?  Y/[N]: ";
157     chomp($input = <STDIN>);
158     print STDERR "releaseversion=($releaseversion), currentversion=($currentversion)\n";#XXXZZZ
159     if ($input =~ /^y/i) {
160         $releaseversion=$currentversion;
161         $tagging_needs_confirmation = 1;
162     } else {
163         print "What should the new version be? [$releaseversion]: ";
164         chomp ($input=<STDIN>);
165         if ($input) {
166             $releaseversion=$input;
167         }
168     }
169 } else {
170     print "What should the new version be? [$releaseversion]: ";
171     chomp ($input=<STDIN>);
172     if ($input) {
173         $releaseversion=$input;
174     }
175 }
176
177
178 print "\nWould you like to tag the CVS repository?\n(answer yes if releasing tarball)  Y/[N]: ";
179 chomp ($input=<STDIN>);
180 my $cvstag=0;
181 # FIXME: This means anything other than n will tag; too dangerous?
182 if ($input=~/^y/i) {
183     $cvstag=1;
184 }
185
186
187 if ($cvstag && $tagging_needs_confirmation) {
188         print "\n\n";
189         print "Do not do this if you have released the tarball to anybody, as it will\n";
190         print "overwrite the tag marking the files that were in the tarball:\n\n";
191         print "Confirm that you want to overwrite the tag for $releaseversion? Y/[N]: ";
192         chomp($input = <STDIN>);
193         if ($input =~ /^n/i || $input !~ /\S/) {
194             print "\nStopping.  Please re-run buildrelease now.\n";
195             exit;
196         }
197 }
198
199
200 my $sfuserid='';
201 if ($cvsroot=$ENV{'CVSROOT'}) {
202     $cvsroot=~m#(.*)\@cvs#;
203     $sfuserid=$1;
204 } else {
205     $ENV{'CVS_RSH'}='ssh';
206     print "\nWhat is your userid at SourceForge: ";
207     chomp($input = <STDIN>);
208     if ($input) {
209         $sfuserid=$input;
210     }
211     $ENV{'CVSROOT'}="$sfuserid\@cvs.koha.sourceforge.net:/cvsroot/koha";
212 }
213 my $tagname=$releaseversion;
214 $tagname=~s/\./-/g;
215
216 print qq|
217 Updating your checked-out copy of the 'koha' CVS files.
218 You may need to enter your SourceForge password.
219 Using $kohadir.
220 |;
221 chdir($kohadir) || die "$kohadir: $!\n";
222 system("cvs update");
223 if ($cvstag) {
224     print qq|
225 Tagging koha with tag R_$tagname
226 |;
227     system("cvs tag -F R_$tagname");
228 }
229 print qq|
230 Updating your checked-out copy of the 'koha-html' CVS files.
231 You may need to enter your SourceForge password.
232 Using $kohahtmldir.
233 |;
234 chdir($kohahtmldir) || die "$kohahtmldir: $!\n";
235 system("cvs update");
236
237 if ($cvstag) {
238     print qq|
239 Tagging koha-html with tag R_$tagname
240 |;
241     system("cvs tag -F R_$tagname");
242 }
243
244 #----------------------------------------------------------
245 # MOVE files to /tmp and build tar.gz
246 #----------------------------------------------------------
247
248
249
250
251 my $rootdir="/tmp/koha-".$releaseversion;
252 system("rm -rf $rootdir");
253 mkdir ($rootdir, 0700);
254 chdir($rootdir) || die "$rootdir: $!\n";
255
256 mkdir("intranet-cgi", 0755);
257 mkdir("intranet-html", 0755);
258 mkdir("opac-cgi", 0755);
259 mkdir("opac-html", 0755);
260 mkdir("scripts", 0755);
261 mkdir("scripts/z3950daemon", 0755);
262 mkdir("modules", 0755);
263 mkdir("docs", 0755);
264
265 # Create koha.version file
266 open (KV, ">$rootdir/koha.version");
267 print KV "$releaseversion\n";
268 close KV;
269
270 # Copy all CVS files to intranet-cgi
271 # FIXME: "cp -a" is GNU-ism. It is not portable.
272 system("cp -a $kohadir/* $rootdir/intranet-cgi");
273
274 # Move C4 to modules directory
275 system("mv $rootdir/intranet-cgi/C4 $rootdir/modules");
276
277 # Move files from intranet-cgi to root of tarball
278 system("mv $rootdir/intranet-cgi/misc/info/* $rootdir");
279 system("mv $rootdir/intranet-cgi/misc/installer.pl $rootdir");
280 system("mv $rootdir/intranet-cgi/misc/koha.upgrade $rootdir");
281 system("mv $rootdir/intranet-cgi/misc/Install.pm $rootdir");
282 #system("mv $rootdir/intranet-cgi/kohareporter $rootdir"); # does not exist ??
283 chmod 0770, "$rootdir/installer.pl";
284 chmod 0770, "$rootdir/koha.upgrade";
285 system("mv $rootdir/intranet-cgi/misc/koha.conf $rootdir");
286 system("mv $rootdir/intranet-cgi/misc/koha.mysql $rootdir");
287 system("mv $rootdir/intranet-cgi/misc/sampledata-1.2 $rootdir");
288 system("gzip -9 $rootdir/sampledata-1.2");
289
290 # Copy files from intranet-cgi to opac-cgi
291 system("cp $rootdir/intranet-cgi/detail.pl $rootdir/opac-cgi");
292 system("cp $rootdir/intranet-cgi/moredetail.pl $rootdir/opac-cgi");
293 system("cp $rootdir/intranet-cgi/search.pl $rootdir/opac-cgi");
294 system("cp $rootdir/intranet-cgi/subjectsearch.pl $rootdir/opac-cgi");
295 system("cp $rootdir/intranet-cgi/logout.pl $rootdir/opac-cgi");
296 system("mv $rootdir/intranet-cgi/opac/* $rootdir/opac-cgi");
297 system("rmdir $rootdir/intranet-cgi/opac");
298
299
300 # Move files from intranet-cgi to /scripts/ directory
301 system("mv $rootdir/intranet-cgi/telnet $rootdir/scripts");
302 system("mv $rootdir/intranet-cgi/tkperl $rootdir/scripts");
303 system("mv $rootdir/intranet-cgi/translator $rootdir/scripts");
304 system("mv $rootdir/intranet-cgi/updater $rootdir/scripts");
305 system("mv $rootdir/intranet-cgi/misc $rootdir/scripts");
306 system("mv $rootdir/intranet-cgi/marc $rootdir/scripts");
307 system("mv $rootdir/intranet-cgi/misc/bulkmarcimport.pl $rootdir/scripts/z3950daemon/");
308 system("mv $rootdir/intranet-cgi/z3950/* $rootdir/scripts/z3950daemon/");
309
310
311 # Remove extraneous files from intranet-cgi
312 system("rm -f $rootdir/intranet-cgi/ChangeLog.bak");
313 system("rm -f $rootdir/intranet-cgi/SendMessages");
314 system("rm -f $rootdir/intranet-cgi/buildrelease");
315 #system("rm -f $rootdir/intranet-cgi/database.mysql");
316 #system("rm -f $rootdir/intranet-cgi/installer-lite.pl");
317 # FIXME: The following two lines look suspicious
318 #system("rm -f $rootdir/intranet-cgi/koha-1.2.0.tar.gz");
319 #system("rm -f $rootdir/intranet-cgi/rel-1-2");
320 #system("rm -f $rootdir/intranet-cgi/testKoha.pl");
321 #system("rm -rf $rootdir/intranet-cgi/html-template");
322 system("rm -rf $rootdir/intranet-cgi/t");
323
324 # Set all .pl scripts executable
325 system("find $rootdir/intranet-cgi -name '*.pl' -exec chmod a+x \\{\\} \\;");
326 # Copy all CVS files to intranet-html and opac-html
327 # FIXME: "cp -a" is GNU-ism. It is not portable.
328 system("cp -a $kohahtmldir/intranet-html/* $rootdir/intranet-html");
329 system("cp -a $kohahtmldir/opac-html/* $rootdir/opac-html");
330
331 # Copy koha-tmpl files
332 # FIXME: "cp -a" is GNU-ism. It is not portable.
333 system('cp', '-a', "$rootdir/intranet-cgi/koha-tmpl/opac-tmpl", "$rootdir/opac-html");
334 system('cp', '-a', "$rootdir/intranet-cgi/koha-tmpl/intranet-tmpl", "$rootdir/intranet-html");
335 system('rm', '-rf', "$rootdir/intranet-cgi/koha-tmpl");
336
337 # Remove extraneous files from opac-html
338 system("rm -f $rootdir/opac-html/koha.mo");
339 system("rm -f $rootdir/opac-html/koha.pot");
340 system("rm -f $rootdir/opac-html/test");
341
342 # Remove extraneous files from intranet-html
343 system("rm -f $rootdir/intranet-html/koha.pot");
344 system("rm -f $rootdir/intranet-html/results.html");
345 system("rm -f $rootdir/intranet-html/test");
346
347 # Remove junk from directory
348 system("find $rootdir -name CVS -exec rm -rf \\{\\} \\; 2>/dev/null");
349 system("find $rootdir -name *~ -exec rm -rf \\{\\} \\; 2>/dev/null");
350 system("find $rootdir -name .#* -exec rm -rf \\{\\} \\; 2>/dev/null");
351
352
353 # FIXME: Why do we copy from $roothomedir? What is supposed to be there?
354 if (-e "$roothomedir/docs") {
355     print "Copying docs folder from $roothomedir/docs...";
356     system("cp -r $roothomedir/docs/* $rootdir/docs/");
357 } else {
358     print "I would have copied the docs from from $roothomedir/docs, but I couldn't find it.\n";
359     print "Press <ENTER> to continue...\n";
360     <STDIN>;
361 }
362
363 chdir("/tmp");
364 system("tar czf /tmp/koha-$releaseversion.tar.gz koha-".$releaseversion);
365 system("rm -rf $rootdir");
366
367 print qq|
368 ============
369 = ALL DONE =
370 ============
371
372 Your new tarball is located in /tmp/koha-$releaseversion.tar.gz
373
374 |;
375
376
377 sub versioncompare {
378     my $v1=shift;
379     my $v2=shift;
380     my @v1=split(/\./, $v1);
381     my @v2=split(/\./, $v2);
382     my $count=$#v1;
383     ($#v2>$count) && ($count=$#v2);
384     for (my $index=0; $index<$count; $index++) {
385         if ($v1[$index]>$v2[$index]) {
386             return 1;
387         }
388     }
389     return 0;
390 }
391
392 sub guess_kohahtmldir ($;$) {
393     my($kohadir, $default) = @_;
394     my $kohahtmldir;
395     # It probably makes sense to assume that the 'koha' and 'koha-html'
396     # modules are checked out within the same parent directory
397     if (-d $kohadir && $kohadir =~ /^(.*)\/[^\/]+$/) {
398         $kohahtmldir = "$1/koha-html"
399     } else {
400         $kohahtmldir = $default;
401     }
402     return $kohahtmldir;
403 }
404