#!/bin/sh

set -e

. /usr/share/debconf/confmodule

_FILE="/etc/live/build-cron.d/images.conf"

case "${1}" in
	configure)
		db_get live-build-cron/images-enable
		LIVE_BUILD_CRON_IMAGES="${RET}" # boolean

		db_get live-build-cron/images-directory
		LIVE_BUILD_CRON_IMAGES_DIRECTORY="${RET:-/srv/debian.net/live/cdimage}" # string (w/o empty)

		db_get live-build-cron/images-build
		LIVE_BUILD_CRON_IMAGES_BUILD="${RET}" # select

		db_get live-build-cron/images-daily-distributions
		LIVE_BUILD_CRON_IMAGES_DAILY_DISTRIBUTIONS="$(echo ${RET} | sed -e 's|,||g')" # multiselect

		db_get live-build-cron/images-daily-flavours
		LIVE_BUILD_CRON_IMAGES_DAILY_FLAVOURS="$(echo ${RET} | sed -e 's|,||g')" # multiselect

		db_get live-build-cron/images-weekly-distributions
		LIVE_BUILD_CRON_IMAGES_WEEKLY_DISTRIBUTIONS="$(echo ${RET} | sed -e 's|,||g')" # multiselect

		db_get live-build-cron/images-weekly-flavours
		LIVE_BUILD_CRON_IMAGES_WEEKLY_FLAVOURS="$(echo ${RET} | sed -e 's|,||g')" # multiselect

		db_get live-build-cron/images-monthly-distributions
		LIVE_BUILD_CRON_IMAGES_MONTHLY_DISTRIBUTIONS="$(echo ${RET} | sed -e 's|,||g')" # multiselect

		db_get live-build-cron/images-monthly-flavours
		LIVE_BUILD_CRON_IMAGES_MONTHLY_FLAVOURS="$(echo ${RET} | sed -e 's|,||g')" # multiselect

		db_get live-build-cron/images-source
		LIVE_BUILD_CRON_IMAGES_SOURCE="${RET}" # boolean

		db_get live-build-cron/images-cron
		_CRON="${RET}" # string (w empty)

		db_stop

		if [ ! -e "${_FILE}" ]
		then

			mkdir -p "$(dirname ${_FILE})"

cat > "${_FILE}" << EOF
# /etc/live/build-cron.d/images.conf

LIVE_BUILD_CRON_IMAGES="${LIVE_BUILD_CRON_IMAGES}"
LIVE_BUILD_CRON_IMAGES_DIRECTORY="${LIVE_BUILD_CRON_IMAGES_DIRECTORY}"
LIVE_BUILD_CRON_IMAGES_BUILD="${LIVE_BUILD_CRON_IMAGES_BUILD}"

LIVE_BUILD_CRON_IMAGES_DAILY_DISTRIBUTIONS="${LIVE_BUILD_CRON_IMAGES_DAILY_DISTRIBUTIONS}"
LIVE_BUILD_CRON_IMAGES_DAILY_FLAVOURS="${LIVE_BUILD_CRON_IMAGES_DAILY_FLAVOURS}"

LIVE_BUILD_CRON_IMAGES_WEEKLY_DISTRIBUTIONS="${LIVE_BUILD_CRON_IMAGES_WEEKLY_DISTRIBUTIONS}"
LIVE_BUILD_CRON_IMAGES_WEEKLY_FLAVOURS="${LIVE_BUILD_CRON_IMAGES_WEEKLY_FLAVOURS}"

LIVE_BUILD_CRON_IMAGES_MONTHLY_DISTRIBUTIONS="${LIVE_BUILD_CRON_IMAGES_MONTHLY_DISTRIBUTIONS}"
LIVE_BUILD_CRON_IMAGES_MONTHLY_FLAVOURS="${LIVE_BUILD_CRON_IMAGES_MONTHLY_FLAVOURS}"

