Here is a limited python script, you still need to identify the elements to be joined and branch properly.