#!/bin/sh
# postrm script for tomsoft-nadzor
#
# see: dh_installdeb(1)

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 http://www.debian.org/doc/debian-policy/ or
# the debian-policy package

# /mn/ we must put this at the beginning so it can redirect output (even if we don't use it!), otherwise any output from the script will confuse debconf
# see https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=52739#20
. /usr/share/debconf/confmodule

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

# we don't need anything more from debconf, so we use db_stop so invoke-rc.d doesn't confuse it (see debconf-devel(7))
db_stop || true

MYSQL_PLUGIN=/usr/share/munin/plugins/mysql_

case "$1" in
    purge)
    	MUNIN_CONF=/etc/munin/munin-node.conf
    	if [ -n "`dpkg-divert --list ${MUNIN_CONF}`" ]
    	then
	    	test -f ${MUNIN_CONF} && mv -f ${MUNIN_CONF} ${MUNIN_CONF}.dpkg-old-tomsoft
    		dpkg-divert --package tomsoft-nadzor --rename --remove ${MUNIN_CONF} >&2
    	fi

	[ -n "`dpkg-divert --list ${MYSQL_PLUGIN}`" ] && rm -f ${MYSQL_PLUGIN} && dpkg-divert --package tomsoft-nadzor --rename --remove ${MYSQL_PLUGIN}

    	# output to /dev/null is needed, or we hit https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=52739;msg=7 with DEBCONF_DEBUG=1 output like:
    	# + RET=20 Unsupported command "stopping" (full line was "Stopping nagios-nrpe: nagios-nrpe.") received from confmodule.
    	invoke-rc.d --quiet munin-node stop >&2 || true
    	invoke-rc.d --quiet nagios-nrpe-server stop >&2 || true
    ;;

    remove|upgrade|failed-upgrade|abort-install|abort-upgrade|disappear)
    ;;

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



exit 0
