#! /bin/sh

. /usr/share/debconf/confmodule

# Back up anna's state.
standard_modules_backup=true
if db_get anna/standard_modules; then
	standard_modules_backup="$RET"
fi
if [ -f /var/lib/anna-install/queue ]; then
	mv -f /var/lib/anna-install/queue /var/lib/anna-install/queue.backup
fi

# Don't do the usual installation of everything we can find.
db_set anna/standard_modules false

anna-install "$@"

# We only support specific retrievers here, because some of them are
# difficult to use properly at this stage, and not all of them make sense.
# For instance, the netboot initrd already has enough packages to retrieve
# Kickstart files from the network, and if you were using that initrd it's
# unlikely that you'd want to retrieve Kickstart files from a CD.
#
# Whatever way we do it, this is going to be a hideous layering violation.
# The only question is which layers we violate. We call postinsts manually
# to avoid leaving packages configured, which would confuse d-i later on.

export UDPKG_QUIET=y

if [ -x /var/lib/dpkg/info/load-cdrom.postinst ]; then
	if [ ! -d /cdrom/dists ]; then
		/lib/kickseed/kickseed-udpkg cdrom-detect
	fi
	/var/lib/dpkg/info/load-cdrom.postinst configure
elif [ -x /var/lib/dpkg/info/load-iso.postinst ]; then
	if [ ! -d /cdrom/dists ]; then
		/lib/kickseed/kickseed-udpkg iso-scan
	fi
	/var/lib/dpkg/info/load-iso.postinst configure
elif [ -x /var/lib/dpkg/info/load-media.postinst ]; then
	/var/lib/dpkg/info/load-media.postinst configure
else
	logger -t kickseed "No supported retriever found!"
	exit 1
fi

# Put anna's state back the way it was, to allow d-i's normal run.
if [ -f /var/lib/anna-install/queue.backup ]; then
	mv -f /var/lib/anna-install/queue.backup /var/lib/anna-install/queue
fi
db_set anna/standard_modules "$standard_modules_backup"

exit 0
