#!/bin/sh
# -*- mode: shell-script; coding: utf-8 -*-
#
# debconf-get
#
# Retrieve given debconf value.
#
# Copyright (C) 2012 Elmar Hoffmann
#
# This program 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 2 of the License, or
# (at your option) any later version.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#

set -e

. /lib/elho/shell-tools

# Unfortunately, confmodule(3) clodders stdout, thus we initially invoke it
# in a subshell and only do the actual debconf  interaction when invoked by
# confmodule(3) within that subshell

if [ -z "${DEBCONF_GET_CONFMODULE_INVOCATION}" ]; then

    if [ $# -ne 1 ]; then
	echo "Usage: $(basename -- "$0") NAME"
	exit 2
    fi

    # duplicate stdout to fd 9 for use by the subshell
    exec 9>&1

    DEBCONF_GET_CONFMODULE_INVOCATION=1
    export DEBCONF_GET_CONFMODULE_INVOCATION

    . /usr/share/debconf/confmodule || exit 1

else
    # we have been invoked by confmodule(3)

    name="$1"

    . /usr/share/debconf/confmodule

    if ! db_get "${name}"; then
	error "Getting debconf value '${name}' failed: Error ${RET}"
	exit 1
    fi

    # stdout is fd 9 in this subshell
    echo "${RET}" >&9
fi
