#!/bin/sh -e
#
# 2011-2014 Nico Schottelius (nico-cdist at schottelius.org)
# 2021 Dennis Camera (dennis.camera at ssrq-sds-fds.ch)
#
# This file is part of cdist.
#
# cdist is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# cdist is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
#

if test -f "${__object:?}/parameter/line"
then
	filename="${__object:?}/parameter/line"
elif test -s "${__object:?}/parameter/file"
then
	filename=$(cat "${__object:?}/parameter/file")
	if test "${filename}" = '-'
	then
		filename="${__object:?}/stdin"
	fi
else
	printf 'Neither --line nor --file set.\n' >&2
	exit 1
fi

# setting no lines makes no sense
test -s "${filename}" || exit 0

state_is=$(cat "${__object:?}/explorer/state")

if test "${state_is}" != 'present'
then
	cat <<-CODE
	debconf-set-selections <<'EOF'
	$(cat "${filename}")
	EOF
	CODE

	awk '
		{
			printf "set %s %s %s %s\n", $1, $2, $3, $4
		}' "${filename}" >>"${__messages_out:?}"
fi
