import of rt 3.0.4
[freeside.git] / rt / sbin / license_tag
1 #!/usr/bin/perl
2
3
4 # BEGIN LICENSE BLOCK
5
6 # Copyright (c) 1996-2003 Jesse Vincent <jesse@bestpractical.com>
7
8 # (Except where explictly superceded by other copyright notices)
9
10 # This work is made available to you under the terms of Version 2 of
11 # the GNU General Public License. A copy of that license should have
12 # been provided with this software, but in any event can be snarfed
13 # from www.gnu.org.
14
15 # This work is distributed in the hope that it will be useful, but
16 # WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 # General Public License for more details.
19
20 # Unless otherwise specified, all modifications, corrections or
21 # extensions to this work which alter its source code become the
22 # property of Best Practical Solutions, LLC when submitted for
23 # inclusion in the work.
24
25
26 # END LICENSE BLOCK
27
28 my $LICENSE  = <<EOL;
29
30 Copyright (c) 1996-2003 Jesse Vincent <jesse\@bestpractical.com>
31
32 (Except where explictly superceded by other copyright notices)
33
34 This work is made available to you under the terms of Version 2 of
35 the GNU General Public License. A copy of that license should have
36 been provided with this software, but in any event can be snarfed
37 from www.gnu.org.
38
39 This work is distributed in the hope that it will be useful, but
40 WITHOUT ANY WARRANTY; without even the implied warranty of
41 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
42 General Public License for more details.
43
44 Unless otherwise specified, all modifications, corrections or
45 extensions to this work which alter its source code become the
46 property of Best Practical Solutions, LLC when submitted for
47 inclusion in the work.
48
49
50 EOL
51
52 use File::Find;
53
54 my @MAKE = qw(Makefile);
55
56 File::Find::find({ no_chdir => 1, wanted => \&tag_pm}, 'lib');
57 File::Find::find({ no_chdir => 1, wanted => \&tag_mason}, 'html');
58 File::Find::find({ no_chdir => 1, wanted => \&tag_script}, 'sbin');
59 File::Find::find({ no_chdir => 1, wanted => \&tag_script}, 'bin');
60 tag_makefile ('Makefile');
61 tag_makefile ('README');
62
63
64 sub tag_mason {
65         my $pm = $_;
66         next unless (-f $pm);
67         next if ($pm =~ /images/);
68         open(FILE,"<$pm") || die "Failed to open $pm";
69         my $file = (join "", <FILE>); 
70         close (FILE);
71         my $pmlic = $LICENSE;
72         $pmlic =~ s/^/%# /mg;
73
74
75         print "$pm - ";
76         if ($file =~ /^%# BEGIN LICENSE BLOCK/ms) {
77                 print "has license section";
78              $file =~ s/^%# BEGIN LICENSE BLOCK(.*?)%# END LICENSE BLOCK/%# BEGIN LICENSE BLOCK\n$pmlic%# END LICENSE BLOCK/ms;
79              
80
81         } else {
82                 print "no license section";
83              $file ="%# BEGIN LICENSE BLOCK\n$pmlic%# END LICENSE BLOCK\n". $file;
84         }
85         $file =~ s/%# END LICENSE BLOCK(\n+)/%# END LICENSE BLOCK\n/mg;
86         print "\n";
87         
88         
89
90
91         open (FILE, ">$pm") || die "couldn't write new file";
92         print FILE $file;
93         close FILE;
94
95 }
96
97
98 sub tag_makefile {
99         my $pm = shift;
100         open(FILE,"<$pm") || die "Failed to open $pm";
101         my $file = (join "", <FILE>); 
102         close (FILE);
103         my $pmlic = $LICENSE;
104         $pmlic =~ s/^/# /mg;
105
106
107         print "$pm - ";
108         if ($file =~ /^# BEGIN LICENSE BLOCK/ms) {
109                 print "has license section";
110              $file =~ s/^# BEGIN LICENSE BLOCK(.*?)# END LICENSE BLOCK/# BEGIN LICENSE BLOCK\n$pmlic# END LICENSE BLOCK/ms;
111              
112
113         } else {
114                 print "no license section";
115              $file ="# BEGIN LICENSE BLOCK\n$pmlic# END LICENSE BLOCK\n". $file;
116         }
117         $file =~ s/# END LICENSE BLOCK(\n+)/# END LICENSE BLOCK\n/mg;
118         print "\n";
119         
120         
121
122
123         open (FILE, ">$pm") || die "couldn't write new file";
124         print FILE $file;
125         close FILE;
126
127 }
128
129
130 sub tag_pm {
131         my $pm = $_;
132         next unless $pm =~ /\.pm\z/s;
133         open(FILE,"<$pm") || die "Failed to open $pm";
134         my $file = (join "", <FILE>); 
135         close (FILE);
136         my $pmlic = $LICENSE;
137         $pmlic =~ s/^/# /mg;
138
139
140         print "$pm - ";
141         if ($file =~ /^# BEGIN LICENSE BLOCK/ms) {
142                 print "has license section";
143              $file =~ s/^# BEGIN LICENSE BLOCK(.*?)# END LICENSE BLOCK/# BEGIN LICENSE BLOCK\n$pmlic# END LICENSE BLOCK/ms;
144              
145
146         } else {
147                 print "no license section";
148              $file ="# BEGIN LICENSE BLOCK\n$pmlic# END LICENSE BLOCK\n". $file;
149         }
150         $file =~ s/# END LICENSE BLOCK(\n+)/# END LICENSE BLOCK\n/mg;
151         print "\n";
152         
153         
154
155
156         open (FILE, ">$pm") || die "couldn't write new file $pm";
157         print FILE $file;
158         close FILE;
159
160 }
161
162
163 sub tag_script {
164         my $pm = $_;
165         return unless (-f $pm);
166         open(FILE,"<$pm") || die "Failed to open $pm";
167         my $file = (join "", <FILE>); 
168         close (FILE);
169         my $pmlic = $LICENSE;
170         $pmlic =~ s/^/# /msg;
171
172         print "$pm - ";
173         if ($file =~ /^# BEGIN LICENSE BLOCK/ms) {
174                 print "has license section";
175              $file =~ s/^# BEGIN LICENSE BLOCK(.*?)# END LICENSE BLOCK/# BEGIN LICENSE BLOCK\n$pmlic# END LICENSE BLOCK/ms;
176              
177
178         } else {
179                 print "no license section";
180                 if ($file =~ /^(#!.*?)\n/) {
181
182             my  $lic ="# BEGIN LICENSE BLOCK\n$pmlic# END LICENSE BLOCK\n";
183                 $file =~ s/^(#!.*?)\n/$1\n$lic/; 
184
185                 } 
186         }
187         $file =~ s/# END LICENSE BLOCK(\n+)/# END LICENSE BLOCK\n\n/mg;
188         print "\n";
189         
190
191         open (FILE, ">$pm") || die "couldn't write new file";
192         print FILE $file;
193         close FILE;
194
195 }
196