The fact that a package may be upgraded, and not simply installed or removed, makes the think a little bit tougher... The problem is that the %postun script of the update is run after the %post of the old version. So all %post stuff is lost...
The solution is brought by a parameter passed to each of the four scripts. That parameter is the number of packages with the same name that will normaly be present after total installation/upgrade.
Table A-1. Value of the parameter passed to pre and post scripts
Parameter \ Script | %pre | %post | %preun | %postun |
---|---|---|---|---|
for a first time install | 1 | 1 | N/C | N/C |
for an upgrade | 2 | 2 | 2 | 2 |
for a removal | N/C | N/C | 0 | 0 |
This will allow the programmer to distinguish different attitudes of his scripts depending on the operation: install or upgrade.
For install scripts (%post, %pre) check if $1 is equal to "1" then it is the first time install, not an update.
For uninstall scripts (%postun, %preun) check if $1 is equal to "0", if yes then it is a full removal; if not it is either an upgrade or an install --force of the same package.
A single test is therefore enough, to call the right action at the right time.