No real help here, just some general comments. First, don't cd in a script.
Use full paths - this includes defining paths to your programs (if you or
someone else put a different program with the same name that is found
earlier in your path, your script is liable to cause a big mess). Bash is
nice and real powerful. However, when my bash scripts grow beyond a few
lines, I evaluate my my sanity and use something more along the lines of
programming (perl is my language of choice but some like python or ruby).
BTW, my reasoning behind using something else when scripts get too big is:
1. Style - shell scripts look ugly as hell, 2. Speed - running all of those
individual programs isn't very fast, 3. Functionality - I can do ten times
more in three lines of perl than you can do in ten lines of bash. Etc, etc,
etc.