cleanup of the script.
divided script into functions.
made globals readonly.
mostly using locals now.
cleanup is more thorough.
still need to work on temporary name creation and the progress display.

this was an exercise in improving the bash script as described in
http://www.kfirlavi.com/blog/2012/11/14/defensive-bash-programming/