# --- T2-COPYRIGHT-NOTE-BEGIN ---
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
# 
# T2 SDE: misc/output/parse-config
# Copyright (C) 2004 - 2007 The T2 SDE Project
# Copyright (C) 1998 - 2003 ROCK Linux Project
# 
# More information can be found in the files COPYING and README.
# 
# 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; version 2 of the License. A copy of the
# GNU General Public License can be found in the file COPYING.
# --- T2-COPYRIGHT-NOTE-END ---

. misc/output/terminal

output_proc() {
	columns=$1
	plugins=""
	for x in $( ls misc/output ); do
		if [ -f misc/output/$x/functions.in ]; then
			eval "y=\$SDECFG_OUTPUT_$( echo $x | tr a-z A-Z )"
			if [ "$y" = 1 ]; then
				. misc/output/$x/functions.in
				plugins="$plugins $x"
			fi
		fi
	done

	while read -r type data; do
		# always output to the terminal
		for plugin in terminal $plugins; do
			eval "echo_${type}_${plugin} $data"
		done
	done
	exit 0
}

if [ -z "$SDE_OUPUT_PLUGIN_PROC_ACTIVE" ]; then
	export SDE_OUPUT_PLUGIN_PROC_ACTIVE=1

	# since the output plugin's stdin is the pipe, we help it out
	# with the columns for pretty printing output
	[ "${columns:=$COLUMNS}" ] || columns="$( stty size 2> /dev/null | cut -d' ' -f2 )"
	[ "$columns" ] || columns=80
	exec 199> >( output_proc $columns )
fi

echo_sched() {
	local request="$1" tag ; shift
	local nl=$'\n' tc="'"

	# add each argument, seperated in quotes, after newlines and
	# tick quotes got escaped
	for tag; do
		tag=${tag//\\\\/\\\\}
		tag=${tag//$tc/\\$tc}
		tag=${tag//$nl/\\n}
		request="$request \$'$tag'"
	done

	echo "$request" >&199
}

echo_header()     { echo_sched header     "$@" ; }
echo_status()     { echo_sched status     "$@" ; }
echo_warning()    { echo_sched warning    "$@" ; }
echo_error()      { echo_sched error      "$@" ; }
echo_pkg_deny()   { echo_sched pkg_deny   "$@" ; }
echo_pkg_start()  { echo_sched pkg_start  "$@" ; }
echo_pkg_finish() { echo_sched pkg_finish "$@" ; }
echo_pkg_abort()  { echo_sched pkg_abort  "$@" ; }
echo_errorquote() { echo_sched errorquote "$@" ; }
