One relay and two diodes take care of every condition except the one special case I mentioned earlier. The diodes maintain the isolation between the two circuits.
I'm drawing two versions of the circuit to see which I like better. One is analog and based on a voltage comparator. The other is digital with a < $1 microcontroller.