#!/bin/sh
set -e

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

is_installed()
{
	local pkg="$1"
	dpkg-query -s "$pkg" >/dev/null 2>&1 || return 1
	local status="$(dpkg-query -W -f '${Status}' $pkg)"
	test "$status" != "unknown ok not-installed" || return 1
	test "$status" != "deinstall ok config-files" || return 1
	return 0
}

dbconfig_enable_purge()
{
	log_debug
	echo "$1 $1${2+/$2}/dbconfig-remove boolean true" | debconf-set-selections
	echo "$1 $1${2+/$2}/purge boolean true" | debconf-set-selections
}

if [ "$PIUPARTS_DISTRIBUTION_NEXT" = "squeeze" ]; then

	case ${PIUPARTS_OBJECTS%%=*} in
		crm114)
			#562946
			log_debug
			echo "crm114 crm114/forceupgrade boolean true" | debconf-set-selections
			;;
		slapd)
			# squeeze postinst wants to restore a backup from lenny that was never created
			log_debug
			dpkg --purge slapd
			;;
	esac

	# squeeze does not properly upgrade adduser.conf, so generate a new one
	if [ -f /etc/adduser.conf ]; then
		md5="$(md5sum /etc/adduser.conf | awk '{ print $1 }')"
		# 5b577c9cb18e4852fc7e45830d230ec1: adduser/lenny pristine
		# 28928669e36f1ab616dfda39af3d79a7: adduser/lenny + dpsyco-lib/lenny
		if	[ "$md5" = "5b577c9cb18e4852fc7e45830d230ec1" ] || \
			[ "$md5" = "28928669e36f1ab616dfda39af3d79a7" ]
		then
			rm -fv /etc/adduser.conf
		fi
	fi

fi

if [ "$PIUPARTS_DISTRIBUTION_NEXT" = "wheezy" ]; then

	case ${PIUPARTS_OBJECTS%%=*} in
		cnews)
			# dpkg 1.16 does not like the bad cnews version number cr.g7-40.4
			# cnews was removed after lenny
			log_debug
			dpkg --purge cnews
			;;
		nfsbooted)
			# sysv-rc fails migration to dependency based boot sequence if nfsbooted is installed
			# nfsbooted was removed after lenny
			log_debug
			dpkg --purge nfsbooted
			;;
		pdns-backend-mysql)
			# WORKSAROUND #698562: upgrade from squeeze => wheezy fails if database has not been created before the upgrade
			log_debug
			dpkg --purge pdns-backend-mysql
			;;
		pdns-backend-pgsql)
			# WORKSAROUND #698562: upgrade from squeeze => wheezy fails if database has not been created before the upgrade
			log_debug
			dpkg --purge pdns-backend-pgsql
			;;
	esac

	# WORKSAROUND #655969: lirc: prompting due to modified conffiles which where not modified by the user: /etc/lirc/hardware.conf
	if [ -f /etc/lirc/hardware.conf ]; then
		log_debug
		sed -i '/^DRIVER=/s/.*/DRIVER="UNCONFIGURED"/' /etc/lirc/hardware.conf
	fi

fi

if [ "$PIUPARTS_DISTRIBUTION_NEXT" = "jessie" ]; then

	case ${PIUPARTS_OBJECTS%%=*} in
		ekeyd-uds)
			# ekeyd-uds was removed after squeeze, maintainer scripts are incompatible with udev/jessie
			log_debug
			dpkg --purge ekeyd-uds
			;;
		otrs2)
			#707075: cannot automatically upgrade from wheezy to jessie due to mysql storage engine change
			if is_installed mysql-server; then
				log_debug
				dbconfig_enable_purge otrs2
				dpkg --purge otrs2
			fi
			;;
	esac

fi

if [ "$PIUPARTS_DISTRIBUTION_NEXT" = "experimental" ]; then

	case ${PIUPARTS_OBJECTS} in
		dnscache-run=1:1.05-9~exp2)
			#664848: breaks dns resolution in chroot if installed in a chroot
			log_debug
			echo "*** ABORT - Installation broke DNS in chroot ***"
			exit 1
			;;
	esac

fi
