This whole thing can go to its own function in pkg-utils.inc.