#!/bin/sh
#
# Load debconf values before installation is started, to allow
# pre-seeding of debconf answers.

if [ -d /var/log/installer ]; then
	DI_SEED=/var/log/installer/debconf-seed
else
	DI_SEED=/var/log/debian-installer/debconf-seed
fi
SEEDDIR=/usr/share/base-config/debconf-seed

case "$1" in
get_preseed_command)
	# Get the preseeded command, if any.
	. /usr/share/debconf/confmodule
	if db_get base-config/early_command; then
		echo "$RET" >&4
	fi

	;;
*)
	if [ -e $DI_SEED ]; then
		if debconf-set-selections -c $DI_SEED; then
			debconf-set-selections $DI_SEED
		else
			echo "error: Unparsable debconf values in file '$DI_SEED'." >&2
		fi
	fi

	if [ -d $SEEDDIR ] ; then
		for file in $SEEDDIR/*; do
			if debconf-set-selections -c $file ; then
				debconf-set-selections $file
			else
				echo "error: Unparsable debconf values in file '$file'." >&2
			fi
		done
	fi

	# See if there is a preseeded command. If so, run it.
	PRESEED_COMMAND=$($0 get_preseed_command 4>&1 || true)
	if [ -n "$PRESEED_COMMAND" ]; then
		# TODO error handling (needs error dialog)
		eval $PRESEED_COMMAND || true
	fi

	;;
esac
