#!/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*)
				# force installation and removal of essential package sysv-rc
				log_debug
				yes 'Yes, do as I say!' | apt-get -y --force-yes install file-rc
				;;
		esac
		;;
	live-config-upstart|\
	netscript-2.4-upstart|\
	upstart)
		case "$PIUPARTS_DISTRIBUTION" in
			squeeze*|wheezy*)
				# force installation and removal of essential package sysvinit
				log_debug
				yes 'Yes, do as I say!' | apt-get -y --force-yes install upstart
				;;
		esac
		;;
	upstart-dconf-bridge|\
	upstart-monitor)
		# switch init to upstart before installation
		apt-get install upstart
		;;
	systemd-sysv)
		case "$PIUPARTS_DISTRIBUTION" in
			wheezy*)
				# force installation and removal of essential package sysvinit
				log_debug
				yes 'Yes, do as I say!' | apt-get -y --force-yes install systemd-sysv
				;;
		esac
		;;
esac

#
# the remaining exceptions are only for the initial package installation
#
if [ "$PIUPARTS_PHASE" != "install" ]; then
	exit 0
fi

case ${PIUPARTS_OBJECTS%%=*} in
	samhain)		log_debug
				# work around #749602
				mkdir -p /var/state/samhain/
				touch /var/state/samhain/samhain_file
				;;
	fai-nfsroot)		log_debug
				# fai-nfsroot refuses installation unless this file exist
				touch /.THIS_IS_THE_FAI_NFSROOT
				;;
	education-thin-client|\
	ltsp-client|\
	ltsp-client-core)	log_debug
				# ltsp-client-core refuses installation unless this file exist
				touch /etc/ltsp_chroot
				;;
	bugzilla3)
			# checksetup.pl goes into infinite loop asking for them
			log_debug
			echo "bugzilla3 bugzilla3/bugzilla_admin_name string bz@local.host" | debconf-set-selections
			echo "bugzilla3 bugzilla3/bugzilla_admin_real_name string Bz" | debconf-set-selections
			echo "bugzilla3 bugzilla3/bugzilla_admin_pwd password bzbzbzbz" | debconf-set-selections
			;;
esac

if [ "$PIUPARTS_DISTRIBUTION" = "wheezy-backports" ]; then

	case ${PIUPARTS_OBJECTS%%=*} in
		libreoffice-style-*)
			log_debug
			apt-get -y -t $PIUPARTS_DISTRIBUTION install ${PIUPARTS_OBJECTS%%=*} libreoffice-common-
			;;
	esac

fi

if [ "$PIUPARTS_DISTRIBUTION" = "squeeze-backports" ]; then

	case ${PIUPARTS_OBJECTS%%=*} in
		ekeyd)
			log_debug
			apt-get -y install udev
			;;
	esac

fi

if	[ "$PIUPARTS_DISTRIBUTION" = "squeeze" ] || \
	[ "$PIUPARTS_DISTRIBUTION" = "squeeze-proposed" ] || \
	[ "$PIUPARTS_DISTRIBUTION" = "squeeze-backports" ] || \
	[ "$PIUPARTS_DISTRIBUTION" = "lenny" ]; then

	case ${PIUPARTS_OBJECTS%%=*} in
		autopkgtest-xenlvm|\
		clvm|\
		cman|\
		collectd|\
		collectd-dbg|\
		collectd-utils|\
		dtc-xen|\
		ganeti|\
		ganeti2|\
		gfs-tools|\
		gfs2-tools|\
		libcollectdclient0|\
		libcollectdclient-dev|\
		liblinux-lvm-perl|\
		libsys-virt-perl|\
		libvirt0|\
		libvirt0-dbg|\
		libvirt-dev|\
		libvirt-ocaml|\
		libvirt-ocaml-dev|\
		libvirt-ruby|\
		libvirt-ruby1.8|\
		mozilla-virt-viewer|\
		munin-libvirt-plugins|\
		mylvmbackup|\
		python-libvirt|\
		redhat-cluster-suite|\
		rgmanager|\
		virtinst|\
		virt-top|\
		virt-viewer|\
		xenwatch|\
		lvm2)
			# work around lvm2 bug https://bugs.debian.org/603036 which is squeeze-ignore
			log_debug
			apt-get -y install udev
			;;
	esac

fi

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

	case ${PIUPARTS_OBJECTS%%=*} in
		bootcd-ia64)
				if [ "$PIUPARTS_TEST" = "distupgrade" ] && [ "$(uname -m)" = "x86_64" ]; then
					#622690: bootcd-ia64 has a Pre-Depends/Depends cycle that apt cannot resolve
					log_debug
					apt-get -y install bootcd-i386
				fi
				;;
		mini-buildd-bld|mini-buildd-rep)
				#632955, #656746 - time limit exceeded during install
				log_debug
				echo "*** ABORT - Installation would deadlock ***"
				exit 1
				;;
	esac

fi

if [ "$PIUPARTS_DISTRIBUTION" = "lenny" ]; then

	# install undeclared dependencies
	case ${PIUPARTS_OBJECTS%%=*} in
		clamav-getfiles)
				#603082
				log_debug
				apt-get -y install curl
				;;
		cyrus-*-2.2|sa-learn-cyrus)
				#694254 db4.2-util exists in lenny only and is needed for upgrades to wheezy
				log_debug
				apt-get -y install db4.2-util
				;;
		debian-edu-artwork-usplash)
				# postinst fails without update-initramfs
				log_debug
				apt-get -y install initramfs-tools
				;;
		gforge-shell-postgresql|gforge-plugin-scmcvs|gforge-plugin-scmsvn)
				#604218
				log_debug
				apt-get -y install libcap2
				;;
		otrs2)
				#561889
				log_debug
				apt-get -y install libapache2-mod-perl2
				;;
		sdic-gene95)
				#478592
				log_debug
				apt-get -y install bzip2 wget
				;;
		tftpd-hpa)
				#522780
				log_debug
				apt-get -y install update-inetd
				;;
		ttf-beteckna)
				#502707
				log_debug
				apt-get -y install defoma
				;;
	esac

	# prefer inn over cnews (bad version number cr.g7-40.4)
	case ${PIUPARTS_OBJECTS%%=*} in
		newsx|post-faq)
				log_debug
				apt-get -y install inn
				;;
	esac

	# work around postinst failures
	case ${PIUPARTS_OBJECTS%%=*} in
		ion3)
			# annoying debconf prompt
			log_debug
			echo "ion3 ion3/acknowledge-maybe-outdated boolean true" | debconf-set-selections
			echo "ion3 ion3/acknowledge-outdated boolean true" | debconf-set-selections
			;;
		ipppd|ibod|isdnutils)
			#542156
			log_debug
			test -e /dev/MAKEDEV || ln -sfv /sbin/MAKEDEV /dev/MAKEDEV
			;;
	esac

	# allow lenny kernel image installation in chroot
	for flavor in amd64 openvz-amd64 vserver-amd64 xen-amd64 486 686 686-bigmem openvz-686 vserver-686 vserver-686-bigmem xen-686
	do
		echo "linux-image-2.6.26-2-$flavor linux-image-2.6.26-2-$flavor/preinst/bootloader-initrd-2.6.26-2-$flavor boolean false"
		echo "linux-image-2.6.26-2-$flavor linux-image-2.6.26-2-$flavor/preinst/lilo-initrd-2.6.26-2-$flavor boolean false"
	done | debconf-set-selections

	# deterministic /bin/sh on upgrades
	echo "dash dash/sh boolean true" | debconf-set-selections

fi
