post_install() {
    :
#!/bin/bash
set -e

# Permissions on the daemon tree
if [ -d /usr/libexec/sota-daemon ]; then
    chown -R root:root /usr/libexec/sota-daemon
    chmod 755 /usr/libexec/sota-daemon
    [ -f /usr/libexec/sota-daemon/sotad ] && chmod 755 /usr/libexec/sota-daemon/sotad
fi

# Load SELinux policy module on systems with SELinux (Fedora/RHEL/Rocky)
if command -v semodule >/dev/null 2>&1 && [ -f /usr/share/selinux/packages/sota-daemon.pp ]; then
    semodule -i /usr/share/selinux/packages/sota-daemon.pp || true
    # Apply our file contexts to the just-installed binaries
    if command -v restorecon >/dev/null 2>&1; then
        restorecon -R /usr/libexec/sota-daemon 2>/dev/null || true
    fi
fi

# Refresh desktop/icon caches (best-effort)
command -v update-desktop-database >/dev/null 2>&1 && update-desktop-database /usr/share/applications 2>/dev/null || true
command -v gtk-update-icon-cache    >/dev/null 2>&1 && gtk-update-icon-cache    /usr/share/icons/hicolor 2>/dev/null || true

# Start the service
if [ -f /etc/systemd/system/sotad.service ]; then
    systemctl daemon-reload
    systemctl enable sotad.service || true
    systemctl start  sotad.service || true
fi

exit 0

}
pre_remove() {
    :
#!/bin/bash
set -e
systemctl is-active --quiet sotad.service 2>/dev/null && systemctl stop sotad.service || true
systemctl is-enabled --quiet sotad.service 2>/dev/null && systemctl disable sotad.service || true
exit 0

}
post_remove() {
    :
#!/bin/bash
set -e

# fpm uses the same hook for all formats, but the package manager passes
# different "what's happening" hints in $1:
#   RPM   : "0" on full removal, "1" (or higher) during upgrade/reinstall
#           — at upgrade time, the NEW package's %post has already loaded
#           the new module, so doing semodule -r here would yank what we
#           just installed.
#   DEB   : "remove" / "purge" on real removal; "upgrade" / "failed-upgrade"
#           / "abort-*" / "disappear" during an upgrade.
#   pacman: no argument — only runs on actual removal.
case "$1" in
    upgrade|failed-upgrade|abort-install|abort-upgrade|disappear)
        exit 0  # DEB upgrade variants — leave the module loaded
        ;;
esac
if [ -n "$1" ] && [ "$1" -ge 1 ] 2>/dev/null; then
    exit 0  # RPM upgrade/reinstall — leave the module loaded
fi

# Real removal — drop our SELinux module if it was loaded.
if command -v semodule >/dev/null 2>&1 && semodule -l 2>/dev/null | grep -q '^sota-daemon$'; then
    semodule -r sota-daemon 2>/dev/null || true
fi

[ -f /etc/systemd/system/sotad.service ] && systemctl daemon-reload || true
command -v update-desktop-database >/dev/null 2>&1 && update-desktop-database /usr/share/applications 2>/dev/null || true

exit 0

}
