This is sort of an old post by now, but I went ahead and wrote a script that does what you're looking for. If you've already solved this, then at the very least I was able to get some good practice out of it.