here is a script you can try.
Feel free to try, debug, modify, criticize, adapt, improve, etc. regards Christophe Combelles