...Here is something I concocted quickly without testing. It will almost certainly not work straight away and will need your tweaking, but it should at least give you the basic idea....