#!/bin/sh

## lxc-debconfig - LXC template for Debian and Debian Derivatives
## Copyright (C) 2006-2013 Daniel Baumann <mail@daniel-baumann.ch>
##
## This program comes with ABSOLUTELY NO WARRANTY; for details see COPYING.
## This is free software, and you are welcome to redistribute it
## under certain conditions; see COPYING for details.


set -e

# FIXME: this whole mess should be simplified at some point

. /usr/share/debconf/confmodule

Distribution ()
{
	db_get lxc-debconfig/distribution
	_DISTRIBUTION="${RET}" # select

	if [ -z "${_DISTRIBUTION}" ]
	then
		case "${_MODE}" in
			debian)
				db_subst lxc-debconfig/distribution CHOICES "Debian GNU/Linux 6 \"squeeze\", Debian GNU/Linux 7 \"wheezy\", Debian GNU/Linux 8 \"jessie\", Debian GNU/Linux unstable/sid"
				db_subst lxc-debconfig/distribution CHOICES_C "squeeze, wheezy, jessie, sid"

				db_set lxc-debconfig/distribution wheezy
				db_fset lxc-debconfig/distribution seen false
				;;

			progress-linux)
				db_subst lxc-debconfig/distribution CHOICES "Progress Linux 1 (artax), Progress Linux 1+ (artax-backports), Progress Linux 2 (baureo), Progress Linux 2+ (baureo-backports), Progress Linux 3 (chairon), Progress Linux 3+ (chairon-backports)"
				db_subst lxc-debconfig/distribution CHOICES_C "artax, artax-backports, baureo, baureo-backports, chairon, chairon-backports"

				db_set lxc-debconfig/distribution baureo
				db_fset lxc-debconfig/distribution seen false
				;;
		esac

		db_settitle lxc-debconfig/title
		db_input high lxc-debconfig/distribution || true
		db_go

		db_get lxc-debconfig/distribution
		_DISTRIBUTION="${RET}" # select
	fi

	echo "_DISTRIBUTION=\"${_DISTRIBUTION}\"" >> "${_TMPDIR}/debconf.default"
	export _DISTRIBUTION
}

Parent_distribution ()
{
	db_get lxc-debconfig/parent-distribution
	_PARENT_DISTRIBUTION="${RET}"

	if [ -z "${_PARENT_DISTRIBUTION}" ]
	then
		case "${_MODE}" in
			progress-linux)
				case "${_DISTRIBUTION}" in
					artax*)
						_PARENT_DISTRIBUTION="squeeze"
						;;

					baureo*)
						_PARENT_DISTRIBUTION="wheezy"
						;;

					chairon*)
						_PARENT_DISTRIBUTION="sid"
						;;
				esac
				;;

			*)
				_PARENT_DISTRIBUTION="${_DISTRIBUTION}"
				;;
		esac
	fi

	echo "_PARENT_DISTRIBUTION=\"${_PARENT_DISTRIBUTION}\"" >> "${_TMPDIR}/debconf.default"
	export _PARENT_DISTRIBUTION
}

Architecture ()
{
	_DEFAULT="${1}"
	_CHOICES="${2}"
	_CHOICES_C="${3}"

	db_get lxc-debconfig/architecture
	_ARCHITECTURE="${RET}" # select

	if [ -z "${_ARCHITECTURE}" ] && [ -n "${_CHOICES}" ]
	then
		db_subst lxc-debconfig/architecture CHOICES ${_CHOICES}
		db_subst lxc-debconfig/architecture CHOICES_C ${_CHOICES_C}

		db_set lxc-debconfig/architecture ${_DEFAULT}
		db_fset lxc-debconfig/distribution seen false

		db_settitle lxc-debconfig/title
		db_input high lxc-debconfig/architecture || true
		db_go

		db_get lxc-debconfig/architecture
		_ARCHITECTURE="${RET}" # select
	fi

	case "${_ARCHITECTURE}" in
		auto)
			_ARCHITECTURE="${_DEFAULT}"
			;;
	esac

	echo "_ARCHITECTURE=\"${_ARCHITECTURE}\"" >> "${_TMPDIR}/debconf.default"
	export _ARCHITECTURE
}

