#!/usr/pkg/bin/perl
use strict;
use File::Find;
our @ARGV;

my @directories_to_search = @ARGV;
my $count;
my $PATCH;
my $pwd = `pwd`;
my $GENERATE_PATCH = 0;
chomp($pwd);

find(\&wanted, @directories_to_search);

#  generate filename as patch-aa, patch-ab ... from integer
sub patch_name($){
    my $num = shift;
    my ($i) = int($num/26);
    my ($j) = $num%26;
    return 'patch-'. chr(0x61+$i) . chr(0x61+$j) ;
    }

# subroutine for find, 
#	(1) find file name
#	(2) find the line to edit 
#	(3) if edit is necessary, generat (write) patch file to /tmp/patch-aa...
sub wanted { 
#   $File::Find::dir is the current directory name,
#   $_ is the current filename within that directory
#   $File::Find::name is the complete pathname to the file.
    my @lines = '';
    my $edit = 0;
    my $file = $File::Find::name;
    my $leaf = $_;
    my $in_install = 0;
    # configure  *.mk
    ##   -> texk/dviljk/Makefile.in  manual edi
    if ( 
	$leaf eq 'Makefile.in' ||
	$leaf =~ /\.mk$/ 
	) {
	open(MAKEFILE, $leaf) || print "problem to open file $leaf:$! \n";
	while(<MAKEFILE>) {

	    # distinguish if the line is within the range to be modified (install-xx: target) or not
	    if ( /^install[-a-z_]*:/ )    { $in_install = 1; push(@lines, $_); next; } # don't edit this line

	    # texlive_base-2007/utils/dialog/Makefile.in  special target -> $@
	    elsif ( /^\$\(bindir\)/ ||
		    /^\$\(man1dir\)/   )
	    		{ $in_install = 1; push(@lines, $_); next; } # just flag, don't edit this line

	    elsif ( /^[ \t]*$/ || 
		    /^[-a-z_]+:/   )      { $in_install = 0 } 

	    if ( $in_install && ! /^[ \t]+\#/ ) {
		if (    / (\$\(bindir\))/  )  {        s| (\$\(bindir\))| \${DESTDIR}$1|g;  $edit++}
		if (    / (\$\(gsftopkpsheader\))/){ s| (\$\(gsftopkpsheader\))| \${DESTDIR}$1|g;  $edit++}
		if (    / (\$\(gsftopkpsheaderdir\))/){ s| (\$\(gsftopkpsheaderdir\))| \${DESTDIR}$1|g;  $edit++}
		if (    / (\$\(psheaderdir\))/){  s| (\$\(psheaderdir\))| \${DESTDIR}$1|g;  $edit++}
		if (    / (\$\(includedir\))/){    s| (\$\(includedir\))| \${DESTDIR}$1|g;  $edit++}
		if (    / (\$\(libdir\))/  )  {        s| (\$\(libdir\))| \${DESTDIR}$1|g;  $edit++}
		if (    / (\$\(man1dir\))/ )  {       s| (\$\(man1dir\))| \${DESTDIR}$1|g;  $edit++}
		if (    / (\$\(man5dir\))/ )  {       s| (\$\(man5dir\))| \${DESTDIR}$1|g;  $edit++}
		if (    / (\$\(mandir\))/ )   {        s| (\$\(mandir\))| \${DESTDIR}$1|g;  $edit++}
		if (    / (\$\(mfpooldir\))/) {     s| (\$\(mfpooldir\))| \${DESTDIR}$1|g;  $edit++}
		if (    / (\$\(scriptdir\))/ ){     s| (\$\(scriptdir\))| \${DESTDIR}$1|g;  $edit++}
		if (    / (\$\(texmf\))/)     {         s|([= ])(\$\(texmf\))|$1\${DESTDIR}$2|g;  $edit++}
		if (    / (\$\(texpooldir\))/){    s| (\$\(texpooldir\))| \${DESTDIR}$1|g;  $edit++}
		if (    / (\$\(web2cdir\))/ ) {      s| (\$\(web2cdir\))| \${DESTDIR}$1|g;  $edit++}
		if (    / (\$\(fmtdir\))/)    {        s| (\$\(fmtdir\))| \${DESTDIR}$1|g;  $edit++}
		if (    / (\$\(afmtdir\))/)   {       s| (\$\(afmtdir\))| \${DESTDIR}$1|g;  $edit++}
		if (    / (\$\(efmtdir\))/)   {       s| (\$\(efmtdir\))| \${DESTDIR}$1|g;  $edit++}
		if (    / (\$\(ofmtdir\))/)   {       s| (\$\(ofmtdir\))| \${DESTDIR}$1|g;  $edit++}
		if (    / (\$\(pdfefmtdir\))/){    s| (\$\(pdfefmtdir\))| \${DESTDIR}$1|g;  $edit++}
		if (    / (\$\(xefmtdir\))/)  {      s| (\$\(xefmtdir\))| \${DESTDIR}$1|g;  $edit++}
		if ( /[= ](\$\(infodir\))/ )  {  s|([= ])(\$\(infodir\))|$1\${DESTDIR}$2|g;  $edit++}
		if (    / \$\@/            ) {
		    if ( ! /echo making/ && ! /\$\(MAKE\) \$\@/ ) 
		    { ##  ./Makefile special
			s| (\$\@)| \${DESTDIR}/$1|g; $edit++}}
	    }
	    push(@lines, $_);
	}
	close(MAKEFILE);
    }
    if ($edit) {
	print $file,"\n";
	my $cwd = `pwd`;
	if ($GENERATE_PATCH) {
	    rename $leaf, $leaf.".orig";
	}
	open(LEAF, "> $leaf") || print "problem opening to write $leaf: $! \n";
	print(LEAF @lines);
	close(LEAF);

	if ($GENERATE_PATCH) {
	$PATCH = '/tmp/'. patch_name($count++);
	  print "(cd    $pwd; echo '\$NetBSD\$'; echo ; diff -u $file.orig $file ) > $PATCH", "\n";
	system ("(cd    $pwd; echo '\$NetBSD\$'; echo ; diff -u $file.orig $file ) > $PATCH");
	}
    }
    return 1;
}

exit;
__END__
Two policies can be selected. For now, (2)
    to take (1) method, modify above to 
    my $GENERATE_PATCH = 1;
    (and texlive/Makefile to be changed also)
(1) generating patches
       cd pkgsrc/wip/texlive
  939  rm -f patches/patch-??
  940  echo init && make clean && make patch
  941  (cd    work/texlive_base-2007; pwd; perl ~/perl/add-destdir-texlive . )
  942  cp /tmp/patch-?? patches/
  944  make distinfo && make clean && make package

(2) not generating patches, do modify directly

       cd pkgsrc/wip/texlive
       make clean
       make package
            do automatically following command by make
            (cd    work/texlive_base-2007; pwd; perl ~/perl/add-destdir-texlive . )
