Hi @jmb
This will require some very custom code which is a bit beyond the scope of this community forum.
Perhaps you should think about the task and break it down into individual tasks that you can understand step by step.
Then work on these smaller tasks one by one.
Cheers
E