Where to start?

In the initial state you wait for the outer detector for any of the lines to be triggered. When you detect a trigger, set that the signal for that line green, wait for the train to trigger the inner detector and then set the signal red again.

For flexibility I'd implement this junction as a state machine (the state variable would indicate which line (if any) is currently green), and I'd use a non-blocking state machine to 'wait' for the detection triggers rather than blocking until the expected event arrived; this would make it possible for the same processor to control multiple junctions or carry out other activities at the same time. But for a crude proof of concept, a blocking approach would probably only take a dozen lines of code.