#!/bin/bash
# ClamUI post-removal script
# Updates icon cache and desktop database after package removal

set -e

# postrm script for clamui
#
# See https://www.debian.org/doc/debian-policy/ for policy details
#
# Actions:
#   remove          - Package files have been removed
#   purge           - Package configuration files are being removed
#   upgrade         - Package is being upgraded (old version's postrm)
#   disappear       - Package disappeared due to overwrite
#   failed-upgrade  - Upgrade failed
#   abort-install   - Installation was aborted
#   abort-upgrade   - Upgrade was aborted

case "$1" in
    remove|purge)
        # Update desktop file database after removal
        # This unregisters the .desktop file from the system
        if command -v update-desktop-database > /dev/null 2>&1; then
            update-desktop-database -q /usr/share/applications || true
        fi

        # Update GTK icon cache after removal
        # This removes the application icon from the desktop environment cache
        if command -v gtk-update-icon-cache > /dev/null 2>&1; then
            gtk-update-icon-cache -q -t -f /usr/share/icons/hicolor || true
        fi
        ;;

    upgrade)
        # Package is being upgraded to a new version
        # New version's postinst will handle cache updates
        :
        ;;

    disappear)
        # Package was overwritten by another package
        # Update caches to reflect the change
        if command -v update-desktop-database > /dev/null 2>&1; then
            update-desktop-database -q /usr/share/applications || true
        fi
        if command -v gtk-update-icon-cache > /dev/null 2>&1; then
            gtk-update-icon-cache -q -t -f /usr/share/icons/hicolor || true
        fi
        ;;

    failed-upgrade|abort-install|abort-upgrade)
        # Operation failed or was aborted
        # Nothing to clean up
        :
        ;;

    *)
        echo "postrm called with unknown argument '$1'" >&2
        exit 1
        ;;
esac

exit 0
