Make script more modular

While the building system is not ready for #4692 , we can start moving the code already.
When #4692 gets reopened other PRs touching script can be rebased on top of this one and decrease the chances of them being breaking/getting broke by 4692.
Can you follow the alphabetical ordering please? I'm starting to get frustrated... I'm not allowed to cleanup and quite often pulls get in that don't even try to match that.