A variational theory for light rays in Lorentzian manifolds