# -*- perl -*-
##! @PERL@

use Getopt::Long;
GetOptions("top-srcdir:s");

$my_topdir = $opt_top_srcdir ? $opt_top_srcdir : "\$(top_srcdir)";

@ignorestuff = ();

sub ignore_query
{
    my ($testfile) = @_;
    return !grep(/$testfile/, @ignorestuff);
}

print "# Generated automatically by wild-perl 0.1\n";


while (<>) {
	$line = $_;
	if ($line =~ /^ *nowildcard: *(.*)$/) {
	    $ignore = $1;
	    @ignorelist = split (/ +/, $ignore);
	    foreach $i (@ignorelist) {
		print "# ignoring wildcards for \`$i\'\n";
		push @ignorestuff, $i; # concat ? uh?
	    }

	    
	} elsif ($line =~ /(.*)\$\(wildcard(.*)\)(.*)/) {
	    $wild = $2;
	    
	    @expanded = glob ($wild);

	    @expanded = grep {ignore_query($_);}  @expanded;
	    
	    print $1 . join (' ', @expanded) . $3 . "\n";
	} else {
	    print $line;
	}
}


print "\$(srcdir)/Makefile.am: \$(srcdir)/Makefile.am.wild\n" .
    "\t\$(PERL) $my_topdir/bin/wild-perl --top-srcdir=\"\$(real_topdir)\" < \$< > \$@";

