A second-order perturbation theory route to vibrational averages and transition properties of molecules: general formulation and application to infrared and vibrational circular dichroism spectroscopies