A theory of semigeostrophic gravity waves and its application to the intrusion of a density current along a coast