Update build shell scripts to detect environment instead of relying on jenkins for setting it up

also perform general script cleanup.
These changes enable running the scripts on developer machines without any environment setup ahead of time.