I did manage to throw some code together that I think works.
Might this plugin do what you need ?
I have looked and am looking at places this touches.