Sorry everything would look nicer if latex formatting would be possible.
Let's consider three gears: G1, G2 and G3.
Now each gear has a given radius r1, r2 and r3.
The problem is to find the angular velocity for each gear (denoted av1, av2 and av3). From a physics course we know that the tangent velocity v is proportional to the radius: v1 = av1 * r1.
Now, if a given gear, say G1, is connectad to a second gear G2, then the tangent velocities match: v1=v2=v, where v is the overall velocity of the system, a global parameter for a system of connected gears.
Now it follows that av1 * r1 = v so av1 = v/r1 for each gear. That way you can find out the angular velocity of all gears in a system.
The next step is finding the rotation direction. For this, simply choose a gear (say G1), fix the rotation direction (say to +av1), look which gears are connected to it. If G2 is connected to it, its angular velocity is -av2.
There is still the problem of the clinch situation:
For small-scale problems I would advise to simply do this gear-wise for all gears: Then, if for any gear you want to set a different direction than already set - set all angular velocity values (or the system velocity) to 0 - the system is "jammed"!
You simply do this for each set of connected gears.
Its not a very sophisticated method to do this but I think it should work. :-)
One more comment: you can easily save the gear structure in the so-called "adjacency matrix". It is a matrix that is 1, if gear i is connected to gear j and 0 otherwise. As an example, take the 3x3 matrix consisting of only ones - where each of three gears is connected to the other - it represents a jammed system!
In your scripts, use the adjacency matrix to write the script that determines the rotation directions!