Don't fallback to thinking we have SVN; say Git if we didn't find a VCS.
[metze/wireshark/wip.git] / make-version.pl
1 #!/usr/bin/perl -w
2 #
3 # Copyright 2004 Jörg Mayer (see AUTHORS file)
4 #
5 # Wireshark - Network traffic analyzer
6 # By Gerald Combs <gerald@wireshark.org>
7 # Copyright 1998 Gerald Combs
8 #
9 # This program is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU General Public License
11 # as published by the Free Software Foundation; either version 2
12 # of the License, or (at your option) any later version.
13 #
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with this program; if not, write to the Free Software
21 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
22
23 # See below for usage
24 #
25 # If "version.conf" is present, it is parsed for configuration values.
26 # Possible values are:
27 #
28 #   enable       - Enable or disable versioning.  Zero (0) disables, nonzero
29 #                  enables.
30 #   svn_client   - Use svn client i.s.o. ugly internal SVN file hack
31 #   tortoise_svn - Use TortoiseSVN client instead of ugly internal SVN
32 #                  file hack
33 #   format       - A strftime() formatted string to use as a template for
34 #                  the version string. The sequence "%#" will substitute
35 #                  the number of commits for Git or the revision number
36 #                  for SVN.
37 #   pkg_enable   - Enable or disable local package versioning.
38 #   pkg_format   - Like "format", but used for the local package version.
39 #
40 # If run with the "-r" or "--set-release" argument the AC_INIT macro in
41 # configure.ac and the VERSION macro in config.nmake will have the
42 # pkg_format template appended to the version number. version.h will
43 # _not_ be generated if either argument is present.
44 #
45 # Default configuration:
46 #
47 # enable: 1
48 # git_client: 0
49 # svn_client: 0
50 # tortoise_svn: 0
51 # format: git %Y%m%d%H%M%S
52 # pkg_enable: 1
53 # pkg_format: -%#
54
55 # XXX - We're pretty dumb about the "%#" substitution, and about having
56 # spaces in the package format.
57
58 use strict;
59
60 use Time::Local;
61 use File::Basename;
62 use POSIX qw(strftime);
63 use Getopt::Long;
64 use Pod::Usage;
65 use IO::Handle;
66 use English;
67
68 my $version_file = 'version.h';
69 my $package_string = "";
70 my $vconf_file = 'version.conf';
71 my $vcs_name = "Git";
72 my $tortoise_file = "tortoise_template";
73 my $last_change = 0;
74 my $num_commits = 0;
75 my $commit_id = '';
76 my $repo_branch = "unknown";
77 my $git_description = undef;
78 my $get_vcs = 0;
79 my $set_vcs = 0;
80 my $print_vcs = 0;
81 my $set_version = 0;
82 my $set_release = 0;
83 my %version_pref = (
84         "version_major" => 2,
85         "version_minor" => 1,
86         "version_micro" => 0,
87         "version_build" => 0,
88
89         "enable"        => 1,
90         "git_client"    => 0,   # set if .git found and .git/svn not found
91         "svn_client"    => 0,   # set if .svn found
92         "tortoise_svn"  => 0,
93         "format"        => "git %Y%m%d%H%M%S",
94
95         # Normal development builds
96         "pkg_enable" => 1,
97         "pkg_format" => "-%#",
98
99         # Development releases
100         #"pkg_enable" => 0,
101         #"pkg_format" => "",
102         );
103 my $srcdir = ".";
104 my $info_cmd = "";
105
106 # Ensure we run with correct locale
107 $ENV{LANG} = "C";
108 $ENV{LC_ALL} = "C";
109 $ENV{GIT_PAGER} = "";
110
111 # Attempt to get revision information from the repository.
112 sub read_repo_info {
113         my $line;
114         my $version_format = $version_pref{"format"};
115         my $package_format = "";
116         my $in_entries = 0;
117         my $svn_name;
118         my $repo_version;
119         my $repo_root = undef;
120         my $repo_url = undef;
121         my $do_hack = 1;
122         my $info_source = "Unknown";
123
124         if ($version_pref{"pkg_enable"} > 0) {
125                 $package_format = $version_pref{"pkg_format"};
126         }
127
128         if (-d "$srcdir/.git" && ! -d "$srcdir/.git/svn") {
129                 $info_source = "Command line (git)";
130                 $version_pref{"git_client"} = 1;
131         } elsif (-d "$srcdir/.svn" or -d "$srcdir/../.svn") {
132                 $info_source = "Command line (svn info)";
133                 $info_cmd = "svn info $srcdir";
134                 $version_pref{"svn_client"} = 1;
135         } elsif (-d "$srcdir/.git/svn") {
136                 $info_source = "Command line (git-svn)";
137                 $info_cmd = "(cd $srcdir; git svn info)";
138         }
139
140         #Git can give us:
141         #
142         # A big ugly hash: git rev-parse HEAD
143         # 1ddc83849075addb0cac69a6fe3782f4325337b9
144         #
145         # A small ugly hash: git rev-parse --short HEAD
146         # 1ddc838
147         #
148         # The upstream branch path: git rev-parse --abbrev-ref --symbolic-full-name @{upstream}
149         # origin/master
150         #
151         # A version description: git describe --tags --dirty
152         # wireshark-1.8.12-15-g1ddc838
153         #
154         # Number of commits in this branch: git rev-list --count HEAD
155         # 48879
156         #
157         # Number of commits since 1.8.0: git rev-list --count 5e212d72ce098a7fec4332cbe6c22fcda796a018..HEAD
158         # 320
159         #
160         # Refs: git ls-remote code.wireshark.org:wireshark
161         # ea19c7f952ce9fc53fe4c223f1d9d6797346258b (r48972, changed version to 1.11.0)
162
163         if ($version_pref{"git_client"}) {
164                 eval {
165                         use warnings "all";
166                         no warnings "all";
167
168                         chomp($line = qx{git --git-dir=$srcdir/.git log -1 --pretty=format:%at});
169                         if ($? == 0 && length($line) > 1) {
170                                 $last_change = $line;
171                         }
172
173                         # Commits since last annotated tag.
174                         chomp($line = qx{git --git-dir=$srcdir/.git describe --long --always --match "v*"});
175                         if ($? == 0 && length($line) > 1) {
176                                 my @parts = split(/-/, $line);
177                                 $git_description = $line;
178                                 $num_commits = $parts[-2];
179                                 $commit_id = $parts[-1];
180                         }
181
182                         chomp($line = qx{git --git-dir=$srcdir/.git ls-remote --get-url origin});
183                         if (defined($line)) {
184                                 $repo_url = $line;
185                         }
186
187                         # This will break in some cases. Hopefully not during
188                         # official package builds.
189                         chomp($line = qx{git --git-dir=$srcdir/.git rev-parse --abbrev-ref --symbolic-full-name \@\{upstream\}});
190                         if ($? == 0 && length($line) > 1) {
191                                 $repo_branch = basename($line);
192                         }
193
194                         1;
195                 };
196
197                 if ($last_change && $num_commits && $repo_url && $repo_branch) {
198                         $do_hack = 0;
199                 }
200         } elsif ($version_pref{"svn_client"}) {
201                 eval {
202                         use warnings "all";
203                         no warnings "all";
204                         $line = qx{$info_cmd};
205                         if (defined($line)) {
206                                 if ($line =~ /Last Changed Date: (\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)/) {
207                                         $last_change = timegm($6, $5, $4, $3, $2 - 1, $1);
208                                 }
209                                 if ($line =~ /Last Changed Rev: (\d+)/) {
210                                         $num_commits = $1;
211                                 }
212                                 if ($line =~ /URL: (\S+)/) {
213                                         $repo_url = $1;
214                                 }
215                                 if ($line =~ /Repository Root: (\S+)/) {
216                                         $repo_root = $1;
217                                 }
218                                 $vcs_name = "SVN";
219                         }
220                         1;
221                 };
222
223                 if ($last_change && $num_commits && $repo_url && $repo_root) {
224                         $do_hack = 0;
225                 }
226         } elsif ($version_pref{"tortoise_svn"}) {
227                 # Dynamically generic template file needed by TortoiseSVN
228                 open(TORTOISE, ">$tortoise_file");
229                 print TORTOISE "#define VCSVERSION \"\$WCREV\$\"\r\n";
230                 print TORTOISE "#define VCSBRANCH \"\$WCURL\$\"\r\n";
231                 close(TORTOISE);
232
233                 $info_source = "Command line (SubWCRev)";
234                 $info_cmd = "SubWCRev $srcdir $tortoise_file $version_file";
235                 my $tortoise = system($info_cmd);
236                 if ($tortoise == 0) {
237                         $do_hack = 0;
238                 }
239                 $vcs_name = "SVN";
240
241                 #clean up the template file
242                 unlink($tortoise_file);
243         }
244
245         if ($num_commits == 0) {
246                 # Fall back to config.nmake
247                 $info_source = "Prodding config.nmake";
248                 my $filepath = "$srcdir/config.nmake";
249                 open(CFGNMAKE, "< $filepath") || die "Can't read $filepath!";
250                 while ($line = <CFGNMAKE>) {
251                         if ($line =~ /^VCS_REVISION=(\d+)/) {
252                                 $num_commits = $1;
253                                 $do_hack = 0;
254                                 last;
255                         }
256                 }
257                 close (CFGNMAKE);
258         }
259         if ($num_commits == 0 and -d "$srcdir/.git") {
260
261                 # Try git...
262                 eval {
263                         use warnings "all";
264                         no warnings "all";
265                         # If someone had properly tagged 1.9.0 we could also use
266                         # "git describe --abbrev=1 --tags HEAD"
267
268                         $info_cmd = "(cd $srcdir; git log --format='%b' -n 1)";
269                         $line = qx{$info_cmd};
270                         if (defined($line)) {
271                                 if ($line =~ /svn path=.*; revision=(\d+)/) {
272                                         $num_commits = $1;
273                                 }
274                         }
275                         $info_cmd = "(cd $srcdir; git log --format='%ad' -n 1 --date=iso)";
276                         $line = qx{$info_cmd};
277                         if (defined($line)) {
278                                 if ($line =~ /(\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)/) {
279                                         $last_change = timegm($6, $5, $4, $3, $2 - 1, $1);
280                                 }
281                         }
282                         $info_cmd = "(cd $srcdir; git branch)";
283                         $line = qx{$info_cmd};
284                         if (defined($line)) {
285                                 if ($line =~ /\* (\S+)/) {
286                                         $repo_branch = $1;
287                                 }
288                         }
289                         1;
290                         };
291         }
292         if ($num_commits == 0 and -d "$srcdir/.bzr") {
293
294                 # Try bzr...
295                 eval {
296                         use warnings "all";
297                         no warnings "all";
298                         $info_cmd = "(cd $srcdir; bzr log -l 1)";
299                         $line = qx{$info_cmd};
300                         if (defined($line)) {
301                                 if ($line =~ /timestamp: \S+ (\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)/) {
302                                         $last_change = timegm($6, $5, $4, $3, $2 - 1, $1);
303                                 }
304                                 if ($line =~ /svn revno: (\d+) \(on (\S+)\)/) {
305                                         $num_commits = $1;
306                                         $repo_branch = $2;
307                                 }
308                                 $vcs_name = "Bzr";
309                         }
310                         1;
311                         };
312         }
313
314
315         # 'svn info' failed or the user really wants us to dig around in .svn/entries
316         if ($do_hack) {
317                 # Start of ugly internal SVN file hack
318                 if (! open (ENTRIES, "< $srcdir/.svn/entries")) {
319                         print ("Unable to open $srcdir/.svn/entries\n");
320                 } else {
321                         $info_source = "Prodding .svn";
322                         # We need to find out whether our parser can handle the entries file
323                         $line = <ENTRIES>;
324                         chomp $line;
325                         if ($line eq '<?xml version="1.0" encoding="utf-8"?>') {
326                                 $repo_version = "pre1.4";
327                         } elsif ($line =~ /^8$/) {
328                                 $repo_version = "1.4";
329                         } else {
330                                 $repo_version = "unknown";
331                         }
332
333                         if ($repo_version eq "pre1.4") {
334                                 # The entries schema is flat, so we can use regexes to parse its contents.
335                                 while ($line = <ENTRIES>) {
336                                         if ($line =~ /<entry$/ || $line =~ /<entry\s/) {
337                                                 $in_entries = 1;
338                                                 $svn_name = "";
339                                         }
340                                         if ($in_entries) {
341                                                 if ($line =~ /name="(.*)"/) { $svn_name = $1; }
342                                                 if ($line =~ /committed-date="(\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)/) {
343                                                         $last_change = timegm($6, $5, $4, $3, $2 - 1, $1);
344                                                 }
345                                                 if ($line =~ /revision="(\d+)"/) { $num_commits = $1; }
346                                         }
347                                         if ($line =~ /\/>/) {
348                                                 if (($svn_name eq "" || $svn_name eq "svn:this_dir") &&
349                                                                 $last_change && $num_commits) {
350                                                         $in_entries = 0;
351                                                         last;
352                                                 }
353                                         }
354                                         # XXX - Fetch the repository root & URL
355                                 }
356                         }
357                         close ENTRIES;
358                 }
359         }
360
361         # If we picked up the revision and modification time,
362         # generate our strings.
363         if ($version_pref{"pkg_enable"}) {
364                 $version_format =~ s/%#/$num_commits/;
365                 $package_format =~ s/%#/$num_commits-$commit_id/;
366                 $package_string = strftime($package_format, gmtime($last_change));
367         }
368
369         if ($repo_url && $repo_root && index($repo_url, $repo_root) == 0) {
370                 $repo_branch = substr($repo_url, length($repo_root));
371         }
372
373         if ($get_vcs) {
374                 print <<"Fin";
375 Commit distance : $num_commits
376 Commit ID       : $commit_id
377 Revision source : $info_source
378 Release stamp   : $package_string
379 Fin
380         } elsif ($print_vcs) {
381                 print new_version_h();
382         }
383 }
384
385
386 # Read CMakeLists.txt, then write it back out with updated "set(PROJECT_..._VERSION ...)
387 # lines
388 # set(GIT_REVISION 999)
389 # set(PROJECT_MAJOR_VERSION 1)
390 # set(PROJECT_MINOR_VERSION 99)
391 # set(PROJECT_PATCH_VERSION 0)
392 # set(PROJECT_VERSION_EXTENSION "-rc5")
393 sub update_cmakelists_txt
394 {
395         my $line;
396         my $contents = "";
397         my $version = "";
398         my $filepath = "$srcdir/CMakeLists.txt";
399         my $cmake_package_string = "\$ENV{WIRESHARK_VERSION_EXTRA}";
400
401         if ($package_string ne "") { $cmake_package_string = $package_string; }
402
403         return if (!$set_version && $package_string eq "");
404
405         open(CFGIN, "< $filepath") || die "Can't read $filepath!";
406         while ($line = <CFGIN>) {
407                 if ($line =~ /^set *\( *GIT_REVISION .*([\r\n]+)$/) {
408                         $line = sprintf("set(GIT_REVISION %d)$1", $num_commits);
409                 } elsif ($line =~ /^set *\( *PROJECT_MAJOR_VERSION .*([\r\n]+)$/) {
410                         $line = sprintf("set(PROJECT_MAJOR_VERSION %d)$1", $version_pref{"version_major"});
411                 } elsif ($line =~ /^set *\( *PROJECT_MINOR_VERSION .*([\r\n]+)$/) {
412                         $line = sprintf("set(PROJECT_MINOR_VERSION %d)$1", $version_pref{"version_minor"});
413                 } elsif ($line =~ /^set *\( *PROJECT_PATCH_VERSION .*([\r\n]+)$/) {
414                         $line = sprintf("set(PROJECT_PATCH_VERSION %d)$1", $version_pref{"version_micro"});
415                 } elsif ($line =~ /^set *\( *PROJECT_VERSION_EXTENSION.*([\r\n]+)$/) {
416                         $line = sprintf("set(PROJECT_VERSION_EXTENSION \"%s\")$1", $cmake_package_string);
417                 }
418                 $contents .= $line
419         }
420
421         open(CFGIN, "> $filepath") || die "Can't write $filepath!";
422         print(CFGIN $contents);
423         close(CFGIN);
424         print "$filepath has been updated.\n";
425 }
426
427 # Read configure.ac, then write it back out with an updated
428 # "AC_INIT" line.
429 sub update_configure_ac
430 {
431         my $line;
432         my $contents = "";
433         my $version = "";
434         my $filepath = "$srcdir/configure.ac";
435
436         return if (!$set_version && $package_string eq "");
437
438         open(CFGIN, "< $filepath") || die "Can't read $filepath!";
439         while ($line = <CFGIN>) {
440                 if ($line =~ /^m4_define\( *\[?version_major\]? *,.*([\r\n]+)$/) {
441                         $line = sprintf("m4_define([version_major], [%d])$1", $version_pref{"version_major"});
442                 } elsif ($line =~ /^m4_define\( *\[?version_minor\]? *,.*([\r\n]+)$/) {
443                         $line = sprintf("m4_define([version_minor], [%d])$1", $version_pref{"version_minor"});
444                 } elsif ($line =~ /^m4_define\( *\[?version_micro\]? *,.*([\r\n]+)$/) {
445                         $line = sprintf("m4_define([version_micro], [%d])$1", $version_pref{"version_micro"});
446                 } elsif ($line =~ /^m4_append\( *\[?version_micro_extra\]? *,.*([\r\n]+)$/) {
447                         $line = sprintf("m4_append([version_micro_extra], [%s])$1", $package_string);
448                 }
449                 $contents .= $line
450         }
451
452         open(CFGIN, "> $filepath") || die "Can't write $filepath!";
453         print(CFGIN $contents);
454         close(CFGIN);
455         print "$filepath has been updated.\n";
456 }
457
458 # Read config.nmake, then write it back out with an updated
459 # "VERSION" line.
460 sub update_config_nmake
461 {
462         my $line;
463         my $contents = "";
464         my $version = "";
465         my $filepath = "$srcdir/config.nmake";
466         my $win_package_string = "\$(WIRESHARK_VERSION_EXTRA)";
467
468         if ($package_string ne "") { $win_package_string = $package_string; }
469
470
471         open(CFGNMAKE, "< $filepath") || die "Can't read $filepath!";
472         while ($line = <CFGNMAKE>) {
473                 if ($line =~ /^VCS_REVISION=.*([\r\n]+)$/) {
474                         $line = sprintf("VCS_REVISION=%d$1", $num_commits);
475                 } elsif ($set_version && $line =~ /^VERSION_MAJOR=.*([\r\n]+)$/) {
476                         $line = sprintf("VERSION_MAJOR=%d$1", $version_pref{"version_major"});
477                 } elsif ($set_version && $line =~ /^VERSION_MINOR=.*([\r\n]+)$/) {
478                         $line = sprintf("VERSION_MINOR=%d$1", $version_pref{"version_minor"});
479                 } elsif ($set_version && $line =~ /^VERSION_MICRO=.*([\r\n]+)$/) {
480                         $line = sprintf("VERSION_MICRO=%d$1", $version_pref{"version_micro"});
481                 } elsif ($line =~ /^VERSION_EXTRA=.*([\r\n]+)$/) {
482                         $line = "VERSION_EXTRA=$win_package_string$1";
483                 }
484                 $contents .= $line
485         }
486
487         open(CFGNMAKE, "> $filepath") || die "Can't write $filepath!";
488         print(CFGNMAKE $contents);
489         close(CFGNMAKE);
490         print "$filepath has been updated.\n";
491 }
492
493 # Read docbook/asciidoc.conf, then write it back out with an updated
494 # wireshark-version replacement line.
495 sub update_release_notes
496 {
497         my $line;
498         my $contents = "";
499         my $version = "";
500         my $filepath = "$srcdir/docbook/asciidoc.conf";
501
502         return if (!$set_version);
503
504         open(ADOC_CONF, "< $filepath") || die "Can't read $filepath!";
505         while ($line = <ADOC_CONF>) {
506                 # wireshark-version:\[\]=1.9.1
507
508                 if ($line =~ /^wireshark-version:\\\[\\\]=.*([\r\n]+)$/) {
509                         $line = sprintf("wireshark-version:\\\[\\\]=%d.%d.%d$1",
510                                         $version_pref{"version_major"},
511                                         $version_pref{"version_minor"},
512                                         $version_pref{"version_micro"},
513                                        );
514                 }
515                 $contents .= $line
516         }
517
518         open(ADOC_CONF, "> $filepath") || die "Can't write $filepath!";
519         print(ADOC_CONF $contents);
520         close(ADOC_CONF);
521         print "$filepath has been updated.\n";
522 }
523
524 # Read debian/changelog, then write back out an updated version.
525 sub update_debian_changelog
526 {
527         my $line;
528         my $contents = "";
529         my $version = "";
530         my $filepath = "$srcdir/debian/changelog";
531
532         return if ($set_version == 0);
533
534         open(CHANGELOG, "< $filepath") || die "Can't read $filepath!";
535         while ($line = <CHANGELOG>) {
536                 if ($set_version && CHANGELOG->input_line_number() == 1) {
537                         $line = sprintf("wireshark (%d.%d.%d) unstable; urgency=low\n",
538                                         $version_pref{"version_major"},
539                                         $version_pref{"version_minor"},
540                                         $version_pref{"version_micro"},
541                                        );
542                 }
543                 $contents .= $line
544         }
545
546         open(CHANGELOG, "> $filepath") || die "Can't write $filepath!";
547         print(CHANGELOG $contents);
548         close(CHANGELOG);
549         print "$filepath has been updated.\n";
550 }
551
552 # Read Makefile.am for each library, then write back out an updated version.
553 sub update_automake_lib_releases
554 {
555         my $line;
556         my $contents = "";
557         my $version = "";
558         my $filedir;
559         my $filepath;
560
561         return if (!$set_version);
562
563         # The Libtool manual says
564         #   "If the library source code has changed at all since the last
565         #    update, then increment revision (‘c:r:a’ becomes ‘c:r+1:a’)."
566         # epan changes with each minor release, almost by definition. wiretap
567         # changes with *most* releases.
568         #
569         # http://www.gnu.org/software/libtool/manual/libtool.html#Updating-version-info
570         for $filedir ("epan", "wiretap") {      # "wsutil"
571                 $contents = "";
572                 $filepath = $filedir . "/Makefile.am";
573                 open(MAKEFILE_AM, "< $filepath") || die "Can't read $filepath!";
574                 while ($line = <MAKEFILE_AM>) {
575                         # libwireshark_la_LDFLAGS = -version-info 2:1:1 -export-symbols
576
577                         if ($line =~ /^(lib\w+_la_LDFLAGS.*version-info\s+\d+:)\d+(:\d+.*)/) {
578                                 $line = sprintf("$1%d$2\n", $version_pref{"version_micro"});
579                         }
580                         $contents .= $line
581                 }
582
583                 open(MAKEFILE_AM, "> $filepath") || die "Can't write $filepath!";
584                 print(MAKEFILE_AM $contents);
585                 close(MAKEFILE_AM);
586                 print "$filepath has been updated.\n";
587         }
588 }
589
590 # Read CMakeLists.txt for each library, then write back out an updated version.
591 sub update_cmake_lib_releases
592 {
593         my $line;
594         my $contents = "";
595         my $version = "";
596         my $filedir;
597         my $filepath;
598
599         return if (!$set_version);
600
601         for $filedir ("epan", "wiretap") {      # "wsutil"
602                 $contents = "";
603                 $filepath = $filedir . "/CMakeLists.txt";
604                 open(CMAKELISTS_TXT, "< $filepath") || die "Can't read $filepath!";
605                 while ($line = <CMAKELISTS_TXT>) {
606                         # set(FULL_SO_VERSION "0.0.0")
607
608                         if ($line =~ /^(set\s*\(\s*FULL_SO_VERSION\s+"\d+\.\d+\.)\d+(".*)/) {
609                                 $line = sprintf("$1%d$2\n", $version_pref{"version_micro"});
610                         }
611                         $contents .= $line
612                 }
613
614                 open(CMAKELISTS_TXT, "> $filepath") || die "Can't write $filepath!";
615                 print(CMAKELISTS_TXT $contents);
616                 close(CMAKELISTS_TXT);
617                 print "$filepath has been updated.\n";
618         }
619 }
620
621 # Update distributed files that contain any version information
622 sub update_versioned_files
623 {
624         &update_cmakelists_txt;
625         &update_configure_ac;
626         &update_config_nmake;
627         &update_release_notes;
628         &update_debian_changelog;
629         &update_automake_lib_releases;
630         &update_cmake_lib_releases;
631 }
632
633 sub new_version_h
634 {
635         my $VCS_REVISION;
636
637         if ($git_description) {
638                 $VCS_REVISION = "#define VCSVERSION \"" .
639                         $git_description . "\"\n" .
640                         "#define VCSBRANCH \"" . $repo_branch . "\"\n";
641         } elsif ($last_change && $num_commits) {
642                 $VCS_REVISION = "#define VCSVERSION \"" . $vcs_name . " Rev " .
643                         $num_commits . "\"\n" .
644                         "#define VCSBRANCH \"" . $repo_branch . "\"\n";
645         } else {
646                 $VCS_REVISION = "#define VCSVERSION \"" . $vcs_name .
647                         " Rev Unknown\"\n" .
648                         "#define VCSBRANCH \"unknown\"\n";
649         }
650
651         return $VCS_REVISION;
652 }
653
654 # Print the version control system's version to $version_file.
655 # Don't change the file if it is not needed.
656 sub print_VCS_REVISION
657 {
658         my $VCS_REVISION = new_version_h();
659         my $needs_update = 1;
660         if (open(OLDREV, "<$version_file")) {
661                 my $old_VCS_REVISION = <OLDREV> . <OLDREV>;
662                 if ($old_VCS_REVISION eq $VCS_REVISION) {
663                         $needs_update = 0;
664                 }
665                 close OLDREV;
666         }
667
668         if (! $set_vcs) { return; }
669
670         if ($needs_update) {
671                 # print "Updating $version_file so it contains:\n$VCS_REVISION";
672                 open(VER, ">$version_file") || die ("Cannot write to $version_file ($!)\n");
673                 print VER "$VCS_REVISION";
674                 close VER;
675                 print "$version_file has been updated.\n";
676         } else {
677                 print "$version_file unchanged.\n";
678         }
679 }
680
681 # Read values from the configuration file, if it exists.
682 sub get_config {
683         my $arg;
684         my $show_help = 0;
685
686         # Get our command-line args
687         # XXX - Do we need an option to undo --set-release?
688         GetOptions(
689                    "help|h", \$show_help,
690                    "get-vcs|get-svn|g", \$get_vcs,
691                    "set-vcs|set-svn|s", \$set_vcs,
692                    "print-vcs", \$print_vcs,
693                    "set-version|v", \$set_version,
694                    "set-release|r|package-version|p", \$set_release
695                    ) || pod2usage(2);
696
697         if ($show_help) { pod2usage(1); }
698
699         if ( !( $show_help || $get_vcs || $set_vcs || $print_vcs || $set_version || $set_release ) ) {
700                 $set_vcs = 1;
701         }
702
703         if ($#ARGV >= 0) {
704                 $srcdir = $ARGV[0]
705         }
706
707         if (! open(FILE, "<$vconf_file")) {
708                 print STDERR "Version configuration file $vconf_file not "
709                 . "found.  Using defaults.\n";
710                 return 1;
711         }
712
713         while (<FILE>) {
714                 s/^\s+|\s+$//g; # chomp() may not handle CR
715                 next if (/^#/);
716                 next unless (/^(\w+)(:|=)\s*(\S.*)/);
717                 $version_pref{$1} = $3;
718         }
719         close FILE;
720         return 1;
721 }
722
723 ##
724 ## Start of code
725 ##
726
727 &get_config();
728
729 &read_repo_info();
730
731 &print_VCS_REVISION;
732
733 if ($set_version || $set_release) {
734         if ($set_version) {
735                 print "Generating version information\n";
736         }
737
738         if ($version_pref{"enable"} == 0) {
739                 print "Release information disabled in $vconf_file.\n";
740                 $set_release = 0;
741         }
742
743         if ($set_release) {
744                 print "Generating release information\n";
745         } else {
746                 print "Resetting release information\n";
747                 $num_commits = 0;
748                 $package_string = "";
749         }
750
751         &update_versioned_files;
752 }
753
754 __END__
755
756 =head1 NAM
757
758 make-version.pl - Get and set build-time version information for Wireshark
759
760 =head1 SYNOPSIS
761
762 make-version.pl [options] [source directory]
763
764   Options:
765
766     --help, -h                 This help message
767     --get-vcs, -g              Print the VCS revision and source.
768     --set-vcs, -s              Set the information in version.h
769     --print-vcs                Print the vcs version to standard output
770     --set-version, -v          Set the major, minor, and micro versions in
771                                configure.ac, config.nmake, debian/changelog,
772                                and docbook/asciidoc.conf.
773                                Resets the release information when used by
774                                itself.
775     --set-release, -r          Set the release information in configure.ac
776                                and config.nmake
777     --package-version, -p      Deprecated. Same as --set-release.
778
779 Options can be used in any combination. If none are specified B<--set-svn>
780 is assumed.
781
782 =cut
783
784 #
785 # Editor modelines  -  http://www.wireshark.org/tools/modelines.html
786 #
787 # Local variables:
788 # c-basic-offset: 8
789 # tab-width: 8
790 # indent-tabs-mode: t
791 # End:
792 #
793 # vi: set shiftwidth=8 tabstop=8 noexpandtab:
794 # :indentSize=8:tabSize=8:noTabs=false:
795 #
796 #