#! /bin/sh -e
# Bring up everything that's needed to fetch a Kickstart file from the
# network.

# without this, debconf clients will talk debconf protocol to syslog
. /usr/share/debconf/confmodule

. /lib/kickseed/cmdline.sh

EXTRA_UDEBS="$@"

if [ ! -x /var/lib/dpkg/info/ethdetect.postinst ] || \
   [ ! -x /var/lib/dpkg/info/netcfg.postinst ]; then
	/lib/kickseed/kickseed-anna ethdetect netcfg $EXTRA_UDEBS
fi

if [ -x /var/lib/dpkg/info/ethdetect.postinst ]; then
	/lib/kickseed/kickseed-udpkg ethdetect
else
	logger -t kickseed "ethdetect cannot be installed"
	exit 1
fi

set_question () {
	if ! db_fget "$1" seen || [ "$RET" = false ]; then
		db_register debian-installer/dummy "$1"
		if [ "$2" ]; then
			db_set "$1" "$2"
		fi
		db_fset "$1" seen true
	fi
}

if [ -x /var/lib/dpkg/info/netcfg.postinst ]; then
	# Make sure we don't get asked unnecessary networking questions.
	# Do tell the user about errors, though.
	KSDEVICE="$(kickseed_cmdline /proc/cmdline ksdevice)"
	if [ -z "$KSDEVICE" ] || [ "$KSDEVICE" = link ]; then
		KSDEVICE=auto
	elif [ "$KSDEVICE" = bootif ]; then
		logger -t kickseed "ksdevice=bootif not supported yet; defaulting to first available interface"
		KSDEVICE=auto
	fi
	set_question netcfg/choose_interface "$KSDEVICE"
	set_question netcfg/get_hostname kickseed
	set_question netcfg/wireless_essid
	set_question netcfg/wireless_essid_again
	set_question netcfg/wireless_wep

	# Run the postinst by hand so that main-menu will run netcfg again
	# later, possibly with different preseeded answers.
	/var/lib/dpkg/info/netcfg.postinst configure 2>&1 | logger -t netcfg
fi
