#!/bin/sh

set -e

# summary of how this script can be called:
#        * <postrm> `remove'
#        * <postrm> `purge'
#        * <old-postrm> `upgrade' <new-version>
#        * <new-postrm> `failed-upgrade' <old-version>
#        * <new-postrm> `abort-install'
#        * <new-postrm> `abort-install' <old-version>
#        * <new-postrm> `abort-upgrade' <old-version>
#        * <disappearer's-postrm> `disappear' <overwriter>
#          <overwriter-version>
# for details, see https://www.debian.org/doc/debian-policy/ or
# the debian-policy package

abort_conffile_transfer() {
    local conffile="$1"
    local lastver="$2"
    local pkgfrom="$3"
    local pkgto="$4"

    if [ "$5" != "--" ]; then
        echo "abort_conffile_transfer called with the wrong number of arguments" >&2
        return 1
    fi
    for _ in $(seq 1 5); do
        shift
    done

    # If we were installing from scratch or upgrading from a new enough version
    # when the error occurred, then no transfer was in progress and we don't
    # need to rollback any changes
    if [ -z "$2" ] || dpkg --compare-versions -- "$2" gt "$lastver"; then
        return 0
    fi

    # If the conffile was being transferred, return it to its original location
    if [ -e "$conffile.dpkg-transfer" ]; then
        mv -f "$conffile.dpkg-transfer" "$conffile"
    fi

    # Clean up additional state
    rm -f "$conffile.dpkg-disappear"
}

NWFILTERS="
    allow-arp
    allow-dhcp
    allow-dhcp-server
    allow-incoming-ipv4
    allow-ipv4
    clean-traffic
    clean-traffic-gateway
    no-arp-ip-spoofing
    no-arp-mac-spoofing
    no-arp-spoofing
    no-ip-multicast
    no-ip-spoofing
    no-mac-broadcast
    no-mac-spoofing
    no-other-l2-traffic
    no-other-rarp-traffic
    qemu-announce-self
    qemu-announce-self-rarp
"

case "$1" in
    purge)
        if getent group libvirt >/dev/null; then
            delgroup libvirt >/dev/null || true
        fi

        if getent passwd libvirt-qemu >/dev/null; then
            deluser libvirt-qemu >/dev/null || true
        fi

        if getent group libvirt-qemu >/dev/null; then
            delgroup libvirt-qemu >/dev/null || true
        fi

        # a running libvirt-dnsmasq will break these removals
        # yet the lifecycle of the network is non-related to the pkg purge
        # Therefore ignore errors on these removals, better leave a user than break
        if getent group libvirt-dnsmasq >/dev/null; then
            delgroup libvirt-dnsmasq --system 2>/dev/null >/dev/null || true
        fi
        if getent passwd libvirt-dnsmasq >/dev/null; then
            deluser libvirt-dnsmasq --system 2>/dev/null >/dev/null || true
        fi

        # Clean up logs and cached capabilities
        rm -rf /var/log/libvirt \
               /var/cache/libvirt/qemu/capabilities

        # Clean up created dirs if existent and empty, they contain precious
        # data otherwise
        for dir in /var/lib/libvirt/qemu/save \
                   /var/lib/libvirt/qemu/snapshot \
                   /var/lib/libvirt/qemu/dump \
                   /var/lib/libvirt/qemu/nvram \
                   /var/lib/libvirt/qemu/ram/libvirt/qemu \
                   /var/lib/libvirt/qemu/ram/libvirt \
                   /var/lib/libvirt/qemu/ram \
                   /var/lib/libvirt/qemu/channel \
                   /var/lib/libvirt/qemu \
                   /var/cache/libvirt/qemu; do
            [ ! -d $dir ] || rmdir --ignore-fail-on-non-empty $dir
        done

        # Remove the link set up by postinst
        rm -f /etc/libvirt/qemu/networks/autostart/default.xml
    ;;

    abort-install|abort-upgrade)
        abort_conffile_transfer \
            "/etc/libvirt/qemu/networks/default.xml" \
            "6.9.0-2~" \
            "libvirt-daemon-system" \
            "libvirt-daemon-config-network" \
            -- \
            "$@"
        for nwfilter in $NWFILTERS; do
            abort_conffile_transfer \
                "/etc/libvirt/nwfilter/$nwfilter.xml" \
                "6.9.0-2~" \
                "libvirt-daemon-system" \
                "libvirt-daemon-config-nwfilter" \
                -- \
                "$@"
        done

        # dh_apparmor can't work with dir/file profile filenames yet (#979500)
        ABSTRACTIONS_DIR="/etc/apparmor.d/abstractions"
        LOCAL_ABSTRACTIONS_DIR="/etc/apparmor.d/local/abstractions"
        LIBVIRT_ABSTRACTIONS="libvirt-lxc libvirt-qemu"
        for name in $LIBVIRT_ABSTRACTIONS; do
            abstraction="$ABSTRACTIONS_DIR/$name"
            local_abstraction="$LOCAL_ABSTRACTIONS_DIR/$name"

            if [ ! -e "$abstraction" ] ; then
                rm -f "$local_abstraction"
                rmdir --ignore-fail-on-non-empty "$LOCAL_ABSTRACTIONS_DIR" 2>/dev/null
            fi
        done
    ;;

    remove)
        if [ -L /etc/dnsmasq.d/libvirt-daemon ]; then
            echo "Removing libvirt-daemon dnsmasq configuration"
            rm -f /etc/dnsmasq.d/libvirt-daemon 2>/dev/null || true

            # Try to restart a potential system wide dnsmasq
            invoke-rc.d dnsmasq restart 2>/dev/null || true
        fi
    ;;

    upgrade|failed-upgrade|disappear)
    ;;

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

