#!/bin/sh
set -e

log_debug() {
	echo "Debug: piuparts exception for package $PIUPARTS_OBJECTS"
}

case ${PIUPARTS_OBJECTS%%=*} in
	file-rc)
		case "$PIUPARTS_DISTRIBUTION" in
			lenny|squeeze*)
				log_debug
				# removal won't work if sysv-rc isn't reinstalled
				yes 'Yes, do as I say!' | apt-get -y --force-yes install sysv-rc
				;;
		esac
		;;
	isdnlog|isdnutils)
		#WORKSAROUND #431855: fails with "There are still files in /etc/isdn/ that you may want to remove manually."
		if [ "$PIUPARTS_DISTRIBUTION" = "lenny" ]; then
			log_debug
			rm -fv /etc/isdn/*
		fi
		;;
	apt-listbugs)
		# cleanup from post_install_exceptions
		log_debug
		rm -f /usr/sbin/apt-listbugs
		dpkg-divert --remove --rename /usr/sbin/apt-listbugs
		;;
esac

case "$PIUPARTS_DISTRIBUTION" in

	stretch*|buster*|sid)
		case ${PIUPARTS_OBJECTS%%=*} in
			init|\
			education-thin-client|\
			ltsp-client|\
			ltsp-client-core)
				log_debug
				# requires removal of essential packages
				if [ ! -f /etc/apt/apt.conf.d/piuparts-allow-remove-essential ]
				then
					echo "Enabling APT::Get::allow-remove-essential"
					echo 'APT::Get::allow-remove-essential "true";' >> /etc/apt/apt.conf.d/piuparts-allow-remove-essential
				fi
				;;
		esac
		;;
esac

# Allow removal of the kernel running on the host from the chroot.
UNAME_R="$(uname -r)"
echo "linux-image-$UNAME_R linux-image-$UNAME_R/prerm/removing-running-kernel-$UNAME_R boolean false" | debconf-set-selections
