#!/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

if [ -e "${_TMPDIR}/debconf.default" ]
then
	. "${_TMPDIR}/debconf.default"
fi

if [ -e "${_PRESEED_FILE}" ]
then
	_PRESEED_FILES="${_PRESEED_FILE}"

	while [ -n "${_PRESEED_FILE}" ]
	do
		if grep -qs '^ *lxc-debconfig *lxc-debconfig/include-preseed-files' "${_PRESEED_FILE}"
		then
			# First expansion
			_INCLUDE_PRESEED_FILES="$(cat ${_PRESEED_FILE} | grep '^ *lxc-debconfig *lxc-debconfig/include-preseed-files' | awk '{ print $4 }')"

			if [ -e "${_INCLUDE_PRESEED_FILES}" ]
			then
				# Use included files before including files
				_PRESEED_FILES="${_INCLUDE_PRESEED_FILES} ${_PRESEED_FILES}"
			fi

			# Second (recursive) expansion
			_PRESEED_FILE="${_INCLUDE_PRESEED_FILES}"
		else
			break
		fi
	done

	for _PRESEED_FILE in ${_PRESEED_FILES}
	do
		# Apply user specified preseed files
		debconf-set-selections "${_PRESEED_FILE}"
	done
fi
