You have only 2 options to retreat:
A) through an EZOC in the direction of friendly LOC
B) through a friendly occupied hex away from friendly LOC (that could subsequently, if routing, allow you to swerve towards friendly LOC
Which take precedence?
Per rule [9.7.6]: "If no other route is available, a Retreating unit may move through friendly-occupied hexes."
(color highlight mine, for emphasis)
The key point here is that retreating through friendly occupied hexes is always the last possible alternative for a retreat; essentially there has to be no other possible direction for the unit to go.
I understand it is optional to attack out of a fort (and that if you choose to you must attack all adjacent enemy units), but what about attacking into one? I presume it should be mandatory since as an attacker you are in the fort's EZOC.
Correct. Units inside a fort/redoubt can choose to ignore adjacent enemy units (their ZOCs don't extend into the fort/redoubt). However, the reverse is not true: the ZOC of the unit inside the fort/redoubt still extends out of the hex, which in turn compels the units outside to attack the fort/redoubt's defender.
Hope that clears up the confusion.
Developer for VPG's "Napoleonic 20" series