Bug 21395: Make perlcritic happy
[koha.git] / xt / fix-old-fsf-address
blobb0c7c001b673764be5785b4e1098f04c99132a4d
1 #!/usr/bin/perl
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:
7 # ./xt/find-license-problems . |
8 # grep -vFx -f ./xt/fix-old-fsf-address.exclude |
9 # ./xt/fix-old-fsf-address
11 # Copyright 2010 Catalyst IT Ltd
13 # This file is part of Koha.
15 # Koha is free software; you can redistribute it and/or modify it
16 # under the terms of the GNU General Public License as published by
17 # the Free Software Foundation; either version 3 of the License, or
18 # (at your option) any later version.
20 # Koha is distributed in the hope that it will be useful, but
21 # 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.
25 # You should have received a copy of the GNU General Public License
26 # along with Koha; if not, see <http://www.gnu.org/licenses>.
28 use Modern::Perl;
30 use File::Basename;
31 use File::Copy;
32 use File::Temp qw/ tempfile /;
35 my $temple = << 'eof';
36 You should have received a copy of the GNU General Public License along with
37 Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
38 Suite 330, Boston, MA 02111-1307 USA
39 eof
41 my $franklin = << 'eof';
42 You should have received a copy of the GNU General Public License along
43 with Koha; if not, write to the Free Software Foundation, Inc.,
44 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
45 eof
48 my $temple2 = << 'eof';
49 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,
50 Suite 330, Boston, MA 02111-1307 USA
51 eof
53 my $franklin2 = << 'eof';
54 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,
55 Fifth Floor, Boston, MA 02110-1301 USA.
56 eof
59 my $temple3 = << 'eof';
60 You should have received a copy of the GNU General Public License
61 along with this program; if not, write to the Free Software
62 Foundation, Inc., 50 Temple Place, Suite 330, Boston, MA 02111-1307 USA
63 eof
65 my $franklin3 = << 'eof';
66 You should have received a copy of the GNU General Public License
67 along with this program; if not, write to the Free Software Foundation, Inc.,
68 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
69 eof
72 my $temple4 = << 'eof';
73 You should have received a copy of the GNU General Public License
74 along with Zebra; see the file LICENSE.zebra. If not, write to the
75 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
76 02111-1307, USA.
77 eof
79 my $franklin4 = << 'eof';
80 You should have received a copy of the GNU General Public License
81 along with Zebra; see the file LICENSE.zebra. If not, write to the
82 Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
83 MA 02110-1301 USA.
84 eof
87 my @patterns = ($temple, $temple2, $temple3, $temple4);
88 my @replacements = ($franklin, $franklin2, $franklin3, $franklin4);
91 sub hashcomment {
92 my ($str) = @_;
93 my @lines = split /\n/, $str;
94 my @result;
95 foreach my $line (@lines) {
96 push @result, "# $line\n";
98 return join "", @result
102 sub dashcomment {
103 my ($str) = @_;
104 my @lines = split /\n/, $str;
105 my @result;
106 foreach my $line (@lines) {
107 push @result, "-- $line\n";
109 return join "", @result
113 sub readfile {
114 my ($filename) = @_;
115 open(my $fh, '<', $filename) || die("Can't open $filename for reading");
116 my @lines;
117 while (my $line = <$fh>) {
118 push @lines, $line;
120 close($fh);
121 return join '', @lines;
125 sub try_to_fix {
126 my ($data, @patterns) = @_;
127 return;
131 sub overwrite {
132 my ($filename, $data) = @_;
133 my ($fh, $tempname) = tempfile(DIR => dirname($filename));
134 print $fh $data;
135 close($fh);
136 copy($tempname, $filename);
137 unlink($tempname);
141 sub fix_temple_street {
142 my ($filename) = @_;
143 my $data = readfile($filename);
144 my @pats = map { ($_, hashcomment($_), dashcomment($_)) } @patterns;
145 my @repls = map { ($_, hashcomment($_), dashcomment($_)) } @replacements;
146 while (@pats) {
147 my $pat = shift @pats;
148 my $repl = shift @repls;
149 my $index = index($data, $pat);
150 next if $index == -1;
151 my $length = length($pat);
152 my $before = substr($data, 0, $index);
153 my $after = substr($data, $index + $length);
154 overwrite($filename, "$before$repl$after");
155 return;
157 die("Cannot find old address in $filename");
161 while (my $filename = <>) {
162 chomp $filename;
163 fix_temple_street($filename);