Added a bunch of arg and error checking.

This way we should be able to call this script from elsewhere and
actually get a reliable exit code.

Change-Id: I75ab6667fc72733616bd48600ace45ed1121cfcd