Fix for IOS-83 Distribution build script phase is buggy and runs even when not necessary

Moving the build script into its own file.
Merging change from @kgn's branch.