b3deda66eac40a2503da6bb4579385b7cb1f7311
[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: make-version.pl,v 1.7 2004/03/04 16:19:40 jmayer Exp $
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: CVS %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 = 'cvsversion.h';
46 my $vconf_file = 'version.conf';
47 my %monthnum = ( "Jan" => "0", "Feb" => "1", "Mar" => "2", "Apr" => "3",
48                 "May" => "4", "Jun" => "5", "Jul" => "6", "Aug" => "7",
49                 "Sep" => "8", "Oct" => "9", "Nov" => "10", "Dec" => "11" );
50 my $last = 0;
51 my $last_file = undef;
52 my %version_pref = ("enable" => 1, "format" => "CVS %Y%m%d%H%M%S");
53
54
55 # Recursively find all CVS Entries files starting from the given directory,
56 # and compute the modification time of the most recently modified Entries file.
57 sub find_last_CVS_Entries {
58         my $dir = shift;
59         my $d;
60
61         opendir(DIR, "$dir") || print STDERR "Can't open directory $dir ($!)\n" && next;
62         foreach $d (readdir(DIR)) {
63                 if (-d "$dir/$d" && $d !~ /^\.(|.)$/) {
64                         if ($d =~ /^CVS$/) {
65                                 if (-f "$dir/CVS/Entries") {
66                                         &lastentry("$dir/CVS/Entries");
67                                 }
68                         } else { # Recurse in directory
69                                 &find_last_CVS_Entries("$dir/$d");
70                         }
71                 }
72         }
73         closedir DIR;
74 }
75
76
77 # Check all entries in $file. In case they are newer, update $last accordingly
78 # Args: Entries file
79 sub lastentry {
80         my $date;
81         my ($wdayascii, $monthascii, $day, $time, $year);
82         my $file = shift;
83         my $current;
84
85         open(FILE, "<$file") || print STDERR "Open $file for reading failed ($!)\n" && return 1;
86
87         while (<FILE>) {
88                 chomp;
89                 # Regular lines look like this: /ethereal_be.py/1.6/Fri Aug  2 22:55:19 2002//
90                 next if (/^D/);
91                 $date = (split(/\//, $_, 5))[3];
92                 #                        Month   Day   Hour   Minute Second Year
93                 next if ($date !~ /\w{3} (\w{3}) (.\d) (\d\d):(\d\d):(\d\d) (\d{4})/);
94                 $current = timegm($5, $4, $3, $2, $monthnum{$1}, $6);
95
96                 if ($current > $last) {
97                         $last = $current;
98                 }
99         }
100         close FILE;
101         return 1;
102 }
103
104
105 # Print the CVS version to $version_file.
106 # Don't change the file if it is not needed.
107 sub print_cvs_version
108 {
109         my $cvs_version;
110         my $needs_update = 1;
111
112         if ($last) {
113                 $cvs_version = "#define CVSVERSION \"" . 
114                         strftime($version_pref{"format"}, gmtime($last)) .
115                         "\"\n";
116         } else {
117                 $cvs_version = "/* #define CVSVERSION \"\" */\n";
118         }
119         if (open(OLDVER, "<$version_file")) {
120                 if (<OLDVER> eq $cvs_version) {
121                         print "$version_file is up-to-date.\n";
122                         $needs_update = 0;
123                 }
124                 close OLDVER;
125         }
126
127         if ($needs_update == 1) {
128                 # print "Updating $version_file so it contains:\n$cvs_version";
129                 open(VER, ">$version_file") || die ("Cannot write to $version_file ($!)\n");
130                 print VER "$cvs_version";
131                 close VER;
132                 print "$version_file has been updated.\n";
133         }
134 }
135
136 # Read values from the configuration file, if it exists.
137 sub get_config {
138         open(FILE, "<$vconf_file") || print STDERR "Version configuration file $vconf_file not found.  Using defaults.\n" && return 1;
139
140         while (<FILE>) {
141                 chomp;
142                 next if (/^#/);
143                 next unless (/^(\w+):\s+(\S.*)/);
144                 $version_pref{$1} = $2;
145         }
146         close FILE;
147         return 1;
148 }
149
150 ##
151 ## Start of code
152 ##
153
154 &get_config();
155
156 if ($version_pref{"enable"} == 0) {
157         print "Version tag disabled in $vconf_file.\n";
158 } elsif (-d "./CVS") {
159         print "This is a build from CVS (or a CVS snapshot), "
160         . "CVS version tag will be computed.\n";
161         &find_last_CVS_Entries(".");
162 } else {
163         print "This is not a CVS build.\n";
164 }
165
166 # Now that we've computed everything, print the CVS version to $version_file
167 &print_cvs_version;
168
169 __END__