#!/usr/pkg/bin/perl
# -*- Perl -*-
# $NetBSD: subst-sheban,v 1.1 2013/10/30 14:16:19 makoto Exp $
# Swap shebang by find file.

use File::Find;
use Getopt::Std;
use strict;

our ($opt_d, $opt_p, $opt_h, $opt_P, $opt_r, $opt_t);

my $DELETE = 0;	# = 1 for delete .orig file, 0 to leave .orig
		# = 0 will leave files in destdir

# initialize conversion table
my %EXEC = qw(
	python python2.7
	lua5.0 lua
	ruby   ruby193
);
my $PREFIX;
my $PERL_PATH;

# See bottom for main()

sub usage() { 
    print STDERR <<HELP;
Synopsis:
    $0 [-d] [-h] [-P prefix] [-p python_exec] [-r ruby_exec] [-t texlua_exec]
  where:
    -d  (to delete *.orig)
    -h help
    -P PREFIX ( i.e. /usr/pkg etc)
    -p python executable name (python2.7)
    -r ruby executable name  (\$RUBY)
    -t texlua executable name
HELP
}
sub wanted () {
#    my $file = "$File::Find::dir/$_";  # fname not really used ?
    my $dir  = $File::Find::dir;
    my $file = $_;
    my $changed = 0;	# Flag for changed or not
    my @lines = ();
    if ( $file =~ /.orig$/) { return; }
    if ( $file =~ m|\./\.|) { return; }
    if ( $file =~ /ChangeLog/) { return; }

    # Read file by file
    if ( -d $file ) { next;}
    open(FILE, $file) || print "Can not open file: $file: $!\n";
    while(<FILE>) {
	if ( m|^\#!\s*/usr/bin/env (\S+)(.*)| ||
	     m|^\#!\s*/usr/bin/(\S+)(.*)|         ) {
	    my $exec = $1;
	    my $rest = $2;
	    if ( $EXEC{$exec}) { $exec =   $EXEC{$exec};}
	    push(@lines, "#!$PREFIX/bin/$exec$rest\n");
	    $changed++;
# Taking care installed stuff in DESTDIR (for second invocation)
	} elsif #( m|^\#!\s*$PREFIX/bin/(ruby\S*)(.*)|      ||
	        #  m|^\#!\s*$PREFIX/bin/(ruby\S*)|      	   ||
	        ( m|^\#!\s*$PREFIX/bin/(texlua)(.*)| ) {
	    my $exec = $1;
	    my $rest = $2;
	    if ( $EXEC{$exec}) { $exec =   $EXEC{$exec};}	    
	    push(@lines, "#!$PREFIX/bin/$exec$rest\n");
	    $changed++;
	} elsif ( m|/usr/bin/env perl| ) {
	    s,/usr/bin/env perl,$PERL_PATH,;
	    push(@lines, $_);
	    $changed++;
	} elsif ( m|/usr/bin/perl| ) {
	    s,/usr/bin/perl,$PERL_PATH,;
	    push(@lines, $_);
	    $changed++;
	} else {
	    push (@lines, $_);
	}
    }
    close(FILE);
   # Write back if the change is necessary.
    if ($changed) {
	# my $pwd = `pwd`;
	my $orig = $file.".orig";
	my ($mode) = (stat($file))[2];
	rename $file, $orig;
	open(FILE,"> $file") || print "Can not write to file: $file: $!\n!";
	print FILE @lines;
	close(FILE);
	my ($omode) = (stat($file))[2];
	if ($omode ne $mode ) {			# some configure loose x bit without this check
	    chmod $mode, $file;
	    printf STDERR "%6o -> %6o ", $omode, $mode ;
	} else {
	    printf STDERR "%6s    %6s ", '','';	# column adjust
	}
	unlink $orig if $DELETE;
	print STDERR "$file -> $orig\n";
    }  
}

# Main Routine 

getopts('dhP:p:r:t:'); 
if ( $opt_d ) { $DELETE = 1 ;}
if ( $opt_h ) { usage(); exit}
if ( $opt_P ) { $PREFIX		= $opt_P;}
if ( $opt_p ) { $EXEC{'python'}	= $opt_p; }
if ( $opt_r ) { $EXEC{'ruby'}	= $opt_r; }
if ( $opt_t ) { 
    $EXEC{'texlua'}	= $opt_t; 
    print STDERR $opt_t, "\n";
}

$PERL_PATH=     $PREFIX.'/bin/perl';


find (\&wanted, '.');
exit;