# Automatically added by dh_installdeb/13.6ubuntu1
dpkg-maintscript-helper rm_conffile /etc/init.d/libvirt-guests 5.6.0-3\~ libvirt-daemon-system -- "$@"
dpkg-maintscript-helper rm_conffile /etc/init.d/libvirtd 5.6.0-3\~ libvirt-daemon-system -- "$@"
dpkg-maintscript-helper rm_conffile /etc/init.d/virtlogd 5.6.0-3\~ libvirt-daemon-system -- "$@"
dpkg-maintscript-helper rm_conffile /etc/logrotate.d/libvirtd.uml 5.0.0-2\~ -- "$@"
# End automatically added section
# Automatically added by dh_installsystemd/13.6ubuntu1
if [ "$1" = remove ] && [ -d /run/systemd/system ] ; then
	systemctl --system daemon-reload >/dev/null || true
fi
# End automatically added section
# Automatically added by dh_installsystemd/13.6ubuntu1
if [ "$1" = "remove" ]; then
	if [ -x "/usr/bin/deb-systemd-helper" ]; then
		deb-systemd-helper mask 'libvirtd-admin.socket' 'libvirtd-ro.socket' 'libvirtd.socket' 'virtlockd-admin.socket' 'virtlockd.socket' 'virtlogd-admin.socket' 'virtlogd.socket' >/dev/null || true
	fi
fi

if [ "$1" = "purge" ]; then
	if [ -x "/usr/bin/deb-systemd-helper" ]; then
		deb-systemd-helper purge 'libvirtd-admin.socket' 'libvirtd-ro.socket' 'libvirtd.socket' 'virtlockd-admin.socket' 'virtlockd.socket' 'virtlogd-admin.socket' 'virtlogd.socket' >/dev/null || true
		deb-systemd-helper unmask 'libvirtd-admin.socket' 'libvirtd-ro.socket' 'libvirtd.socket' 'virtlockd-admin.socket' 'virtlockd.socket' 'virtlogd-admin.socket' 'virtlogd.socket' >/dev/null || true
	fi
fi
# End automatically added section
# Automatically added by dh_installsystemd/13.6ubuntu1
if [ "$1" = remove ] && [ -d /run/systemd/system ] ; then
	systemctl --system daemon-reload >/dev/null || true
fi
# End automatically added section
# Automatically added by dh_installsystemd/13.6ubuntu1
if [ "$1" = "remove" ]; then
	if [ -x "/usr/bin/deb-systemd-helper" ]; then
		deb-systemd-helper mask 'libvirt-guests.service' 'virtlockd.service' 'virtlogd.service' >/dev/null || true
	fi
fi

if [ "$1" = "purge" ]; then
	if [ -x "/usr/bin/deb-systemd-helper" ]; then
		deb-systemd-helper purge 'libvirt-guests.service' 'virtlockd.service' 'virtlogd.service' >/dev/null || true
		deb-systemd-helper unmask 'libvirt-guests.service' 'virtlockd.service' 'virtlogd.service' >/dev/null || true
	fi
fi
# End automatically added section
# Automatically added by dh_installsystemd/13.6ubuntu1
if [ "$1" = remove ] && [ -d /run/systemd/system ] ; then
	systemctl --system daemon-reload >/dev/null || true
fi
# End automatically added section
# Automatically added by dh_installsystemd/13.6ubuntu1
if [ "$1" = "remove" ]; then
	if [ -x "/usr/bin/deb-systemd-helper" ]; then
		deb-systemd-helper mask 'libvirtd.service' >/dev/null || true
	fi
fi

if [ "$1" = "purge" ]; then
	if [ -x "/usr/bin/deb-systemd-helper" ]; then
		deb-systemd-helper purge 'libvirtd.service' >/dev/null || true
		deb-systemd-helper unmask 'libvirtd.service' >/dev/null || true
	fi
fi
# End automatically added section
# Automatically added by dh_installdebconf/13.6ubuntu1
if [ "$1" = purge ] && [ -e /usr/share/debconf/confmodule ]; then
	. /usr/share/debconf/confmodule
	db_purge
fi
# End automatically added section
# Automatically added by dh_apparmor/3.0.4-2ubuntu2.4
if [ "$1" = "purge" ] && ! [ -e "/etc/apparmor.d/usr.sbin.libvirtd" ] ; then
    rm -f "/etc/apparmor.d/disable/usr.sbin.libvirtd" || true
    rm -f "/etc/apparmor.d/force-complain/usr.sbin.libvirtd" || true
    rm -f "/etc/apparmor.d/local/usr.sbin.libvirtd" || true
    rm -f /var/cache/apparmor/*/"usr.sbin.libvirtd" || true
    rmdir /etc/apparmor.d/disable 2>/dev/null || true
    rmdir /etc/apparmor.d/local   2>/dev/null || true
    rmdir /etc/apparmor.d         2>/dev/null || true
fi
# End automatically added section
# Automatically added by dh_apparmor/3.0.4-2ubuntu2.4
if [ "$1" = "purge" ] && ! [ -e "/etc/apparmor.d/usr.lib.libvirt.virt-aa-helper" ] ; then
    rm -f "/etc/apparmor.d/disable/usr.lib.libvirt.virt-aa-helper" || true
    rm -f "/etc/apparmor.d/force-complain/usr.lib.libvirt.virt-aa-helper" || true
    rm -f "/etc/apparmor.d/local/usr.lib.libvirt.virt-aa-helper" || true
    rm -f /var/cache/apparmor/*/"usr.lib.libvirt.virt-aa-helper" || true
    rmdir /etc/apparmor.d/disable 2>/dev/null || true
    rmdir /etc/apparmor.d/local   2>/dev/null || true
    rmdir /etc/apparmor.d         2>/dev/null || true
fi
# End automatically added section


exit 0
