From Mark C. Brown: fix a typo.
[obnox/wireshark/wip.git] / make-version.pl
1 #!/usr/bin/perl -w
2 #
3 # Copyright 2004 Jörg Mayer (see AUTHORS file)
4 #
5 # $Id$
6 #
7 # Ethereal - Network traffic analyzer
8 # By Gerald Combs <gerald@ethereal.com>
9 # Copyright 1998 Gerald Combs
10 #
11 # This program is free software; you can redistribute it and/or
12 # modify it under the terms of the GNU General Public License
13 # as published by the Free Software Foundation; either version 2
14 # of the License, or (at your option) any later version.
15 #
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 # GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License
22 # along with this program; if not, write to the Free Software
23 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24
25 # usage:  ./make-version.pl
26 #
27 # If "version.conf" is present, it is parsed for configuration values.  
28 # Possible values are:
29 #
30 #   enable - Enable or disable versioning.  Zero (0) disables, nonzero
31 #            enables.
32 #   format - A strftime() formatted string to use as a template for the
33 #            version string.
34 #
35 # Default configuration:
36 #
37 # enable: 1
38 # format: SVN %Y%m%d%H%M%S
39
40 use strict;
41
42 use Time::Local;
43 use POSIX qw(strftime);
44
45 my $version_file = 'svnversion.h';
46 my $vconf_file = 'version.conf';
47 my $last = 0;
48 my %version_pref = ("enable" => 1, "format" => "SVN %Y%m%d%H%M%S");
49
50
51 # Recursively find all SVN Entries files starting from the given directory,
52 # and compute the modification time of the most recently modified Entries file.
53 sub read_svn_info {
54         my $line;
55
56         open(SVNINFO, "svn info |") || return;
57         while ($line = <SVNINFO>) {
58                 if ($line =~ /^Last Changed Date: (\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)/) {
59                         $last = timegm($6, $5, $4, $3, $2 - 1, $1);
60                 }
61         }
62         close SVNINFO;
63 }
64
65
66 # Print the SVN version to $version_file.
67 # Don't change the file if it is not needed.
68 sub print_svn_version
69 {
70         my $svn_version;
71         my $needs_update = 1;
72
73         if ($last) {
74                 $svn_version = "#define SVNVERSION \"" . 
75                         strftime($version_pref{"format"}, gmtime($last)) .
76                         "\"\n";
77         } else {
78                 $svn_version = "/* #define SVNVERSION \"\" */\n";
79         }
80         if (open(OLDVER, "<$version_file")) {
81                 if (<OLDVER> eq $svn_version) {
82                         print "$version_file is up-to-date.\n";
83                         $needs_update = 0;
84                 }
85                 close OLDVER;
86         }
87
88         if ($needs_update == 1) {
89                 # print "Updating $version_file so it contains:\n$svn_version";
90                 open(VER, ">$version_file") || die ("Cannot write to $version_file ($!)\n");
91                 print VER "$svn_version";
92                 close VER;
93                 print "$version_file has been updated.\n";
94         }
95 }
96
97 # Read values from the configuration file, if it exists.
98 sub get_config {
99         open(FILE, "<$vconf_file") || print STDERR "Version configuration file $vconf_file not found.  Using defaults.\n" && return 1;
100
101         while (<FILE>) {
102                 chomp;
103                 next if (/^#/);
104                 next unless (/^(\w+):\s+(\S.*)/);
105                 $version_pref{$1} = $2;
106         }
107         close FILE;
108         return 1;
109 }
110
111 ##
112 ## Start of code
113 ##
114
115 &get_config();
116
117 if ($version_pref{"enable"} == 0) {
118         print "Version tag disabled in $vconf_file.\n";
119 } elsif (-d "./.svn") {
120         print "This is a build from SVN (or a SVN snapshot), "
121         . "SVN version tag will be computed.\n";
122         &read_svn_info(".");
123 } else {
124         print "This is not a SVN build.\n";
125 }
126
127 # Now that we've computed everything, print the SVN version to $version_file
128 &print_svn_version;
129
130 __END__