#! /bin/sh -e

. /usr/share/debconf/confmodule
db_version 2.0

# Only ask this question on new installs and reconfigures
if ([ "$1" = "configure" ] && [ -z "$2" ]) || [ "$1" = "reconfigure" ]; then
	db_input low cobbler/password || true
	db_go

	# Sanitize some ip settings, so as to make cobbler more useful out of the box
	# This interface/ipaddr code was stolen from /usr/lib/byobu/ip_address
	while read Iface Destination Gateway Flags RefCnt Use Metric Mask MTU Window IRTT; do
		[ "$Mask" = "00000000" ] && \
		interface="$Iface" && \
		ipaddr=$(LC_ALL=C /sbin/ip -4 addr list dev "$interface" scope global) && \
		ipaddr=${ipaddr#* inet } && \
		ipaddr=${ipaddr%%/*} && \
		break
	done < /proc/net/route

	db_get cobbler/server_and_next_server || true
	if ([ -n "$RET" ] && [ "$RET" != "127.0.0.1" ]); then
		db_set cobbler/server_and_next_server "$RET"
	elif [ -n "$ipaddr" ]; then
		db_set cobbler/server_and_next_server "$ipaddr"
	fi

	db_input low cobbler/server_and_next_server || true
	db_go
fi

exit 0