export LIVE_BUILD_CRON_IMAGES LIVE_BUILD_CRON_IMAGES_DIRECTORY
export LIVE_BUILD_CRON_IMAGES_DAILY_DISTRIBUTIONS LIVE_BUILD_CRON_IMAGES_DAILY_FLAVOURS
export LIVE_BUILD_CRON_IMAGES_WEEKLY_DISTRIBUTIONS LIVE_BUILD_CRON_IMAGES_WEEKLY_FLAVOURS
export LIVE_BUILD_CRON_IMAGES_MONTHLY_DISTRIBUTIONS LIVE_BUILD_CRON_IMAGES_MONTHLY_FLAVOURS
export LIVE_BUILD_CRON_IMAGES_SOURCE
EOF

		fi

		cp -a -f "${_FILE}" "${_FILE}.tmp"

		# If the admin deleted or commented some variables but then set
		# them via debconf, (re-)add them to the config file.

		test -z "${LIVE_BUILD_CRON_IMAGES}" || \
		grep -Eq '^ *LIVE_BUILD_CRON_IMAGES=' "${_FILE}" || \
		echo "LIVE_BUILD_CRON_IMAGES=" >> "${_FILE}"

		test -z "${LIVE_BUILD_CRON_IMAGES_DIRECTORY}" || \
		grep -Eq '^ *LIVE_BUILD_CRON_IMAGES_DIRECTORY=' "${_FILE}" || \
		echo "LIVE_BUILD_CRON_IMAGES_DIRECTORY=" >> "${_FILE}"

		test -z "${LIVE_BUILD_CRON_IMAGES_BUILD}" || \
		grep -Eq '^ *LIVE_BUILD_CRON_IMAGES_BUILD=' "${_FILE}" || \
		echo "LIVE_BUILD_CRON_IMAGES_BUILD=" >> "${_FILE}"

		test -z "${LIVE_BUILD_CRON_IMAGES_DAILY_DISTRIBUTIONS}" || \
		grep -Eq '^ *LIVE_BUILD_CRON_IMAGES_DAILY_DISTRIBUTIONS=' "${_FILE}" || \
		echo "LIVE_BUILD_CRON_IMAGES_DAILY_DISTRIBUTIONS=" >> "${_FILE}"

		test -z "${LIVE_BUILD_CRON_IMAGES_DAILY_FLAVOURS}" || \
		grep -Eq '^ *LIVE_BUILD_CRON_IMAGES_DAILY_FLAVOURS=' "${_FILE}" || \
		echo "LIVE_BUILD_CRON_IMAGES_DAILY_FLAVOURS=" >> "${_FILE}"

		test -z "${LIVE_BUILD_CRON_IMAGES_WEEKLY_DISTRIBUTIONS}" || \
		grep -Eq '^ *LIVE_BUILD_CRON_IMAGES_WEEKLY_DISTRIBUTIONS=' "${_FILE}" || \
		echo "LIVE_BUILD_CRON_IMAGES_WEEKLY_DISTRIBUTIONS=" >> "${_FILE}"

		test -z "${LIVE_BUILD_CRON_IMAGES_WEEKLY_FLAVOURS}" || \
		grep -Eq '^ *LIVE_BUILD_CRON_IMAGES_WEEKLY_FLAVOURS=' "${_FILE}" || \
		echo "LIVE_BUILD_CRON_IMAGES_WEEKLY_FLAVOURS=" >> "${_FILE}"

		test -z "${LIVE_BUILD_CRON_IMAGES_MONTHLY_DISTRIBUTIONS}" || \
		grep -Eq '^ *LIVE_BUILD_CRON_IMAGES_MONTHLY_DISTRIBUTIONS=' "${_FILE}" || \
		echo "LIVE_BUILD_CRON_IMAGES_MONTHLY_DISTRIBUTIONS=" >> "${_FILE}"

		test -z "${LIVE_BUILD_CRON_IMAGES_MONTHLY_FLAVOURS}" || \
		grep -Eq '^ *LIVE_BUILD_CRON_IMAGES_MONTHLY_FLAVOURS=' "${_FILE}" || \
		echo "LIVE_BUILD_CRON_IMAGES_MONTHLY_FLAVOURS=" >> "${_FILE}"

		test -z "${LIVE_BUILD_CRON_IMAGES_SOURCE}" || \
		grep -Eq '^ *LIVE_BUILD_CRON_IMAGES_SOURCE=' "${_FILE}" || \
		echo "LIVE_BUILD_CRON_IMAGES_SOURCE=" >> "${_FILE}"

		sed	-e "s|^ *LIVE_BUILD_CRON_IMAGES=.*|LIVE_BUILD_CRON_IMAGES=\"${LIVE_BUILD_CRON_IMAGES}\"|" \
			-e "s|^ *LIVE_BUILD_CRON_IMAGES_DIRECTORY=.*|LIVE_BUILD_CRON_IMAGES_DIRECTORY=\"${LIVE_BUILD_CRON_IMAGES_DIRECTORY}\"|" \
			-e "s|^ *LIVE_BUILD_CRON_IMAGES_BUILD=.*|LIVE_BUILD_CRON_IMAGES_BUILD=\"${LIVE_BUILD_CRON_IMAGES_BUILD}\"|" \
			-e "s|^ *LIVE_BUILD_CRON_IMAGES_DAILY_DISTRIBUTIONS=.*|LIVE_BUILD_CRON_IMAGES_DAILY_DISTRIBUTIONS=\"${LIVE_BUILD_CRON_IMAGES_DAILY_DISTRIBUTIONS}\"|" \
			-e "s|^ *LIVE_BUILD_CRON_IMAGES_DAILY_FLAVOURS=.*|LIVE_BUILD_CRON_IMAGES_DAILY_FLAVOURS=\"${LIVE_BUILD_CRON_IMAGES_DAILY_FLAVOURS}\"|" \
			-e "s|^ *LIVE_BUILD_CRON_IMAGES_WEEKLY_DISTRIBUTIONS=.*|LIVE_BUILD_CRON_IMAGES_WEEKLY_DISTRIBUTIONS=\"${LIVE_BUILD_CRON_IMAGES_WEEKLY_DISTRIBUTIONS}\"|" \
			-e "s|^ *LIVE_BUILD_CRON_IMAGES_WEEKLY_FLAVOURS=.*|LIVE_BUILD_CRON_IMAGES_WEEKLY_FLAVOURS=\"${LIVE_BUILD_CRON_IMAGES_WEEKLY_FLAVOURS}\"|" \
			-e "s|^ *LIVE_BUILD_CRON_IMAGES_MONTHLY_DISTRIBUTIONS=.*|LIVE_BUILD_CRON_IMAGES_MONTHLY_DISTRIBUTIONS=\"${LIVE_BUILD_CRON_IMAGES_MONTHLY_DISTRIBUTIONS}\"|" \
			-e "s|^ *LIVE_BUILD_CRON_IMAGES_MONTHLY_FLAVOURS=.*|LIVE_BUILD_CRON_IMAGES_MONTHLY_FLAVOURS=\"${LIVE_BUILD_CRON_IMAGES_MONTHLY_FLAVOURS}\"|" \
			-e "s|^ *LIVE_BUILD_CRON_IMAGES_SOURCE=.*|LIVE_BUILD_CRON_IMAGES_SOURCE=\"${LIVE_BUILD_CRON_IMAGES_SOURCE}\"|" \
		"${_FILE}" > "${_FILE}.tmp"

		mv -f "${_FILE}.tmp" "${_FILE}"

		mkdir -p "${LIVE_BUILD_CRON_IMAGES_DIRECTORY}"

		if [ -n "${_CRON}" ]
		then

cat > /etc/cron.d/live-build-cron-images << EOF
# /etc/cron.d/live-build-cron-images

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

# m h dom mon dow	user		command
${_CRON}		root		/usr/bin/live-build-cron-images
EOF

		else
			rm -f /etc/cron.d/live-build-cron-images
		fi

		if [ -x "/etc/init.d/cron" ]
		then
			if [ -x "$(which invoke-rc.d 2>/dev/null)" ]
			then
				invoke-rc.d cron restart || exit ${?}
			else
				/etc/init.d/cron restart || exit ${?}
			fi
		fi
		;;

	abort-upgrade|abort-remove|abort-deconfigure)

		;;

	*)
		echo "postinst called with unknown argument \`${1}'" >&2
		exit 1
		;;
esac

#DEBHELPER#

exit 0
