#!/afs/athena/contrib/perl5/p -w
# -*- cperl -*-

use strict;
use File::Find;
use File::Basename;
use File::Path;

use vars qw($include $root %ignore);

$include = '/usr/include';
$root =    '/tmp/ph';
%ignore = ();

while (@ARGV) {
  my $arg = shift(@ARGV);
  my @argspl;

  ($arg eq '-include') && ($include = shift(@ARGV), next);
  ($arg eq '-root')    && ($root = shift(@ARGV),    next);
  ($arg eq '-ignore')  && (@argspl = split(/[,\s]/, shift(@ARGV)),
			   @ignore{@argspl} = (1) x @argspl, next);
  die "usage: $0 [-include dir] [-root dir] [-ignore subdir1[,subdir2...]]\n";
}

sub wanted {
  my ($alt) = $root . substr($File::Find::name, length($include));

  ($ignore{$_} || $ignore{$File::Find::name}) && ($File::Find::prune=1,return);
  (-d $File::Find::name) && (mkpath($alt, 1, 0755), return);
  ($alt =~ s/\.h$/.ph/) || (warn "Skipping $File::Find::name...\n", return);  

  print "Generating $alt...\n";
  system("h2ph < $File::Find::name > $alt");
}

find(\&wanted, $include);

