Properly check if a package is installed or not.
dpkg-query -s <pkg> returns 0 even when status is "deinstall ok config-files".
Instead, check if the full "Status" string starts with either "install" or "hold".
Replace virtual package names by actual ones.
Remove package with remove instead of autoremove. Run autoremove at the end of post-common.
autoremove is not supposed to take parameters in the first place, and fails to remove uneeded dependencies of the specified packages when --purge is set.
Leverage on autoremove to remove packages that are only needed for building.
This makes the installation faster as we do not need to constantly install/uninstall packages.