blob: 1cd2198d15cfa00f1eb613a4eebe6ef1a1561114 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
#!/usr/bin/env perl
# re! -- rewrite shebangs
use strict;
my $test = 0;
sub usage { die "usage: $0 [-n] file ...\n" }
if (@ARGV and $ARGV[0] eq '-n') {
$test = 1;
shift @ARGV;
}
usage if not @ARGV;
for my $file (@ARGV) {
open my $o, '<', $file or die "could not open $file: $!\n";
# parse shebang
my $shebang = <$o>;
if (not $shebang =~ /^#!/) {
warn "no shebang: $file\n";
close $o;
next;
}
$shebang =~ /^#!\s*(\S+)\s*(.*)/;
my ($old, $args) = ($1, $2);
# validate path
next if -x $old;
# get new path
(my $basename = $old) =~ s,.*/,,;
chomp(my $new = `which $basename`);
$new or die "could not find $basename\n";
next if $old eq $new;
# print results if test
if ($test) {
print "$file: $old -> $new\n";
next;
}
# write new shebang
open my $n, '>', "$file.tmp" or die "could not open $file.tmp: $!\n";
print $n "#!$new $args\n";
print $n $_ while <$o>;
close for ($o, $n);
system('mv', "$file.tmp", $file) == 0
or die "could not overwrite $file\n";
}
|