This script allows much control over the orientation of joints.
You can specify the axis pointing down the joint, the up axis, and the direction into which the up axis should point. There's also an "Ik Mode" where the up direction is automatically the parent joint. This last option will orient the rotation axis of the joint (the preferred anlge) in such a way that the pole vector is consistent and predictable.
The script is separated into two files. One is just the user interface, the other is the actual working part that does the job. You can use this non-UI script for automated rigging. See the script's header for more information.