: # perl -w
# Magic to run perl on any unix platform (if perl is in the path).
eval 'exec perl -Sw $0 ${1+"$@"}'
        if 0;

use File::Basename;

use Cwd;
$pwd		= getcwd();

$DEBUG = "";

$commons = "$pwd/headers.lst";

if ($ARGV[0]) { 
  @mkcodelist = @ARGV ;
} 
else {
  die "No source file(s) given to modify\n";
}

# the list of headerfiles we want to change from exports/... to ../module/src.h
open (CM, "<$commons") || die "Can't open $commons ($!)\n";
while ( <CM> ) { chomp ($_); push (@hlist, $_); }
close (CM);

# Examine and change all .h and .c  files #include statements
chkSrc (\@mkcodelist, \@hlist);
#chkSrc (\@SRC, \@hlist);

sub chkSrc {
	my $aref = shift;
	my $href = shift;
	my $Q = '"';
	my $f = "";
	$filepath = "";
	$file = "";
	$searchstring = "";
	$replacestring = "";
	foreach $filepath (@$href) {
	  $file = basename($filepath);
	  $searchstring = "#\\s*include.*[<$Q]exports/$file.*[$Q>]|#\\s*include.*[<$Q]$file.*[$Q>]";
	  $replacestring = "#include $Q$filepath$Q";
	  push (@substitute, "s:$searchstring:$replacestring:o");
	} # for header in header list
	$subst = '$_ =~ ';
	$subst .= join (' || ', @substitute);

	foreach $f (@$aref) {
	  chomp($f);
	  print "$f\n" if ($DEBUG);
	  unless (-f $f) {$f = "$pwd/code/$f";}
	  if (-f $f) { open (S, "<$f") || die "Can't open $f ($!)\n"; }
	  @hackthis = <S>;
	  close (S);
	  for (@hackthis) {
	    next unless (/^#\s*include/);
	    #change all #include <export/...> to #include "export/..."
	    eval $subst ;
	    print "$_" if ($DEBUG);
	  } # for lines in file
	  open (H, ">$f") || die "Can't open $f ($!)\n";
	  print "Writing $f\n\n" if ($DEBUG);
	  print (H @hackthis); 
	  close (H);
	} # for file in src list
}
