A condensate in a lossy cavity: Collective atomic recoil and generation of entanglement