#!/bin/sh

set -e

diversion_added_ver=0.14

case "$1" in
    purge)
	if which ucfq >/dev/null ; then
	    for cf in $(ucfq -w orphan-sysvinit-scripts | cut -f 1 -d ':') ; do
		for ext in '~' '%' .bak .ucf-new .ucf-old .ucf-dist;  do
		    rm -f "${cf}.$ext"
		done
		rm -f "$cf"
		if which ucf >/dev/null ; then
		    ucf --purge "$cf"
		fi
		if which ucfr >/dev/null ; then
		    ucfr --purge orphan-sysvinit-scripts "$cf"
		fi
		update-rc.d "${cf#/etc/init.d/}" remove >/dev/null
	    done
	fi

	;;
    upgrade)
	if which ucfq >/dev/null ; then
	    if [ -e /etc/init.d/iwd ]; then
		if ucfq -w /etc/init.d/iwd | grep -q orphan-sysvinit-scripts
		then
		    if which ucf >/dev/null ; then
			ucf --purge /etc/init.d/iwd
		    fi
		    if which ucfr >/dev/null ; then
			ucfr --purge orphan-sysvinit-scripts /etc/init.d/iwd
		    fi
		fi
	    fi
	fi

	;;
esac

if [ remove = "$1" ] || [ abort-install = "$1" ] || [ disappear = "$1" ] ||
       {
	   { [ abort-upgrade = "$1" ] || [ upgrade = "$1" ]; } &&
	       dpkg --compare-versions "$2" lt "$diversion_added_ver";
       }; then
   dpkg-divert --package orphan-sysvinit-scripts --remove --rename \
	       --divert /usr/lib/rsyslog/rsyslog-rotate.real \
	       /usr/lib/rsyslog/rsyslog-rotate
fi
# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0
