Please please please don't do so much directly in main(). It is much more readable to split things up a bit more logically.

For example, argument parsing should be in, say, parse_args().