bash style standardization

Before adding any more features to the scripts, standardize the bash
style.  I prefer something close to the google shell style guidelines:

  http://google-styleguide.googlecode.com/svn/trunk/shell.xml

- change [[ to [ (more robust)
- put variable references in quotes (more robust)
- put "then" on same line as "if" (more C-like, readable)
- print error messages on stdout

Also a few error handling improvements:

- set errexit, nounset to catch errors before they multiply and give
  reasonable error messages to the user where possible
- use die function where appropriate