Merge branch 'master' of git.freeside.biz:/home/git/freeside
[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-2013 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-2013 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 File::Find::find({ no_chdir => 1, wanted => \&tag_mason}, 'share/html');
107 File::Find::find({ no_chdir => 1, wanted => \&tag_script}, 'sbin');
108 File::Find::find({ no_chdir => 1, wanted => \&tag_script}, 'bin');
109 File::Find::find({ no_chdir => 1, wanted => \&tag_script}, 'etc/upgrade');
110 File::Find::find({ no_chdir => 1, wanted => \&tag_script}, 'devel/tools');
111 tag_makefile ('Makefile.in');
112 tag_makefile ('README');
113
114
115 sub tag_mason {
116         my $pm = $_;
117         return unless (-f $pm);
118         return if $pm =~ /\.(?:png|jpe?g|gif)$/;
119         open( FILE, '<', $pm ) or die "Failed to open $pm";
120         my $file = (join "", <FILE>);
121         close (FILE);
122         print "$pm - ";
123         return if another_license($pm => $file) && print "has different license\n";
124
125         my $pmlic = $LICENSE;
126         $pmlic =~ s/^/%# /mg;
127         $pmlic =~ s/\s*$//mg;
128         if ($file =~ /^%# BEGIN BPS TAGGED BLOCK {{{/ms) {
129                 print "has license section";
130              $file =~ s/^%# BEGIN BPS TAGGED BLOCK {{{(.*?)%# END BPS TAGGED BLOCK }}}/%# BEGIN BPS TAGGED BLOCK {{{\n$pmlic\n%# END BPS TAGGED BLOCK }}}/ms;
131
132
133         } else {
134                 print "no license section";
135              $file ="%# BEGIN BPS TAGGED BLOCK {{{\n$pmlic\n%# END BPS TAGGED BLOCK }}}\n". $file;
136         }
137         $file =~ s/%# END BPS TAGGED BLOCK }}}(\n+)/%# END BPS TAGGED BLOCK }}}\n/mg;
138         print "\n";
139
140
141
142
143         open( FILE, '>', $pm ) or die "couldn't write new file";
144         print FILE $file;
145         close FILE;
146
147 }
148
149
150 sub tag_makefile {
151         my $pm = shift;
152         open( FILE, '<', $pm ) or die "Failed to open $pm";
153         my $file = (join "", <FILE>);
154         close (FILE);
155         print "$pm - ";
156         return if another_license($pm => $file) && print "has different license\n";
157
158         my $pmlic = $LICENSE;
159         $pmlic =~ s/^/# /mg;
160         $pmlic =~ s/\s*$//mg;
161         if ($file =~ /^# BEGIN BPS TAGGED BLOCK {{{/ms) {
162                 print "has license section";
163              $file =~ s/^# BEGIN BPS TAGGED BLOCK {{{(.*?)# END BPS TAGGED BLOCK }}}/# BEGIN BPS TAGGED BLOCK {{{\n$pmlic\n# END BPS TAGGED BLOCK }}}/ms;
164
165
166         } else {
167                 print "no license section";
168              $file ="# BEGIN BPS TAGGED BLOCK {{{\n$pmlic\n# END BPS TAGGED BLOCK }}}\n". $file;
169         }
170         $file =~ s/# END BPS TAGGED BLOCK }}}(\n+)/# END BPS TAGGED BLOCK }}}\n/mg;
171         print "\n";
172
173
174
175
176         open( FILE, '>', $pm ) or die "couldn't write new file";
177         print FILE $file;
178         close FILE;
179
180 }
181
182
183 sub tag_pm {
184         my $pm = $_;
185         next unless $pm =~ /\.pm/s;
186         open( FILE, '<', $pm ) or die "Failed to open $pm";
187         my $file = (join "", <FILE>);
188         close (FILE);
189         print "$pm - ";
190         return if another_license($pm => $file) && print "has different license\n";
191
192         my $pmlic = $LICENSE;
193         $pmlic =~ s/^/# /mg;
194         $pmlic =~ s/\s*$//mg;
195         if ($file =~ /^# BEGIN BPS TAGGED BLOCK {{{/ms) {
196                 print "has license section";
197              $file =~ s/^# BEGIN BPS TAGGED BLOCK {{{(.*?)# END BPS TAGGED BLOCK }}}/# BEGIN BPS TAGGED BLOCK {{{\n$pmlic\n# END BPS TAGGED BLOCK }}}/ms;
198
199
200         } else {
201                 print "no license section";
202              $file ="# BEGIN BPS TAGGED BLOCK {{{\n$pmlic\n# END BPS TAGGED BLOCK }}}\n". $file;
203         }
204         $file =~ s/# END BPS TAGGED BLOCK }}}(\n+)/# END BPS TAGGED BLOCK }}}\n\n/mg;
205         print "\n";
206
207
208
209
210         open( FILE, '>', $pm ) or die "couldn't write new file $pm";
211         print FILE $file;
212         close FILE;
213
214 }
215
216
217 sub tag_script {
218         my $pm = $_;
219         return unless (-f $pm);
220         open( FILE, '<', $pm ) or die "Failed to open $pm";
221         my $file = (join "", <FILE>);
222         close (FILE);
223         print "$pm - ";
224         return if another_license($pm => $file) && print "has different license\n";
225
226         my $pmlic = $LICENSE;
227         $pmlic =~ s/^/# /msg;
228         $pmlic =~ s/\s*$//mg;
229         if ($file =~ /^# BEGIN BPS TAGGED BLOCK {{{/ms) {
230                 print "has license section";
231              $file =~ s/^# BEGIN BPS TAGGED BLOCK {{{(.*?)# END BPS TAGGED BLOCK }}}/# BEGIN BPS TAGGED BLOCK {{{\n$pmlic\n# END BPS TAGGED BLOCK }}}/ms;
232
233
234         } else {
235                 print "no license section";
236                 if ($file =~ /^(#!.*?)\n/) {
237
238             my  $lic ="# BEGIN BPS TAGGED BLOCK {{{\n$pmlic\n# END BPS TAGGED BLOCK }}}\n";
239                 $file =~ s/^(#!.*?)\n/$1\n$lic/;
240
241                 }
242         }
243         $file =~ s/# END BPS TAGGED BLOCK }}}(\n+)/# END BPS TAGGED BLOCK }}}\n/mg;
244         print "\n";
245
246
247         open( FILE, '>', $pm ) or die "couldn't write new file";
248         print FILE $file;
249         close FILE;
250
251 }
252
253 sub another_license {
254     my $name = shift;
255     my $file = shift;
256
257     return 1 if ($name =~ /(?:ckeditor|scriptaculous|superfish|tablesorter|farbtastic)/i);
258
259     return 0 if $file =~ /Copyright\s+\(c\)\s+\d\d\d\d-\d\d\d\d Best Practical Solutions/i;
260     return 1 if $file =~ /\b(copyright|GPL|Public Domain)\b/i; # common
261     return 1 if $file =~ /\(c\)\s+\d\d\d\d(?:-\d\d\d\d)?/i; # prototype
262     return 0;
263 }
264