summaryrefslogtreecommitdiff
path: root/utils/svn-testament.pl
blob: 17e6970045bdb17c2ed290a8ad6feb2bb944792f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/perl -w

use strict;
use POSIX;

=head1

Generate a testament describing the current SVN status. This gets written
out in a C form which can be used to construct the NetSurf SVN testament
file for signon notification.

If there is no SVN in place, the data is invented arbitrarily.

=cut

my $root = shift @ARGV;
my $targetfile = shift @ARGV;

my %svninfo; # The SVN info output

$root .= "/" unless ($root =~ m@/$@);

my $svn_present = 0;
if ( -d ".svn" ) {
   $svn_present = 1;
}

if ( $svn_present ) {
   foreach my $line (split(/\n/, `svn info $root`)) {
      my ($key, $value) = split(/: /, $line, 2);
      $key = lc($key);
      $key =~ s/\s+//g;
      $svninfo{$key} = $value;
   }
} else {
   $svninfo{repositoryroot} = "http://nowhere/";
   $svninfo{url} = "http://nowhere/tarball/";
   $svninfo{revision} = "unknown";
}

my %svnstatus; # The SVN status output

if ( $svn_present ) {
   foreach my $line (split(/\n/, `svn status $root`)) {
      chomp $line;
      my $op = substr($line, 0, 1);
      if ($op eq ' ' && substr($line, 1, 1) ne ' ') { $op = "p"; }
      my $fn = substr($line, 8);
      $fn = substr($fn, length($root)) if (substr($fn, 0, length($root)) eq $root);
      next unless (care_about_file($fn, $op));
      $svnstatus{$fn} = $op;
   }
}

my %userinfo; # The information about the current user

{
   my @pwent = getpwuid($<);
   $userinfo{USERNAME} = $pwent[0];
   my $gecos = $pwent[6];
   $gecos =~ s/,.+//g;
   $gecos =~ s/"/'/g;
   $userinfo{GECOS} = $gecos;
}

# The current date, in AmigaOS version friendly format (dd.mm.yyyy)

my $compiledate = POSIX::strftime("%d.%m.%Y", localtime);
chomp $compiledate;

# Spew the testament out

my $testament = "";

$testament .= "#define USERNAME \"$userinfo{USERNAME}\"\n";
$testament .= "#define GECOS \"$userinfo{GECOS}\"\n";

my $qroot = $root;
$qroot =~ s/"/\\"/g;

my $hostname = $ENV{HOSTNAME};

if ( !defined ( $hostname ) ) { # Try hostname command if env-var empty
   $hostname = `hostname`;
   chomp $hostname;
}

$hostname = "unknown-host" unless (defined($hostname) && $hostname ne "");
$hostname =~ s/"/\\"/g;

$testament .= "#define WT_ROOT \"$qroot\"\n";
$testament .= "#define WT_HOSTNAME \"$hostname\"\n";
$testament .= "#define WT_COMPILEDATE \"$compiledate\"\n";

my $url = $svninfo{url};
# This only works on 1.3.x and above
$url = substr($url, length($svninfo{repositoryroot}));
if ( substr($url,0,1) ne '/' ) { $url = "/$url"; }
$testament .= "#define WT_BRANCHPATH \"$url\"\n";
if ($url =~ m@/trunk/@) {
   $testament .= "#define WT_BRANCHISTRUNK 1\n";
}
if ($url =~ m@/tags/@) {
   $testament .= "#define WT_BRANCHISTAG 1\n";
}
if ($url =~ m@/tarball/@) {
   $testament .= "#define WT_NO_SVN 1\n";
}
$testament .= "#define WT_REVID \"$svninfo{revision}\"\n";
$testament .= "#define WT_MODIFIED " . scalar(keys %svnstatus) . "\n";
$testament .= "#define WT_MODIFICATIONS {\\\n";
my $doneone = 0;
foreach my $filename (sort keys %svnstatus) {
   if ($doneone) {
      $testament .= ", \\\n";
   }
   $testament .= "  { \"$filename\", '$svnstatus{$filename}' }";
   $doneone = 1;
}
$testament .= " \\\n}\n";

use Digest::MD5 qw(md5_hex);

my $oldcsum = "";
if ( -e $targetfile ) {
   open OLDVALUES, "<", $targetfile;
   foreach my $line (readline(OLDVALUES)) {
      if ($line =~ /MD5:([0-9a-f]+)/) {
         $oldcsum = $1;
      }
   }
   close OLDVALUES;
}

my $newcsum = md5_hex($testament);

if ($oldcsum ne $newcsum) {
   print "TESTMENT: $targetfile\n";
   open NEWVALUES, ">", $targetfile or die "$!";
   print NEWVALUES "/* ", $targetfile,"\n";
   print NEWVALUES <<'EOS';
 * 
 * Revision testament.
 *
 * *WARNING* this file is automatically generated by svn-testament.pl 
 *
 * Copyright 2011 NetSurf Browser Project
 */

EOS

   print NEWVALUES "#ifndef NETSURF_REVISION_TESTAMENT\n";
   print NEWVALUES "#define NETSURF_REVISION_TESTAMENT \"$newcsum\"\n\n";
   print NEWVALUES "/* Revision testament checksum:\n";
   print NEWVALUES " * MD5:", $newcsum,"\n */\n\n";
   print NEWVALUES "/* Revision testament: */\n";
   print NEWVALUES $testament;
   print NEWVALUES "\n#endif\n";
   close NEWVALUES;
     foreach my $unwanted (@ARGV) {
        next unless(-e $unwanted);
        print "TESTAMENT: Removing $unwanted\n";
        system("rm", "-f", "--", $unwanted);
     }
} else {
   print "TESTMENT: unchanged\n";
}

exit 0;

sub care_about_file {
   my ($fn, $op) = @_;
   return 0 if ($fn =~ /\.d$/); # Don't care for extraneous DEP files
   return 0 if ($fn =~ /\.a$/); # Don't care for extraneous archive files
   return 0 if ($fn =~ /\.md5$/); # Don't care for md5sum files
   return 0 if ($fn =~ /\.map$/); # Don't care for map files
   return 1;
}