How to add a package but only if it is not already there?

This is more or less what I ended up doing.