Archives ()
{
	db_get lxc-debconfig/archives
	_ARCHIVES="${RET}" # multiselect

	if [ -z "${_ARCHIVES}" ]
	then
		case "${_MODE}" in
			debian)
				case "${_PARENT_DISTRIBUTION}" in
					sid)
						db_subst lxc-debconfig/archives CHOICES "Experimental"
						db_subst lxc-debconfig/archives CHOICES_C "experimental"

						db_set lxc-debconfig/archives ""
						db_fset lxc-debconfig/archives seen false
						;;

					*)
						db_subst lxc-debconfig/archives CHOICES "Security, Updates, Backports, Proposed Updates"
						db_subst lxc-debconfig/archives CHOICES_C "${_DISTRIBUTION}-security, ${_DISTRIBUTION}-updates, ${_DISTRIBUTION}-backports, ${_DISTRIBUTION}-proposed-updates"

						case "${_PARENT_DISTRIBUTION}" in
							jessie|sid)
								db_set lxc-debconfig/archives "${_DISTRIBUTION}-security, ${_DISTRIBUTION}-updates"
								;;

							*)
								db_set lxc-debconfig/archives "${_DISTRIBUTION}-security, ${_DISTRIBUTION}-updates, ${_DISTRIBUTION}-backports"
								;;
						esac

						db_fset lxc-debconfig/archives seen false
						;;
				esac
				;;

			progress-linux)
				db_subst lxc-debconfig/archives CHOICES "staging, Security, Security (staging), Updates, Updates (staging), Backports, Backports (staging)"
				db_subst lxc-debconfig/archives CHOICES_C "${_DISTRIBUTION}-staging, ${_DISTRIBUTION}-security, ${_DISTRIBUTION}-security-staging, ${_DISTRIBUTION}-updates, ${_DISTRIBUTION}-updates-staging, ${_DISTRIBUTION}-backports, ${_DISTRIBUTION}-backports-staging"

				db_set lxc-debconfig/archives "${_DISTRIBUTION}-security, ${_DISTRIBUTION}-updates, ${_DISTRIBUTION}-backports"
				db_fset lxc-debconfig/archives seen false
				;;
		esac

		db_settitle lxc-debconfig/title
		db_input high lxc-debconfig/archives || true
		db_go

		db_get lxc-debconfig/archives
		_ARCHIVES="${RET}" # multiselect
	fi

	_ARCHIVES="$(echo ${_ARCHIVES} | sed -e 's|, | |g')"

	echo "_ARCHIVES=\"${_ARCHIVES}\"" >> "${_TMPDIR}/debconf.default"
	export _ARCHIVES
}

Parent_archives ()
{
	db_get lxc-debconfig/parent-archives
	_PARENT_ARCHIVES="${RET}" # multiselect (w/o empty)

	if [ -z "${_PARENT_ARCHIVES}" ]
	then
		case "${_MODE}" in
			progress-linux)
				db_subst lxc-debconfig/parent-archives CHOICES "Security, Updates , Backports, Proposed Updates"
				db_subst lxc-debconfig/parent-archives CHOICES_C "${_DISTRIBUTION}-security, ${_DISTRIBUTION}-updates, ${_DISTRIBUTION}-backports, ${_DISTRIBUTION}-proposed-updates"

				db_set lxc-debconfig/parent-archives "${_DISTRIBUTION}-security, ${_DISTRIBUTION}-updates, ${_DISTRIBUTION}-backports"
				db_fset lxc-debconfig/parent-archives seen false

				db_settitle lxc-debconfig/title
				db_input high lxc-debconfig/parent-archives || true
				db_go
				;;

			*)
				db_subst lxc-debconfig/parent-archives CHOICES "Security, Updates , Backports, Proposed Updates"
				db_subst lxc-debconfig/parent-archives CHOICES_C "${_ARCHIVES}"

				db_set lxc-debconfig/parent-archives "${_ARCHIVES}"
				db_fset lxc-debconfig/parent-archives seen true
				;;
		esac

		db_get lxc-debconfig/parent-archives
		_PARENT_ARCHIVES="${RET}" # multiselect (w/o empty)

		if [ -z "${_PARENT_ARCHIVES}" ]
		then
			case "${_MODE}" in
				progress-linux)
					_PARENT_ARCHIVES="${_DISTRIBUTION}-security, ${_DISTRIBUTION}-updates, ${_DISTRIBUTION}-backports"
					;;

				*)
					_PARENT_ARCHIVES="${_ARCHIVES}"
					;;
			esac
		fi
	fi

	_PARENT_ARCHIVES="$(echo ${_PARENT_ARCHIVES} | sed -e 's|, | |g')"

	echo "_PARENT_ARCHIVES=\"${_PARENT_ARCHIVES}\"" >> "${_TMPDIR}/debconf.default"
	export _PARENT_ARCHIVES
}

