#!/bin/sh

set -e

. /usr/share/debconf/confmodule

_FILES="/etc/live/build-cron.conf /etc/live/build-cron.d/*"

for _FILE in "${_FILES}"
do
	if [ -e "${_FILE}" ]
	then
		_DEBCONF="true"

		. "${_FILE}" || true
	fi
done

if [ "${_DEBCONF}" = "true" ]
then
	# live-build-cron-images
	db_set live-build-cron/images-enable "${LIVE_BUILD_CRON_IMAGES}"
	db_set live-build-cron/images-directory "${LIVE_BUILD_CRON_IMAGES_DIRECTORY}"
	db_set live-build-cron/images-build "${LIVE_BUILD_CRON_IMAGES_BUILD}"

	db_set live-build-cron/images-daily-distributions "${LIVE_BUILD_CRON_IMAGES_DAILY_DISTRIBUTIONS}"
	db_set live-build-cron/images-daily-flavours "${LIVE_BUILD_CRON_IMAGES_DAILY_FLAVOURS}"

	db_set live-build-cron/images-weekly-distributions "${LIVE_BUILD_CRON_IMAGES_WEEKLY_DISTRIBUTIONS}"
	db_set live-build-cron/images-weekly-flavours "${LIVE_BUILD_CRON_IMAGES_WEEKLY_FLAVOURS}"

	db_set live-build-cron/images-monthly-distributions "${LIVE_BUILD_CRON_IMAGES_MONTHLY_DISTRIBUTIONS}"
	db_set live-build-cron/images-monthly-flavours "${LIVE_BUILD_CRON_IMAGES_MONTHLY_FLAVOURS}"

	db_set live-build-cron/images-source "${LIVE_BUILD_CRON_IMAGES_SOURCE}"

	# live-build-cron-manual
	db_set live-build-cron/manual-enable "${LIVE_BUILD_CRON_MANUAL}"
fi

db_settitle live-build-cron/title
db_input high live-build-cron/images-enable || true
db_go

db_get live-build-cron/images-enable
LIVE_BUILD_CRON_IMAGES="${RET}"

if [ "${LIVE_BUILD_CRON_IMAGES}" = "true" ]
then
	db_settitle live-build-cron/title
	db_input low live-build-cron/images-directory || true
	db_go

	db_settitle live-build-cron/title
	db_input low live-build-cron/images-build || true
	db_go

	for _BUILDS in daily weekly monthly
	do
		db_settitle live-build-cron/title
		db_input low live-build-cron/images-${_BUILDS}-distributions || true
		db_go

		db_settitle live-build-cron/title
		db_input low live-build-cron/images-${_BUILDS}-flavours || true
		db_go
	done

	db_settitle live-build-cron/title
	db_input low live-build-cron/images-source || true
	db_go

	db_settitle live-build-cron/title
	db_input low live-build-cron/images-cron || true
	db_go
fi

db_settitle live-build-cron/title
db_input high live-build-cron/manual-enable || true
db_go

db_get live-build-cron/manual-enable
LIVE_BUILD_CRON_MANUAL="${RET}"

if [ "${LIVE_BUILD_CRON_MANUAL}" = "true" ]
then
	db_settitle live-build-cron/title
	db_input low live-build-cron/manual-username || true
	db_go

	db_settitle live-build-cron/title
	db_input low live-build-cron/manual-directory || true
	db_go

	db_settitle live-build-cron/title
	db_input low live-build-cron/manual-cron || true
	db_go
fi

db_stop
