Bug 14298: Update database
[koha.git] / fix-perl-path.PL
blobc36381d7838c1e6467145d5bdfbe9ccc0d5c200a
1 #!/usr/bin/perl
2 # This file is part of Koha.
4 # Koha is free software; you can redistribute it and/or modify it
5 # under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
9 # Koha is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with Koha; if not, see <http://www.gnu.org/licenses>.
18 use strict;
19 use ExtUtils::MakeMaker::Config;
20 use Tie::File;
22 my $basedir = (shift);
23 my $DEBUG = exists $ENV{'DEBUG'} ? $ENV{'DEBUG'} : 0;
25 $DEBUG = 1 if $basedir eq 'test';
27 my $bindir = $Config{installbin};
28 $bindir =~ s!\\!/!g; # make all directory separators uniform since Win32 does not care and *nix does...
29 my $shebang = "#!$bindir\/perl";
31 warn "Perl binary located in $bindir on this system.\n" if $DEBUG;
32 warn "The shebang line for this sytems should be $shebang\n\n" if $DEBUG;
34 die if $basedir eq 'test';
36 =head1 NAME
38 fix-perl-path.PL - A script to correct the shebang line to match the current platform
40 =head1 SYNOPSIS
42 =head2 BASIC USAGE
44 perl fix-perl-path.PL /absolute/path/to/foo
46 =head1 DESCRIPTION
48 This script should be run from the base of the directory
49 structure which contains the file(s) that need the
50 shebang line corrected. It will recurse through all
51 directories below the one called from and modify all
52 .pl files.
54 =head2 fixshebang
56 This sub will recurse through a given directory and its subdirectories checking for the existence of a shebang
57 line in .pl files and replacing it with the correct line for the current OS if needed. It should be called
58 in a manner similar to 'fixshebang (foodir)' but may be supplied with any directory.
60 =cut
62 sub fixshebang{
63 my $dir = shift;
64 opendir my $dh, $dir or die $!;
65 warn "Reading $dir contents.\n" if $DEBUG;
66 while( my $file = readdir($dh) ) {
67 # this may be used to exclude any desired files from the scan
68 # if ( $file =~ /foo/ ) { next; }
69 # handle files... other extensions could be substituted/added if needed
70 if ( $file =~ /\.pl$/ ) {
71 my @filearray;
72 my $pathfile =$dir . '/' . $file;
73 warn "Found a perl script named $pathfile\n" if $DEBUG;
75 # At this point, file is in 'blib' and by default
76 # has mode a-w. Therefore, must change permission
77 # to make it writable. Note that stat and chmod
78 # (the Perl functions) should work on Win32
79 my $old_perm;
80 $old_perm = (stat $pathfile)[2] & 07777;
81 my $new_perm = $old_perm | 0200;
82 chmod $new_perm, $pathfile;
84 # tie the file -- note that we're explicitly setting the line (record)
85 # separator to hex 0A (the Unix newline) because that's what
86 # the files copied to blib are using, regardless of whether the install
87 # is under a Unix variant or Windows.
88 tie @filearray, 'Tie::File', $pathfile, recsep => "\x0a" or die $!;
90 warn "First line of $file is $filearray[0]\n\n" if $DEBUG;
91 if ( ( $filearray[0] =~ /#!.*perl/ ) && ( $filearray[0] !~ /$shebang|"$shebang -w"/ ) ) {
92 warn "\n\tRe-writing shebang line for $pathfile\n" if $DEBUG;
93 warn "\tOriginal shebang line: $filearray[0]\n" if $DEBUG;
94 $filearray[0] =~ /-w$/ ? $filearray[0] = "$shebang -w" : $filearray[0] = $shebang;
95 warn "\tNew shebang line is: $filearray[0]\n\n" if $DEBUG;
97 elsif ( $filearray[0] =~ /$shebang|"$shebang -w"/ ) {
98 warn "\n\tShebang line is correct.\n\n" if $DEBUG;
100 else {
101 warn "\n\tNo shebang line found in $pathfile\n\n" if $DEBUG;
103 untie @filearray;
104 chmod $old_perm, $pathfile;
106 # handle directories
107 elsif ( -d ($dir . '/' . $file) && $file !~ /^\.{1,2}/ ) {
108 my $dirpath = $dir . '/' . $file;
109 warn "Found a subdir named $dirpath\n" if $DEBUG;
110 fixshebang ($dirpath);
113 closedir $dh;
116 fixshebang ($basedir);