#
# Copyright (c) 2024, Jesús Daniel Colmenares Oviedo <DtxdF@disroot.org>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
#   list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
#
# * Neither the name of the copyright holder nor the names of its
#   contributors may be used to endorse or promote products derived from
#   this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

TABLE_NAME=
COLUMNS=
COLUMNS_COUNT=0
TABLE_FLAG_ESCAPE=0
TABLE_FLAG_COLUMNS=0
TABLE_FLAG_EMPTY=0
TABLE_FLAG_ONLY_ONCE=1
TABLE_FLAG_PRETTY=0
TABLE_FLAG_TABULATE=0

lib_table_init()
{
	TABLE_NAME="$1"

	if [ -z "${TABLE_NAME}" ]; then
		lib_err ${EX_USAGE} "usage: lib_table_init table_name"
	fi

	COLUMNS=
	COLUMNS_COUNT=0

	TABLE_FLAG_ESCAPE=0
	TABLE_FLAG_COLUMNS=0
	TABLE_FLAG_EMPTY=0
	TABLE_FLAG_ONLY_ONCE=1
	TABLE_FLAG_PRETTY=0
	TABLE_FLAG_TABULATE=0
}

lib_table_set()
{
	if [ -z "${TABLE_NAME}" ]; then
		lib_err ${EX_CONFIG} "Table name must be set!"
	fi

	local name="$1" value="$2"

	if [ -z "${name}" ]; then
		lib_err ${EX_USAGE} "usage: lib_table_set name [value]"
	fi

	COLUMNS="${COLUMNS} ${name}"
	COLUMNS_COUNT=$((COLUMNS_COUNT+1))

	setvar "${TABLE_NAME}_${name}" "${value}"
}

lib_table_enable_escape()
{
	TABLE_FLAG_ESCAPE=1
}

lib_table_disable_escape()
{
	TABLE_FLAG_ESCAPE=0
}

lib_table_enable_columns()
{
	TABLE_FLAG_COLUMNS=1
}

lib_table_disable_columns()
{
	TABLE_FLAG_COLUMNS=0
}

lib_table_enable_empty()
{
	TABLE_FLAG_EMPTY=1
}

lib_table_disable_empty()
{
	TABLE_FLAG_EMPTY=0
}

lib_table_enable_only_once()
{
	TABLE_FLAG_ONLY_ONCE=1
}

lib_table_disable_only_once()
{
	TABLE_FLAG_ONLY_ONCE=0
}

lib_table_enable_pretty()
{
	TABLE_FLAG_PRETTY=1
}

lib_table_disable_pretty()
{
	TABLE_FLAG_PRETTY=0
}

lib_table_enable_tabulate()
{
	TABLE_FLAG_TABULATE=1
}

lib_table_disable_tabulate()
{
	TABLE_FLAG_TABULATE=0
}

lib_table_print()
{
	if [ -z "${TABLE_NAME}" ]; then
		lib_err ${EX_CONFIG} "Table name must be set!"
	fi

	# There is no problem when no columns are defined.
	if [ -z "${COLUMNS}" ]; then
		return 0
	fi

	local empty_separator
	if [ ${COLUMNS_COUNT} -eq 1 ]; then
		empty_separator=
	else
		empty_separator=" "
	fi

	local columns_upper=`echo "${COLUMNS}" | tr '[:lower:]' '[:upper:]'`

	{
		if [ ${TABLE_FLAG_COLUMNS} -eq 1 ]; then
			printf "%s\n" "${columns_upper}" | \
				if [ ${TABLE_FLAG_PRETTY} -eq 1 -o ${TABLE_FLAG_TABULATE} -eq 1 ]; then
					sed -Ee 's/ /\t/g'
				else
					cat
				fi
		fi

		local sep=" "
		if [ ${TABLE_FLAG_PRETTY} -eq 1 -o ${TABLE_FLAG_TABULATE} -eq 1 ]; then
			sep="\t"
		fi

		local column content
		for column in ${COLUMNS}; do
			content=`lib_loaded_var "${TABLE_NAME}_${column}"`

			if [ ${TABLE_FLAG_ONLY_ONCE} -eq 1 ]; then
				unset "${TABLE_NAME}_${column}"
			fi

			if lib_check_empty "${content}"; then
				if [ ${TABLE_FLAG_EMPTY} -eq 1 ]; then
					content="${empty_separator}"
				else
					content="-"
				fi
			else
				if [ ${TABLE_FLAG_PRETTY} -eq 1 -o ${TABLE_FLAG_ESCAPE} -eq 1 ]; then
					content=`printf "%s" "${content}" | sed -Ee 's/\t/<TAB>/g'`
				fi
			fi

			printf "%s${sep}" "${content}"
		done
		echo
	} | \
	sed -Ee 's/ *$//' | \
	if [ ${TABLE_FLAG_PRETTY} -eq 1 ]; then
		column -ts $'\t'
	else
		cat
	fi

	if [ ${TABLE_FLAG_ONLY_ONCE} -eq 1 ]; then
		unset "${TABLE_NAME}"
	fi
}
