#!/usr/bin/env perl

=head1 SYNOPSIS

Munge invocation line for all perl scripts in a directories. Default to 
I<grandparent> dir of this script. --force to bypass path check.

 $0 [directories..] --perl /path/of/perl [--force]

=cut
use strict;
use warnings;

use FindBin qw( $Bin );
use File::Basename;
use Getopt::Long;
use Pod::Usage;
use Tie::File;

my ( %o, @file );
Pod::Usage::pod2usage( -input => $0, -output => \*STDERR, -verbose => 2 )
    unless Getopt::Long::GetOptions( \%o, qw( perl=s force ) ) && $o{perl};

my $perl = -l $o{perl} ? readlink $o{perl} : $o{perl};
exit 1 unless $o{force} || $perl =~ /perl/ && -f $perl && -x _;
push @ARGV, dirname( $Bin ) unless @ARGV;

for my $file ( `find @ARGV -type f` )
{
    chomp $file;
    tie @file, 'Tie::File', $file;

    next unless @file && $file[0] =~ /#!.*perl(.*$)/o;
    $file[0] = "#!$o{perl}$1";
    warn "$file\n";
    untie @file;
}
exit 0;
