#!/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)
				# force installation and removal of essential package sysv-rc
				log_debug
				echo 'Yes, do as I say!' | apt-get -y --force-yes install file-rc
				;;
			squeeze*|stretch*)
				# 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
		;;
	sysvinit-core)
		# this will remove systemd-sysv
		log_debug
		apt-get -y install sysvinit-core
		;;
	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

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

	case ${PIUPARTS_OBJECTS%%=*} in
		libudev1)
			log_debug
			apt-get -y -t $PIUPARTS_DISTRIBUTION install udev systemd
			;;
	esac

fi

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

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

case ${PIUPARTS_OBJECTS%%=*} in
	auctex)
			# do not run the (long) latex macro indexing task in the background (#867020)
			log_debug
			echo "auctex auctex/doauto select Foreground" | debconf-set-selections
			;;
	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
			;;
	msttcorefonts|\
	ttf-mscorefonts-installer)
			# Recommends: ca-certificates
			log_debug
			apt-get -y install ca-certificates
			;;
	samhain)
			# work around #749602
			log_debug
			mkdir -p /var/state/samhain/
			touch /var/state/samhain/samhain_file
			;;
esac

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

	case ${PIUPARTS_OBJECTS%%=*} in
		libjs-protoaculous)
			#919917: yui-compressor: insufficient java dependency in jessie
			log_debug
			apt-get -y install default-jre-headless
			;;
	esac

fi

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

	case ${PIUPARTS_OBJECTS%%=*} in
		ttf-root-installer)
			#774163: missing Depends: ca-certificates
			log_debug
			apt-get -y install ca-certificates
			;;
	esac

fi

if	[ "$PIUPARTS_DISTRIBUTION" = "squeeze" ] || \
	[ "$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
			;;
		jhove|\
		libbytecode-java|\
		libcodemodel-java|\
		libjavassist-java|\
		libjdic-java|\
		libjgroups-java|\
		libjibx-java|\
		libpj-java|\
		libswingx-java)
			# these packages depend on sun-java* | java2-runtime
			# install default-jre to satisfy the java2-runtime dependency
			log_debug
			apt-get -y install default-jre
			;;
	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-rep)
			#656746: mini-buildd-rep: fails to install: missing dependency on lvm2
			log_debug
			apt-get -y install udev lvm2
			;;
	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|dtc-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
			if [ ! -e /dev/MAKEDEV ]; then
				log_debug
				ln -sfv /sbin/MAKEDEV /dev/MAKEDEV
			fi
			;;
	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
