#!/bin/sh
set -e

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

#
# deal with packages depending on exceptions:
#
case ${PIUPARTS_OBJECTS%%=*} in
	ltsp-client|education-thin-client)
				log_debug
				PIUPARTS_OBJECTS=ltsp-client-core
				;;
	upstart-dconf-bridge|upstart-monitor)
				log_debug
				PIUPARTS_OBJECTS=upstart
				;;
	netscript-2.4-upstart)	log_debug
				PIUPARTS_OBJECTS=upstart
				;;
	live-config-upstart)	log_debug
				PIUPARTS_OBJECTS=upstart
				;;
	clvm|dtc-xen|ganeti|ganeti2|mylvmbackup|redhat-cluster-suite|libvirt0|libcollectdclient0|liblinux-lvm-perl|autopkgtest-xenlvm|collectd-dbg|collectd|cman|libsys-virt-perl|libvirt-dev|libvirt-ocaml|libvirt-ruby1.8|libvirt0-dbg|python-libvirt|virt-top|virt-viewer|xenwatch|gfs-tools|gfs2-tools|rgmanager|virtinst|collectd-utils|libcollectdclient-dev|libvirt-ocaml-dev|libvirt-ruby|mozilla-virt-viewer|munin-libvirt-plugins)
				# skip mini-buildd-bld here due to resource violation, see below
				log_debug
				PIUPARTS_OBJECTS=lvm2
				;;
esac

#
# deal with exceptions:
#
case ${PIUPARTS_OBJECTS%%=*} in
	fai-nfsroot)		log_debug
				# fai-nfsroot refuses installation unless this file exist
				touch /.THIS_IS_THE_FAI_NFSROOT
				;;
	ltsp-client-core)	log_debug
				# ltsp-client-core refuses installation unless this file exist
				touch /etc/ltsp_chroot
				;;
	upstart)		log_debug
				# force installation and removal of essential package sysvinit
				yes 'Yes, do as I say!' | apt-get -y --force-yes install upstart
				;;
	file-rc)		log_debug
				# force installation and removal of essential package sysv-rc
				yes 'Yes, do as I say!' | apt-get -y --force-yes install file-rc
				;;
	systemd-sysv)		log_debug
				# force installation and removal of essential package sysvinit
				yes 'Yes, do as I say!' | apt-get -y --force-yes install systemd-sysv
				;;
	lvm2)
				if [ "$PIUPARTS_PHASE" = "install" ] && ( \
					[ "$PIUPARTS_DISTRIBUTION" = "squeeze" ] || \
					[ "$PIUPARTS_DISTRIBUTION" = "squeeze-proposed" ] || \
					[ "$PIUPARTS_DISTRIBUTION" = "squeeze-backports" ] || \
					[ "$PIUPARTS_DISTRIBUTION" = "lenny" ] )
				then
					# work around lvm2 bug http://bugs.debian.org/603036 which is squeeze-ignore
					log_debug
					apt-get -y install udev
				fi
				;;
	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" ]; 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
