b66a753865d4705e19179680e84c54a001fb57db
[freeside.git] / rt / devel / tools / license_tag
1 #!/usr/bin/env perl
2
3
4 # BEGIN BPS TAGGED BLOCK {{{
5 #
6 # COPYRIGHT:
7 #
8 # This software is Copyright (c) 1996-2016 Best Practical Solutions, LLC
9 #                                          <sales@bestpractical.com>
10 #
11 # (Except where explicitly superseded by other copyright notices)
12 #
13 #
14 # LICENSE:
15 #
16 # This work is made available to you under the terms of Version 2 of
17 # the GNU General Public License. A copy of that license should have
18 # been provided with this software, but in any event can be snarfed
19 # from www.gnu.org.
20 #
21 # This work is distributed in the hope that it will be useful, but
22 # WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24 # General Public License for more details.
25 #
26 # You should have received a copy of the GNU General Public License
27 # along with this program; if not, write to the Free Software
28 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
29 # 02110-1301 or visit their web page on the internet at
30 # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
31 #
32 #
33 # CONTRIBUTION SUBMISSION POLICY:
34 #
35 # (The following paragraph is not intended to limit the rights granted
36 # to you to modify and distribute this software under the terms of
37 # the GNU General Public License and is only of importance to you if
38 # you choose to contribute your changes and enhancements to the
39 # community by submitting them to Best Practical Solutions, LLC.)
40 #
41 # By intentionally submitting any modifications, corrections or
42 # derivatives to this work, or any other work intended for use with
43 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
44 # you are the copyright holder for those contributions and you grant
45 # Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
46 # royalty-free, perpetual, license to use, copy, create derivative
47 # works based on those contributions, and sublicense and distribute
48 # those contributions and any derivatives thereof.
49 #
50 # END BPS TAGGED BLOCK }}}
51 use strict;
52 use warnings;
53 my $LICENSE  = <<'EOL';
54
55 COPYRIGHT:
56
57 This software is Copyright (c) 1996-2016 Best Practical Solutions, LLC
58                                          <sales@bestpractical.com>
59
60 (Except where explicitly superseded by other copyright notices)
61
62
63 LICENSE:
64
65 This work is made available to you under the terms of Version 2 of
66 the GNU General Public License. A copy of that license should have
67 been provided with this software, but in any event can be snarfed
68 from www.gnu.org.
69
70 This work is distributed in the hope that it will be useful, but
71 WITHOUT ANY WARRANTY; without even the implied warranty of
72 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
73 General Public License for more details.
74
75 You should have received a copy of the GNU General Public License
76 along with this program; if not, write to the Free Software
77 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
78 02110-1301 or visit their web page on the internet at
79 http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
80
81
82 CONTRIBUTION SUBMISSION POLICY:
83
84 (The following paragraph is not intended to limit the rights granted
85 to you to modify and distribute this software under the terms of
86 the GNU General Public License and is only of importance to you if
87 you choose to contribute your changes and enhancements to the
88 community by submitting them to Best Practical Solutions, LLC.)
89
90 By intentionally submitting any modifications, corrections or
91 derivatives to this work, or any other work intended for use with
92 Request Tracker, to Best Practical Solutions, LLC, you confirm that
93 you are the copyright holder for those contributions and you grant
94 Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
95 royalty-free, perpetual, license to use, copy, create derivative
96 works based on those contributions, and sublicense and distribute
97 those contributions and any derivatives thereof.
98
99 EOL
100
101 use File::Find;
102
103 my @MAKE = qw(Makefile);
104
105 File::Find::find({ no_chdir => 1, wanted => \&tag_pm}, 'lib');
106 for my $masondir (qw( html share/html )) {
107     next unless -d $masondir;
108     File::Find::find({ no_chdir => 1, wanted => \&tag_mason}, $masondir);
109 }
110 for my $bindir (qw( sbin bin etc/upgrade devel/tools )) {
111     next unless -d $bindir;
112     File::Find::find({ no_chdir => 1, wanted => \&tag_script}, $bindir);
113 }
114 tag_makefile ('Makefile.in') if -f 'Makefile.in';
115 tag_makefile ('README');
116
117
118 sub tag_mason {
119         my $pm = $_;
120         return unless (-f $pm);
121         return if $pm =~ /\.(?:png|jpe?g|gif)$/;
122         open( FILE, '<', $pm ) or die "Failed to open $pm";
123         my $file = (join "", <FILE>);
124         close (FILE);
125         print "$pm - ";
126         return if another_license($pm => $file) && print "has different license\n";
127
128         my $pmlic = $LICENSE;
129         $pmlic =~ s/^/%# /mg;
130         $pmlic =~ s/\s*$//mg;
131         if ($file =~ /^%# BEGIN BPS TAGGED BLOCK \{\{\{/ms) {
132                 print "has license section";
133              $file =~ s/^%# BEGIN BPS TAGGED BLOCK \{\{\{(.*?)%# END BPS TAGGED BLOCK \}\}\}/%# BEGIN BPS TAGGED BLOCK {{{\n$pmlic\n%# END BPS TAGGED BLOCK }}}/ms;
134
135
136         } else {
137                 print "no license section";
138              $file ="%# BEGIN BPS TAGGED BLOCK {{{\n$pmlic\n%# END BPS TAGGED BLOCK }}}\n". $file;
139         }
140         $file =~ s/%# END BPS TAGGED BLOCK \}\}\}(\n+)/%# END BPS TAGGED BLOCK }}}\n/mg;
141         print "\n";
142
143
144
145
146         open( FILE, '>', $pm ) or die "couldn't write new file";
147         print FILE $file;
148         close FILE;
149
150 }
151
152
153 sub tag_makefile {
154         my $pm = shift;
155         open( FILE, '<', $pm ) or die "Failed to open $pm";
156         my $file = (join "", <FILE>);
157         close (FILE);
158         print "$pm - ";
159         return if another_license($pm => $file) && print "has different license\n";
160
161         my $pmlic = $LICENSE;
162         $pmlic =~ s/^/# /mg;
163         $pmlic =~ s/\s*$//mg;
164         if ($file =~ /^# BEGIN BPS TAGGED BLOCK \{\{\{/ms) {
165                 print "has license section";
166              $file =~ s/^# BEGIN BPS TAGGED BLOCK \{\{\{(.*?)# END BPS TAGGED BLOCK \}\}\}/# BEGIN BPS TAGGED BLOCK {{{\n$pmlic\n# END BPS TAGGED BLOCK }}}/ms;
167
168
169         } else {
170                 print "no license section";
171              $file ="# BEGIN BPS TAGGED BLOCK {{{\n$pmlic\n# END BPS TAGGED BLOCK }}}\n". $file;
172         }
173         $file =~ s/# END BPS TAGGED BLOCK \}\}\}(\n+)/# END BPS TAGGED BLOCK }}}\n/mg;
174         print "\n";
175
176
177
178
179         open( FILE, '>', $pm ) or die "couldn't write new file";
180         print FILE $file;
181         close FILE;
182
183 }
184
185
186 sub tag_pm {
187         my $pm = $_;
188         return unless $pm =~ /\.pm/s;
189         open( FILE, '<', $pm ) or die "Failed to open $pm";
190         my $file = (join "", <FILE>);
191         close (FILE);
192         print "$pm - ";
193         return if another_license($pm => $file) && print "has different license\n";
194
195         my $pmlic = $LICENSE;
196         $pmlic =~ s/^/# /mg;
197         $pmlic =~ s/\s*$//mg;
198         if ($file =~ /^# BEGIN BPS TAGGED BLOCK \{\{\{/ms) {
199                 print "has license section";
200              $file =~ s/^# BEGIN BPS TAGGED BLOCK \{\{\{(.*?)# END BPS TAGGED BLOCK \}\}\}/# BEGIN BPS TAGGED BLOCK {{{\n$pmlic\n# END BPS TAGGED BLOCK }}}/ms;
201
202
203         } else {
204                 print "no license section";
205              $file ="# BEGIN BPS TAGGED BLOCK {{{\n$pmlic\n# END BPS TAGGED BLOCK }}}\n". $file;
206         }
207         $file =~ s/# END BPS TAGGED BLOCK \}\}\}(\n+)/# END BPS TAGGED BLOCK }}}\n\n/mg;
208         print "\n";
209
210
211
212
213         open( FILE, '>', $pm ) or die "couldn't write new file $pm";
214         print FILE $file;
215         close FILE;
216
217 }
218
219
220 sub tag_script {
221         my $pm = $_;
222         return unless (-f $pm);
223         open( FILE, '<', $pm ) or die "Failed to open $pm";
224         my $file = (join "", <FILE>);
225         close (FILE);
226         print "$pm - ";
227         return if another_license($pm => $file) && print "has different license\n";
228
229         my $pmlic = $LICENSE;
230         $pmlic =~ s/^/# /msg;
231         $pmlic =~ s/\s*$//mg;
232         if ($file =~ /^# BEGIN BPS TAGGED BLOCK \{\{\{/ms) {
233                 print "has license section";
234              $file =~ s/^# BEGIN BPS TAGGED BLOCK \{\{\{(.*?)# END BPS TAGGED BLOCK \}\}\}/# BEGIN BPS TAGGED BLOCK {{{\n$pmlic\n# END BPS TAGGED BLOCK }}}/ms;
235
236
237         } else {
238                 print "no license section";
239                 if ($file =~ /^(#!.*?)\n/) {
240
241             my  $lic ="# BEGIN BPS TAGGED BLOCK {{{\n$pmlic\n# END BPS TAGGED BLOCK }}}\n";
242                 $file =~ s/^(#!.*?)\n/$1\n$lic/;
243
244                 }
245         }
246         $file =~ s/# END BPS TAGGED BLOCK \}\}\}(\n+)/# END BPS TAGGED BLOCK }}}\n/mg;
247         print "\n";
248
249
250         open( FILE, '>', $pm ) or die "couldn't write new file";
251         print FILE $file;
252         close FILE;
253
254 }
255
256 sub another_license {
257     my $name = shift;
258     my $file = shift;
259
260     return 1 if ($name =~ /(?:ckeditor|scriptaculous|superfish|tablesorter|farbtastic)/i);
261
262     return 0 if $file =~ /Copyright\s+\(c\)\s+\d\d\d\d-\d\d\d\d Best Practical Solutions/i;
263     return 1 if $file =~ /\b(copyright|GPL|Public Domain)\b/i; # common
264     return 1 if $file =~ /\(c\)\s+\d\d\d\d(?:-\d\d\d\d)?/i; # prototype
265     return 0;
266 }
267