~kosova/+junk/tuxfamily-twiki

« back to all changes in this revision

Viewing changes to foswiki/tools/rewriteshbang.pl

  • Committer: James Michael DuPont
  • Date: 2009-07-18 19:58:49 UTC
  • Revision ID: jamesmikedupont@gmail.com-20090718195849-vgbmaht2ys791uo2
added foswiki

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! perl -w
 
2
#
 
3
# Foswiki - The Free and Open Source Wiki, http://foswiki.org/
 
4
#
 
5
# Copyright (C) 2005-2007 Foswiki Contributors.
 
6
# All Rights Reserved. Foswiki Contributors are listed in
 
7
# the AUTHORS file in the root of this distribution.
 
8
# NOTE: Please extend that file, not this notice.
 
9
#
 
10
# For licensing info read license.txt file in the TWiki root.
 
11
# This program is free software; you can redistribute it and/or
 
12
# modify it under the terms of the GNU General Public License
 
13
# as published by the Free Software Foundation; either version 2
 
14
# of the License, or (at your option) any later version.
 
15
#
 
16
# This program is distributed in the hope that it will be useful,
 
17
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
18
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
19
# GNU General Public License for more details, published at 
 
20
# http://www.gnu.org/copyleft/gpl.html
 
21
#
 
22
# HELP
 
23
print <<'END';
 
24
Change the "shebang" lines of all perl scripts found in the current
 
25
directory.
 
26
 
 
27
"shebang" lines tell the shell what interpreter to use for running
 
28
scripts. By default the TWiki bin scripts are set to user the
 
29
"/usr/bin/perl" interpreter, which is where perl lives on most
 
30
UNIX-like platforms. On some platforms you will need to change this line
 
31
to run a different interpreter e.g. "D:\indigoperl\bin\perl"
 
32
or "/usr/bin/speedy"
 
33
 
 
34
This script will change the "shebang" lines of all scripts found in
 
35
the directory where the script is run from.
 
36
 
 
37
Note: the path to the interpreter *must not* contain any spaces.
 
38
END
 
39
 
 
40
use strict;
 
41
 
 
42
my $new = 'perl';
 
43
$/ = "\n";
 
44
 
 
45
while (1) {
 
46
    print "Enter path to interpreter [hit enter to choose '$new']: ";
 
47
    my $n = <>;
 
48
    chomp $n;
 
49
    last if( !$n );
 
50
    $new = $n;
 
51
};
 
52
 
 
53
unless( -x $new ) {
 
54
    print "Warning: I could not find an executable at $new
 
55
Are you sure you want to use this path (y/n)? ";
 
56
    my $n = <>;
 
57
    die "Aborted" unless $n =~ /^y/i;
 
58
}
 
59
 
 
60
my $changed = 0;
 
61
my $scanned = 0;
 
62
opendir(D, ".") || die $!;
 
63
foreach my $file (grep { -f && /^\w+$/ } readdir D) {
 
64
    $scanned++;
 
65
    $/ = undef;
 
66
    open(F, "<$file") || die $!;
 
67
    my $contents = <F>;
 
68
    close F;
 
69
 
 
70
    if( $contents =~ s/^#!\s*\S+/#!$new/s ) {
 
71
        my $mode = (stat($file))[2];
 
72
        chmod( oct(600), "$file");
 
73
        open(F, ">$file") || die $!;
 
74
        print F $contents;
 
75
        close F;
 
76
        chmod( $mode, "$file");
 
77
        print "$file modified\n";
 
78
        $changed++;
 
79
    } else {
 
80
        print "$file modified\n";
 
81
    }
 
82
}
 
83
closedir(D);
 
84
print "$changed of $scanned files changed\n";