Mirror ()
{
	db_get lxc-debconfig/mirror
	_MIRROR="${RET}"

	if [ -z "${_MIRROR}" ]
	then
		case "${_MODE}" in
			debian)
				db_set lxc-debconfig/mirror http://http.debian.net/debian/
				db_fset lxc-debconfig/mirror seen false
				;;

			progress-linux)
				db_set lxc-debconfig/mirror http://cdn.archive.progress-linux.org/packages/
				db_fset lxc-debconfig/mirror seen false
				;;
		esac

		db_settitle lxc-debconfig/title
		db_input high lxc-debconfig/mirror || true
		db_go

		db_get lxc-debconfig/mirror
		_MIRROR="${RET}" # string (w/o empty)

		if [ -z "${_MIRROR}" ]
		then
			case "${_MODE}" in
				debian)
					_MIRROR="http://http.debian.net/debian/"
					;;

				progress-linux)
					_MIRROR="http://cdn.archive.progress-linux.org/packages/"
					;;
			esac
		fi
	fi

	echo "_MIRROR=\"${_MIRROR}\"" >> "${_TMPDIR}/debconf.default"
	export _MIRROR
}

Mirror_security ()
{
	db_get lxc-debconfig/mirror-security
	_MIRROR_SECURITY="${RET}" # string (w/o empty)

	if [ -z "${_MIRROR_SECURITY}" ]
	then
		case "${_MODE}" in
			debian)
				db_set lxc-debconfig/mirror-security http://security.debian.org/
				db_fset lxc-debconfig/mirror-security seen false
				;;

			*)
				db_set lxc-debconfig/mirror-security ${_MIRROR}
				db_fset lxc-debconfig/mirror-security seen true
				;;
		esac

		db_settitle lxc-debconfig/title
		db_input high lxc-debconfig/mirror-security || true
		db_go

		db_get lxc-debconfig/mirror-security
		_MIRROR_SECURITY="${RET}" # string (w/o empty)

		if [ -z "${_MIRROR_SECURITY}" ]
		then
			case "${_MODE}" in
				debian)
					_MIRROR_SECURITY="http://security.debian.org/"
					;;

				*)
					_MIRROR_SECURITY="${_MIRROR}"
					;;
			esac
		fi
	fi

	echo "_MIRROR_SECURITY=\"${_MIRROR_SECURITY}\"" >> "${_TMPDIR}/debconf.default"
	export _MIRROR_SECURITY
}

Mirror_backports ()
{
	db_get lxc-debconfig/mirror-backports
	_MIRROR_BACKPORTS="${RET}" # string (w/o empty)

	if [ -z "${_MIRROR_BACKPORTS}" ]
	then
		case "${_MODE}" in
			debian)
				case "${_DISTRIBUTION}" in
					squeeze)
						db_set lxc-debconfig/mirror-backports http://http.debian.net/debian-backports/
						db_fset lxc-debconfig/mirror-backports seen false
						;;

					*)
						db_set lxc-debconfig/mirror-backports http://http.debian.net/debian/
						db_fset lxc-debconfig/mirror-backports seen true
						;;
				esac
				;;

			*)
				db_set lxc-debconfig/mirror-backports ${_MIRROR}
				db_fset lxc-debconfig/mirror-backports seen true
				;;
		esac

		db_settitle lxc-debconfig/title
		db_input high lxc-debconfig/mirror-backports || true
		db_go

		db_get lxc-debconfig/mirror-backports
		_MIRROR_BACKPORTS="${RET}" # string (w/o empty)

		if [ -z "${_MIRROR_BACKPORTS}" ]
		then
			case "${_MODE}" in
				debian)
					case "${_DISTRIBUTION}" in
						squeeze)
							_MIRROR_BACKPORTS="http://http.debian.net/debian-backports/"
							;;

						*)
							_MIRROR_BACKPORTS="http://http.debian.net/debian/"
							;;
					esac
					;;

				*)
					_MIRROR_BACKPORTS="${_MIRROR}"
					;;
			esac
		fi
	fi

	echo "_MIRROR_BACKPORTS=\"${_MIRROR_BACKPORTS}\"" >> "${_TMPDIR}/debconf.default"
	export _MIRROR_BACKPORTS
}

Parent_mirror ()
{
	db_get lxc-debconfig/parent-mirror
	_PARENT_MIRROR="${RET}" # string (w/o empty)

	if [ -z "${_PARENT_MIRROR}" ]
	then
		case "${_MODE}" in
			progress-linux)
				db_set lxc-debconfig/parent-mirror http://ftp.debian.org/debian/
				db_fset lxc-debconfig/parent-mirror seen false

				db_settitle lxc-debconfig/title
				db_input high lxc-debconfig/parent-mirror || true
				db_go
				;;

			*)
				db_set lxc-debconfig/parent-mirror ${_MIRROR}
				db_fset lxc-debconfig/parent-mirror seen true
				;;
		esac

		db_get lxc-debconfig/parent-mirror
		_PARENT_MIRROR="${RET}" # string (w/o empty)

		if [ -z "${_PARENT_MIRROR}" ]
		then
			case "${_MODE}" in
				progress-linux)
					_PARENT_MIRROR="http://ftp.debian.org/debian/"
					;;

				*)
					_PARENT_MIRROR="${_MIRROR}"
					;;
			esac
		fi
	fi

	echo "_PARENT_MIRROR=\"${_PARENT_MIRROR}\"" >> "${_TMPDIR}/debconf.default"
	export _PARENT_MIRROR
}

