Bug 26265: (QA follow-up) Remove g option from regex, add few dirs
[koha-equinox.git] / xt / fix-old-fsf-address
1 #!/usr/bin/perl
2 #
3 # Fix GPLv2 license blurbs that have the old FSF address at Temple Street,
4 # instead of the Franklin Street one. Files to be fixed are read from
5 # stdin. Typical usage would be:
6 #
7 #   ./xt/find-license-problems . | 
8 #   grep -vFx -f ./xt/fix-old-fsf-address.exclude | 
9 #   ./xt/fix-old-fsf-address
10 #
11 # Copyright 2010 Catalyst IT Ltd
12 #
13 # This file is part of Koha.
14 #
15 # This program is free software; you can redistribute it and/or modify
16 # it under the terms of the GNU General Public License as published by
17 # the Free Software Foundation; either version 2 of the License, or
18 # (at your option) any later version.
19 #
20 # This program is distributed in the hope that it will be useful,
21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 # GNU General Public License for more details.
24 #
25 # You should have received a copy of the GNU General Public License along
26 # with this program; if not, write to the Free Software Foundation, Inc.,
27 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28
29
30 use Modern::Perl;
31
32 use File::Basename;
33 use File::Copy;
34 use File::Temp qw/ tempfile /;
35
36
37 my $temple = << 'eof';
38 You should have received a copy of the GNU General Public License along with
39 Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
40 Suite 330, Boston, MA  02111-1307 USA
41 eof
42
43 my $franklin = << 'eof';
44 You should have received a copy of the GNU General Public License along
45 with Koha; if not, write to the Free Software Foundation, Inc.,
46 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
47 eof
48
49
50 my $temple2 = << 'eof';
51 You should have received a copy of the GNU General Public License along with Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
52 Suite 330, Boston, MA  02111-1307 USA
53 eof
54
55 my $franklin2 = << 'eof';
56 You should have received a copy of the GNU General Public License along with Koha; if not, write to the Free Software Foundation, Inc., 51 Franklin Street,
57 Fifth Floor, Boston, MA 02110-1301 USA.
58 eof
59
60
61 my $temple3 = << 'eof';
62 You should have received a copy of the GNU General Public License
63 along with this program; if not, write to the Free Software
64 Foundation, Inc., 50 Temple Place, Suite 330, Boston, MA 02111-1307  USA
65 eof
66
67 my $franklin3 = << 'eof';
68 You should have received a copy of the GNU General Public License
69 along with this program; if not, write to the Free Software Foundation, Inc., 
70 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
71 eof
72
73
74 my $temple4 = << 'eof';
75 You should have received a copy of the GNU General Public License
76 along with Zebra; see the file LICENSE.zebra.  If not, write to the
77 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
78 02111-1307, USA.
79 eof
80
81 my $franklin4 = << 'eof';
82 You should have received a copy of the GNU General Public License
83 along with Zebra; see the file LICENSE.zebra.  If not, write to the
84 Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, 
85 MA 02110-1301 USA.
86 eof
87
88
89 my @patterns = ($temple, $temple2, $temple3, $temple4);
90 my @replacements = ($franklin, $franklin2, $franklin3, $franklin4);
91
92
93 sub hashcomment {
94     my ($str) = @_;
95     my @lines = split /\n/, $str;
96     my @result;
97     foreach my $line (@lines) {
98         push @result, "# $line\n";
99     }
100     return join "", @result
101 }
102
103
104 sub dashcomment {
105     my ($str) = @_;
106     my @lines = split /\n/, $str;
107     my @result;
108     foreach my $line (@lines) {
109         push @result, "-- $line\n";
110     }
111     return join "", @result
112 }
113
114
115 sub readfile {
116     my ($filename) = @_;
117     open(FILE, $filename) || die("Can't open $filename for reading");
118     my @lines;
119     while (my $line = <FILE>) {
120         push @lines, $line;
121     }
122     close(FILE);
123     return join '', @lines;
124 }
125
126
127 sub try_to_fix {
128     my ($data, @patterns) = @_;
129     return undef;
130 }
131
132
133 sub overwrite {
134     my ($filename, $data) = @_;
135     my ($fh, $tempname) = tempfile(DIR => dirname($filename));
136     print $fh $data;
137     close($fh);
138     copy($tempname, $filename);
139     unlink($tempname);
140 }
141
142
143 sub fix_temple_street {
144     my ($filename) = @_;
145     my $data = readfile($filename);
146     my @pats = map { ($_, hashcomment($_), dashcomment($_)) } @patterns; 
147     my @repls = map { ($_, hashcomment($_), dashcomment($_)) } @replacements;
148     while (@pats) {
149         my $pat = shift @pats;
150         my $repl = shift @repls;
151         my $index = index($data, $pat);
152         next if $index == -1;
153         my $length = length($pat);
154         my $before = substr($data, 0, $index);
155         my $after = substr($data, $index + $length);
156         overwrite($filename, "$before$repl$after");
157         return;
158     }
159     die("Cannot find old address in $filename");
160 }
161
162
163 while (my $filename = <>) {
164     chomp $filename;
165     fix_temple_street($filename);
166 }