Have a look at this approach:
I have tried this script myself (I think I needed some small fixes) and it works very well.