A variational principle and the classical and quantum mechanics of the damped harmonic oscillator