Parent_mirror_security ()
{
	db_get lxc-debconfig/parent-mirror-security
	_PARENT_MIRROR_SECURITY="${RET}" # string (w/o empty)

	if [ -z "${_PARENT_MIRROR_SECURITY}" ]
	then
		case "${_MODE}" in
			progress-linux)
				db_set lxc-debconfig/parent-mirror-security http://security.debian.org/
				db_fset lxc-debconfig/parent-mirror-security seen false

				db_settitle lxc-debconfig/title
				db_input high lxc-debconfig/parent-mirror-security || true
				db_go
				;;

			*)
				db_set lxc-debconfig/parent-mirror-security ${_MIRROR_SECURITY}
				db_fset lxc-debconfig/parent-mirror-security seen true
				;;
		esac

		db_get lxc-debconfig/parent-mirror-security
		_PARENT_MIRROR_SECURITY="${RET}" # string (w/o empty)

		if [ -z "${_PARENT_MIRROR_SECURITY}" ]
		then
			case "${_MODE}" in
				progress-linux)
					_PARENT_MIRROR_SECURITY="http://security.debian.org/"
					;;

				*)
					_PARENT_MIRROR_SECURITY="${_MIRROR_SECURITY}"
					;;
			esac
		fi
	fi

	echo "_PARENT_MIRROR_SECURITY=\"${_PARENT_MIRROR_SECURITY}\"" >> "${_TMPDIR}/debconf.default"
	export _PARENT_MIRROR_SECURITY
}

Parent_mirror_backports ()
{
	db_get lxc-debconfig/parent-mirror-backports
	_PARENT_MIRROR_BACKPORTS="${RET}" # string (w/o empty)

	if [ -z "${_PARENT_MIRROR_BACKPORTS}" ]
	then
		case "${_MODE}" in
			progress-linux)
				case "${_PARENT_DISTRIBUTION}" in
					squeeze)
						db_set lxc-debconfig/parent-mirror-backports http://backports.debian.org/debian-backports/
						db_fset lxc-debconfig/parent-mirror-backports seen false
						;;

					*)
						db_set lxc-debconfig/parent-mirror-backports ${_PARENT_MIRROR}
						db_fset lxc-debconfig/parent-mirror-backports seen true
						;;
				esac

				db_settitle lxc-debconfig/title
				db_input high lxc-debconfig/parent-mirror-backports || true
				db_go
				;;

			*)
				db_set lxc-debconfig/parent-mirror-backports ${_MIRROR_BACKPORTS}
				db_fset lxc-debconfig/parent-mirror-backports seen true
				;;
		esac

		db_get lxc-debconfig/parent-mirror-backports
		_PARENT_MIRROR_BACKPORTS="${RET}" # string (w/o empty)

		if [ -z "${_PARENT_MIRROR_BACKPORTS}" ]
		then
			case "${_MODE}" in
				progress-linux)
					case "${_PARENT_DISTRIBUTION}" in
						squeeze)
							_PARENT_MIRROR_BACKPORTS="http://backports.debian.org/debian-backports/"
							;;

						*)
							_PARENT_MIRROR_BACKPORTS="${_PARENT_MIRROR}"
							;;
					esac
					;;

				*)
					_PARENT_MIRROR_BACKPORTS="${_MIRROR_BACKPORTS}"
					;;
			esac
		fi
	fi

	echo "_PARENT_MIRROR_BACKPORTS=\"${_PARENT_MIRROR_BACKPORTS}\"" >> "${_TMPDIR}/debconf.default"
	export _PARENT_MIRROR_BACKPORTS
}

