1 # $Id: insertperl.pl.root 449 2005-05-31 19:07:04Z nodine $
7 use File
::Basename
qw(basename dirname fileparse);
11 use Text
::Restructured
::PrestConfig
;
13 use vars
qw($VERSION @ISA
14 $Is_Mac $Is_OS2 $Is_VMS $Is_Win32 $Is_Dos $Is_VOS
15 $Verbose %pm %static $Xsubpp_Version
19 my $taintflag = $Text::Restructured::PrestConfig::TAINT =~ /^y/i ? ' -T' : '';
20 my $perl_exec = "$^X$taintflag";
22 my($does_shbang) = $Config{'sharpbang'} =~ /^\s*\#\!/;
23 foreach my $file (@ARGV) {
26 open FIXIN, $file or croak "Can't read '$file': $1";
28 chomp (my $line = <FIXIN>);
29 next unless $line =~ s/^\s*\#!\s*//; # Not a shbang file.
30 # Now figure out the interpreter name.
31 my($cmd,$arg) = split ' ', $line, 2;
35 $interpreter = $perl_exec;
38 my(@absdirs) = reverse grep {File::Spec->file_name_is_absolute} File::Spec->path;
41 foreach $dir (@absdirs) {
42 if (maybe_command($cmd)) {
43 warn "Ignoring $interpreter in $file\n" if $Verbose && $interpreter;
44 $interpreter = File::Spec->catfile($dir,$cmd);
48 # Figure out how to invoke interpreter on this machine.
52 print STDOUT "Changing sharpbang in $file to $interpreter" if $Verbose;
53 # this is probably value-free on DOSISH platforms
55 $shb .= "$Config{'sharpbang'}$interpreter";
56 $shb .= ' ' . $arg if defined $arg;
60 eval 'exec $interpreter $arg -S \$0 \${1+"\$\@"}'
61 if 0; # not running under some shell
62 } unless $Is_Win32; # this won't work on win32, so don't
64 warn "Can't find $cmd in PATH, $file unchanged"
69 unless ( open(FIXOUT,">$file.new") ) {
70 warn "Can't create new $file: $!\n";
73 my($dev,$ino,$mode) = stat FIXIN;
75 # Print out the new #! line (or equivalent).
78 print FIXOUT $shb, <FIXIN>;
82 unless ( rename($file, "$file.bak") ) {
83 warn "Can't rename $file to $file.bak: $!";
86 unless ( rename("$file.new", $file) ) {
87 warn "Can't rename $file.new to $file: $!";
88 unless ( rename("$file.bak", $file) ) {
89 warn "Can't rename $file.bak back to $file either: $!";
90 warn "Leaving $file renamed as $file.bak\n";
96 close(FIXIN) if fileno(FIXIN);
97 system("$Config{'eunicefix'} $file") if $Config{'eunicefix'} ne ':';;
102 return $file if -x $file && ! -d $file;