Detumbling and reorienting underactuated rigid spacecraft