Making automatic railway switches

I’d like to make automatic switches. Meaning, if a train passes over a switch set to the wrong direction, it will correct itself automatically.

This is the script for manual switching. This is taken from NWSpaceK’s Minitrainz track kit. It goes in the switch’s model.

local state = false

function act()
	state = not state
	for _,j in ipairs (script.Parent:GetChildren()) do
		if j.Name == "Normal" then
			j.CanCollide = not state
			j.Transparency = state and 1 or 0
		elseif j.Name == "Reverse" then
			j.CanCollide = state
			j.Transparency = state and 0 or 1
		end
	end
end

script.Parent.Show.Click.MouseClick:connect(act)

I have a lot of the railway already built, and I don’t have the time to replace every single switch with an updated version of itself. Would it also be possible for automatic switching to be done without adding/modifying any parts?

(If it helps at all, train gliders are all named “Glider”.)