Extending automake

In KDE, currently, we have a custom perl script called am_edit which
does a bunch of voodoo to support some custom keywords that we use in
KDE-ized Makefile.am files.

I personally am on a vendetta against our existing crufty build system,
and as part of that I'm trying to figure out how to kill am_edit.

Now, one of the things[0] that our build system supports which I really
want to continue to support is the definition:

foo_METASOURCES = AUTO

Which automatically scans all of the source files and generates '.moc'
files for the ones that require it by running the moc command. It's
slightly more complicated than just running moc, as you need to run moc
against the header file and not the cpp file.

For example, foo.cpp may have the line '#include "foo.moc"' in it
somewhere. The command that needs to be run before foo.cpp gets compiled is:

moc -o foo.moc foo.h
How do I go about doing this The Right Way?
-clee