#!/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)		log_debug
				PIUPARTS_OBJECTS=ltsp-client-core
				;;
	netscript-2.4-upstart)	log_debug
				PIUPARTS_OBJECTS=upstart
				;;
	live-config-upstart)	log_debug
				PIUPARTS_OBJECTS=upstart
				;;
	clvm|dtc-xen|ganeti2|mylvmbackup|redhat-cluster-suite)
				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" = "lenny" ] ); then
					# work around lvm2 bug http://bugs.debian.org/603036 which is squeeze-ignore
					log_debug
					apt-get -y install udev
				fi
				;;
esac

# allow lenny kernel image installation in chroot
if [ "$PIUPARTS_DISTRIBUTION" = "lenny" ]; then
	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
fi
