#!/bin/sh
# This is a shell library to interface to the Debian configuration management
# system.

###############################################################################
# Initialization.

# Check to see if a FrontEnd is running.
if [ ! "$DEBIAN_HAS_FRONTEND" ]; then
	# Since there is no FrontEnd, this program execs a FrontEnd.
	# It will then run a new copy of $0 that can talk to it.
	exec /usr/share/debconf/frontend -- $0 "$@"
fi

# Only do this once.
if [ -z "$DEBCONF_REDIR" ]; then
	# Redirect standard output to standard error. This prevents common
	# mistakes by making all the output of the postinst or whatever
	# script is using this library not be parsed as confmodule commands.
	#
	# To actually send something to standard output, send it to fd 3.
	exec 3>&1 1>&5
	DEBCONF_REDIR=1
	export DEBCONF_REDIR
fi

# The original stdin/stdout/stderr fds are duplicated onto fds
# DEBCONF_OLD_FD_BASE to DEBCONF_OLD_FD_BASE + 2. These values are exported
# so that you can get the original fds back. This is kind of a hack:
# ideally, we might use something like a socket for the debconf protocol
# instead of stdio.
DEBCONF_OLD_FD_BASE='@DEBCONF_OLD_FD_BASE@'
export DEBCONF_OLD_FD_BASE

###############################################################################
# Commands.

_db_cmd () {
	echo "$@" >&3
	# Set to newline to get whole line.
	local IFS='
'
	local _LINE
	read -r _LINE
	# Disgusting, but it's the only good way to split the line,
	# preserving all other whitespace.
	RET="${_LINE#[! 	][ 	]}"
	return ${_LINE%%[ 	]*}
}

REPEAT: db_@command@ ()	{ _db_cmd "@COMMAND@ $@"; }

# An old alias for input.
db_text () {
	db_input $@
}

# Cannot read a return code, since there is none and it would block.
db_stop () {
	echo STOP >&3
}