Archive_areas ()
{
	db_get lxc-debconfig/archive-areas
	_ARCHIVE_AREAS="${RET}"

	if [ -z "${_ARCHIVE_AREAS}" ]
	then
		case "${_MODE}" in
			progress-linux)
				db_subst lxc-debconfig/archive-areas CHOICES "main, contrib, non-free"

				db_set lxc-debconfig/archive-areas "main, contrib, non-free"
				db_fset lxc-debconfig/archive-areas seen false
				;;

			*)
				db_subst lxc-debconfig/archive-areas CHOICES "main, contrib, non-free"

				db_set lxc-debconfig/archive-areas "main"
				db_fset lxc-debconfig/archive-areas seen false
				;;
		esac

		db_settitle lxc-debconfig/title
		db_input high lxc-debconfig/archive-areas || true
		db_go

		db_get lxc-debconfig/archive-areas
		_ARCHIVE_AREAS="${RET}" # multiselect (w/o empty)

		if [ -z "${_ARCHIVE_AREAS}" ]
		then
			case "${_MODE}" in
				debian)
					_ARCHIVE_AREAS="main"
					;;

				progress-linux)
					_ARCHIVE_AREAS="main, contrib, non-free"
					;;
			esac
		fi
	fi

	_ARCHIVE_AREAS="$(echo ${_ARCHIVE_AREAS} | sed -e 's| ||g')"

	echo "_ARCHIVE_AREAS=\"${_ARCHIVE_AREAS}\"" >> "${_TMPDIR}/debconf.default"
	export _ARCHIVE_AREAS
}

Parent_archive_areas ()
{
	db_get lxc-debconfig/parent-archive-areas
	_PARENT_ARCHIVE_AREAS="${RET}" # multiselect (w/o empty)

	if [ -z "${_PARENT_ARCHIVE_AREAS}" ]
	then
		case "${_MODE}" in
			progress-linux)
				db_subst lxc-debconfig/parent-archive-areas CHOICES "main, contrib, non-free"

				db_set lxc-debconfig/parent-archive-areas "main, contrib, non-free"
				db_fset lxc-debconfig/parent-archive-areas seen false

				db_settitle lxc-debconfig/title
				db_input high lxc-debconfig/parent-archive-areas || true
				db_go
				;;

			*)
				db_subst lxc-debconfig/parent-archive-areas CHOICES "${_ARCHIVE_AREAS}"

				db_set lxc-debconfig/parent-archive-areas "${_ARCHIVE_AREAS}"
				db_fset lxc-debconfig/parent-archive-areas seen true
				;;
		esac

		db_get lxc-debconfig/parent-archive-areas
		_PARENT_ARCHIVE_AREAS="${RET}" # multiselect (w/o empty)

		if [ -z "${_PARENT_ARCHIVE_AREAS}" ]
		then
			case "${_MODE}" in
				progress-linux)
					_PARENT_ARCHIVE_AREAS="main, contrib, non-free"
					;;

				*)
					_PARENT_ARCHIVE_AREAS="${_ARCHIVE_AREAS}"
					;;
			esac
		fi
	fi

	_PARENT_ARCHIVE_AREAS="$(echo ${_PARENT_ARCHIVE_AREAS} | sed -e 's| ||g')"

	echo "_PARENT_ARCHIVE_AREAS=\"${_PARENT_ARCHIVE_AREAS}\"" >> "${_TMPDIR}/debconf.default"
	export _PARENT_ARCHIVE_AREAS
}

Packages ()
{
	db_get lxc-debconfig/packages
	_PACKAGES="${RET}" # string (w/ empty)

	if [ -z "${_PACKAGES}" ]
	then
		db_settitle lxc-debconfig/title
		db_input high lxc-debconfig/packages || true
		db_go

		db_get lxc-debconfig/packages
		_PACKAGES="${RET}" # string (w/ empty)
	fi

	echo "_PACKAGES=\"${_PACKAGES}\"" >> "${_TMPDIR}/debconf.default"
	export _PACKAGES
}

Local_archives ()
{
	_NUMBER="0"

	while db_get lxc-debconfig/archives${_NUMBER}/repository && [ "${RET}" ]
	do
		mkdir -p "${_TMPDIR}/sources.list.d"

		_REPOSITORY="${RET#deb }"

		_LIST="archive${_NUMBER}.list"
		if db_get lxc-debconfig/archives${_NUMBER}/list
		then
			_LIST="$(basename ${RET} .list).list"
		fi

		_COMMENT=""
		if db_get lxc-debconfig/archives${_NUMBER}/comment
		then
			_COMMENT="${RET}"

			echo "# ${_COMMENT}" > "${_TMPDIR}/sources.list.d/${_LIST}"
		fi

		echo "deb ${_REPOSITORY}" >> "${_TMPDIR}/sources.list.d/${_LIST}"

		if db_get lxc-debconfig/archives${_NUMBER}/source && [ "$RET" = true ]
		then
			echo "deb-src ${_REPOSITORY}" >> "${_TMPDIR}/sources.list.d/${_LIST}"
		fi

		_KEY=""
		if db_get lxc-debconfig/archives${_NUMBER}/key
		then
			_KEY="${RET}"

			wget -q "${_KEY}" -O "${_TMPDIR}/sources.list.d/$(basename ${_LIST} .list).key"
		fi

		_PREFERENCES_PACKAGE=""
		_PREFERENCES_PIN=""
		_PREFERENCES_PIN_PRIORITY=""
		if db_get lxc-debconfig/archives${_NUMBER}/preferences-package
		then
			_PREFERENCES_PACKAGE="${RET}"

			if db_get lxc-debconfig/archives${_NUMBER}/preferences-pin
			then
				_PREFERENCES_PIN="${RET}"
			fi

			if db_get lxc-debconfig/archives${_NUMBER}/preferences-pin-priority
			then
				_PREFERENCES_PIN_PRIORITY="${RET}"
			fi

			if [ -n "${_PREFERENCES_PACKAGE}" ] || [ -n "${_PREFERENCES_PIN}" ] || [ -n "${_PREFERENCES_PIN_PRIORITY}" ]
			then

cat > "${_TMPDIR}/sources.list.d/$(basename ${_LIST} .list).pref" << EOF
Package: ${_PREFERENCES_PACKAGE}
Pin: ${_PREFERENCES_PIN}
Pin-Priority: ${_PREFERENCES_PIN_PRIORITY}
EOF

			fi
		fi

		_NUMBER="$((${_NUMBER} + 1))"
	done
}

