you can try maid (https://github.com/benjaminoakes/maid), or hazel on Mac, but I am not sure whether they can do this automatically. I think your question essentially is a set of boolean expressions and a last-chance rescue check, bash scripts should work