Woah, this is an extremely deeply nested if clause. I think it'd benefit immensely from being condensed a bit - conversion to "if [ ]; then" style, but also by chaining the various conditions using "-a" in a single [ ] set, or at least connecting multiple [ ] sets using " && ". No need for a full fledged if clause for everything.