Root_password ()
{
	if ! db_get live-debconfig/passwd/root-password-crypted
	then
		if db_get live-debconfig/passwd/root-password
		then
			_ROOT_PASSWORD="${RET}" # string (w/o empty)
		fi

		if [ -z "${_ROOT_PASSWORD}" ]
		then
			# Create a random password as suggestion for the user
			_RANDOM_PASSWORD="$(dd if=/dev/urandom bs=6 count=1 2> /dev/null | base64)"

			db_set live-debconfig/passwd/root-password ${_RANDOM_PASSWORD}
			db_fset live-debconfig/passwd/root-password seen false

			db_settitle lxc-debconfig/title
			db_input high live-debconfig/passwd/root-password || true
			db_go

			db_get live-debconfig/passwd/root-password
			_ROOT_PASSWORD="${RET}"

			if [ -z "${_ROOT_PASSWORD}" ]
			then
				# User did set not set a password, falling back to random password
				_ROOT_PASSWORD="${_RANDOM_PASSWORD}"

				echo "_ROOT_RANDOM_PASSWORD=\"true\"" >> "${_TMPDIR}/debconf.default"
			fi
		fi

		echo "_ROOT_PASSWORD=\"${_ROOT_PASSWORD}\"" >> "${_TMPDIR}/debconf.default"
	fi
}

Network ()
{
	_NUMBER="0"

	while db_get live-debconfig/ifupdown/eth${_NUMBER}-ipv4-method && [ "${RET}" ]
	do
		if db_get live-debconfig/ifupdown/eth${_NUMBER}-ipv4-comment
		then
			eval LXC_DEBCONFIG_ETH${_NUMBER}_IPV4_COMMENT="\"${RET}\"" # string (w/)
		fi

		if db_get lxc-debconfig/eth${_NUMBER}-bridge
		then
			eval LXC_DEBCONFIG_ETH${_NUMBER}_BRIDGE="\"${RET}\"" # string (w/o empty)
		fi

		if db_get lxc-debconfig/eth${_NUMBER}-mac
		then
			eval LXC_DEBCONFIG_ETH${_NUMBER}_MAC="\"${RET}\"" # string (w/o empty)
		fi

		if db_get lxc-debconfig/eth${_NUMBER}-veth
		then
			eval LXC_DEBCONFIG_ETH${_NUMBER}_VETH="\"${RET}\"" # string (w/ empty)
		fi

		_NUMBER="$((${_NUMBER} + 1))"
	done

	# FIXME: this is ugly
	if [ "${_NUMBER}" -gt 0 ]
	then
		LXC_DEBCONFIG_ETH_NUMBER="$((${_NUMBER} - 1))"
	else
		LXC_DEBCONFIG_ETH_NUMBER="${_NUMBER}"
	fi

	if [ -z ${LXC_DEBCONFIG_ETH0_BRIDGE} ]
	then
		# FIXME: do guess the default bridge device
		db_set lxc-debconfig/eth0-bridge br0
		db_fset lxc-debconfig/eth0-bridge seen false

		db_settitle lxc-debconfig/title
		db_input high lxc-debconfig/eth0-bridge || true
		db_go

		db_get lxc-debconfig/eth0-bridge
		LXC_DEBCONFIG_ETH0_BRIDGE="${RET:-br0}"
	fi

	if [ -z "${LXC_DEBCONFIG_ETH0_MAC}" ]
	then
		if [ -x /usr/bin/hexdump ]
		then
			# Create a random mac as suggestion for the user
			_RANDOM_MAC="00:FF$(dd bs=1 count=4 if=/dev/urandom 2> /dev/null | hexdump -v -e '/1 ":%02X"')"
		else
			# FIXME: do guess a default mac adrress based on existing container macs or something
			_RANDOM_MAC="00:FF:00:00:00:01"
		fi

		db_set lxc-debconfig/eth0-mac ${_RANDOM_MAC}
		db_fset lxc-debconfig/eth0-mac seen false

		db_settitle lxc-debconfig/title
		db_input high lxc-debconfig/eth0-mac || true
		db_go

		db_get lxc-debconfig/eth0-mac
		LXC_DEBCONFIG_ETH0_MAC="${RET:-${_RANDOM_MAC}}"
	fi

	echo "LXC_DEBCONFIG_ETH_NUMBER=\"${LXC_DEBCONFIG_ETH_NUMBER}\"" >> "${_TMPDIR}/debconf.default"

	for _NUMBER in $(seq 0 ${LXC_DEBCONFIG_ETH_NUMBER})
	do
		eval _COMMENT="$`echo LXC_DEBCONFIG_ETH${_NUMBER}_IPV4_COMMENT`"
		echo "LXC_DEBCONFIG_ETH${_NUMBER}_IPV4_COMMENT=\"${_COMMENT}\"" >> "${_TMPDIR}/debconf.default"

		eval _BRIDGE="$`echo LXC_DEBCONFIG_ETH${_NUMBER}_BRIDGE`"
		echo "LXC_DEBCONFIG_ETH${_NUMBER}_BRIDGE=\"${_BRIDGE}\"" >> "${_TMPDIR}/debconf.default"

		eval _MAC="$`echo LXC_DEBCONFIG_ETH${_NUMBER}_MAC`"
		echo "LXC_DEBCONFIG_ETH${_NUMBER}_MAC=\"${_MAC}\"" >> "${_TMPDIR}/debconf.default"

		eval _VETH="$`echo LXC_DEBCONFIG_ETH${_NUMBER}_VETH`"
		echo "LXC_DEBCONFIG_ETH${_NUMBER}_VETH=\"${_VETH}\"" >> "${_TMPDIR}/debconf.default"
	done
}

Auto ()
{
	db_get lxc-debconfig/auto
	_AUTO="${RET}" # boolean

	if [ -z "${_AUTO}" ]
	then
		db_set lxc-debconfig/auto false
		db_fset lxc-debconfig/auto seen false

		db_settitle lxc-debconfig/title
		db_input high lxc-debconfig/auto || true
		db_go

		db_get lxc-debconfig/auto
		_AUTO="${RET}"
	fi

	echo "_AUTO=\"${_AUTO}\"" >> "${_TMPDIR}/debconf.default"
	export _AUTO
}

Internal_options ()
{
	if db_get lxc-debconfig/lxc-debconfig-with-live-debconfig
	then
		LXC_DEBCONFIG_WITH_LIVE_DEBCONFIG="${RET}" # boolean
	fi

	if db_get lxc-debconfig/apt-recommends
	then
		_APT_RECOMMENDS="${RET}" # boolean (w/ empty)
	fi

	echo "_APT_RECOMMENDS=\"${_APT_RECOMMENDS}\"" >> "${_TMPDIR}/debconf.default"

	if db_get lxc-debconfig/debconf-frontend
	then
		_DEBCONF_FRONTEND="${RET}" # select
	fi

	_DEBCONF_FRONTEND="${_DEBCONF_FRONTEND:-dialog}"
	echo "_DEBCONF_FRONTEND=\"${_DEBCONF_FRONTEND}\"" >> "${_TMPDIR}/debconf.default"

	if db_get lxc-debconfig/debconf-priority
	then
		_DEBCONF_PRIORITY="${RET}" # select
	fi

	_DEBCONF_PRIORITY="${_DEBCONF_PRIORITY:-high}"
	echo "_DEBCONF_PRIORITY=\"${_DEBCONF_PRIORITY}\"" >> "${_TMPDIR}/debconf.default"

	if db_get lxc-debconfig/late-command
	then
		_LATE_COMMAND="${RET}" # string (w/ empty)
	fi

	echo "_LATE_COMMAND=\"${_LATE_COMMAND}\"" >> "${_TMPDIR}/debconf.default"

	if db_get lxc-debconfig/late-host-command
	then
		_LATE_HOST_COMMAND="${RET}" # string (w/ empty)
	fi

	echo "_LATE_HOST_COMMAND=\"${_LATE_HOST_COMMAND}\"" >> "${_TMPDIR}/debconf.default"

	if db_get lxc-debconfig/capabilities
	then
		_CAPABILITIES="${RET}" # string (w/ empty)
	fi

	echo "_CAPABILITIES=\"${_CAPABILITIES}\"" >> "${_TMPDIR}/debconf.default"

	if db_get lxc-debconfig/initsystem
	then
		_INITSYSTEM="${RET}" # string (w/o empty)
	fi

	_INITSYSTEM="${_INITSYSTEM:-sysvinit}"

	echo "_INITSYSTEM=\"${_INITSYSTEM}\"" >> "${_TMPDIR}/debconf.default"

	if db_get lxc-debconfig/mount-proc/entry
	then
		LXC_DEBCONFIG_MOUNT_PROC_ENTRY="${RET}"
	fi

	case "${_MODE}" in
		progress-linux)
			LXC_DEBCONFIG_MOUNT_PROC_ENTRY="${LXC_DEBCONFIG_MOUNT_PROC_ENTRY:-proc proc proc ro,nodev,noexec,nosuid,hidepid=2 0 0}"
			;;

		*)
			LXC_DEBCONFIG_MOUNT_PROC_ENTRY="${LXC_DEBCONFIG_MOUNT_PROC_ENTRY:-proc proc proc ro,nodev,noexec,nosuid 0 0}"
			;;
	esac

	echo "LXC_DEBCONFIG_MOUNT_PROC_ENTRY=\"${LXC_DEBCONFIG_MOUNT_PROC_ENTRY}\"" >> "${_TMPDIR}/debconf.default"

	if db_get lxc-debconfig/mount-sysfs/entry
	then
		LXC_DEBCONFIG_MOUNT_SYSFS_ENTRY="${RET}"
	fi

	LXC_DEBCONFIG_MOUNT_SYSFS_ENTRY="${LXC_DEBCONFIG_MOUNT_SYSFS_ENTRY:-sysfs sys sysfs ro 0 0}"

	echo "LXC_DEBCONFIG_MOUNT_SYSFS_ENTRY=\"${LXC_DEBCONFIG_MOUNT_SYSFS_ENTRY}\"" >> "${_TMPDIR}/debconf.default"

	_NUMBER="0"

	while db_get lxc-debconfig/mount${_NUMBER}/entry && [ "${RET}" ]
	do
		eval LXC_DEBCONFIG_MOUNT${_NUMBER}_ENTRY="\"${RET}\"" # string (w/o empty)

		if db_get lxc-debconfig/mount${_NUMBER}/comment
		then
			eval LXC_DEBCONFIG_MOUNT${_NUMBER}_COMMENT="\"${RET}\"" # string (w/ empty)
		fi

		_NUMBER="$((${_NUMBER} + 1))"
	done

	# FIXME: this is ugly
	if [ "${_NUMBER}" -gt 0 ]
	then
		LXC_DEBCONFIG_MOUNT_NUMBER="$((${_NUMBER} - 1))"
	else
		LXC_DEBCONFIG_MOUNT_NUMBER="${_NUMBER}"
	fi

	echo "LXC_DEBCONFIG_MOUNT_NUMBER=\"${LXC_DEBCONFIG_MOUNT_NUMBER}\"" >> "${_TMPDIR}/debconf.default"

	for _NUMBER in $(seq 0 ${LXC_DEBCONFIG_MOUNT_NUMBER})
	do
		eval _COMMENT="$`echo LXC_DEBCONFIG_MOUNT${_NUMBER}_COMMENT`"
		echo "LXC_DEBCONFIG_MOUNT${_NUMBER}_COMMENT=\"${_COMMENT}\"" >> "${_TMPDIR}/debconf.default"

		eval _ENTRY="$`echo LXC_DEBCONFIG_MOUNT${_NUMBER}_ENTRY`"
		echo "LXC_DEBCONFIG_MOUNT${_NUMBER}_ENTRY=\"${_ENTRY}\"" >> "${_TMPDIR}/debconf.default"
	done
}

Distribution
Parent_distribution

case "$(dpkg --print-architecture)" in
	amd64)
		# Usage: Architecture Default CHOICES CHOICES_C
		Architecture "amd64" "Automatic, 32-bit PC (i386), 64-bit PC (amd64)" "auto, i386, amd64"
		;;

	i386)
		# Usage: Architecture Default CHOICES CHOICES_C
		Architecture "i386"
		;;
esac

Archives
Parent_archives

Mirror

for _ARCHIVE in ${_ARCHIVES}
do
	case "${_ARCHIVE}" in
		*-security*)
			Mirror_security
			;;

		*-backports*)
			Mirror_backports
			;;
	esac
done

Parent_mirror

for _PARENT_ARCHIVE in ${_PARENT_ARCHIVES}
do
	case "${_PARENT_ARCHIVE}" in
		*-security)
			Parent_mirror_security
			;;

		*-backports)
			Parent_mirror_backports
			;;
	esac
done

Archive_areas
Parent_archive_areas

Packages
Local_archives

Root_password
Network
Auto

Internal_options

db_stop
