#!/bin/bash

###########################################################################
#
# MODULE:       Commands
# COPYRIGHT:    (C) 2009-2025 by CacheGuard Technologies Ltd (UK)
# COPYRIGHT:    (C) 2026-2026 by CacheGuard Technologies SAS (FR)
#
# 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 3 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, see <http://www.gnu.org/licenses/>.
#
###########################################################################

get-html-bold-command()
{
    test -n "${1}" || return 1
    local cmd=${1}

    cmd="<a href='${DOC_COMMAND_DIR}/${cmd}.html' target='_blank'><u>${cmd}</u></a>"

    echo ${cmd}
}

get-magnified-command()
{
    test -n "${1}" || return 1
    local cmd=${1}

    if test ${TERM} == ${WADMIN_TERM} ; then
	cmd=$(get-html-bold-command ${cmd})
    else
	cmd=$(get-terminal-bold-text ${cmd})
    fi

    echo ${cmd}
}

get-magnified-error-text()
{
    test -n "${1}" || return 1
    local in_txt=${1}

    local out_txt wrd len

    for wrd in ${in_txt}
    do
	if test ${wrd:0:1} != '@' ; then
	    out_txt="${out_txt} ${wrd}"
	    continue
	fi

	len=${#wrd}
	((len--))

	if test ${wrd:${len}:1} != '@' ; then
	    out_txt="${out_txt} ${wrd}"
	    continue
	fi

	((len--))
	wrd=${wrd:1:${len}}
	wrd=$(get-magnified-command ${wrd})

	out_txt="${out_txt} ${wrd}"
    done

    test ${TERM} == ${WADMIN_TERM} || out_txt="${out_txt} To do, so use the $(get-terminal-bold-text help) command followed by the command name."

    echo ${out_txt:1}
}

display-error()
{
    test -n "${1}" || return 1
    local err=${1}
    local txt=${2}
    local notag=${3}

    local txt tag com trx

    txt="${txt}${Errors[${err}]}"

    if test ${TERM} == ${WADMIN_TERM} ; then
	test -n "${notag}" || tag="${ERROR_TAG} "
    else
	test -n "${notag}" || tag='*** '
	com=$(file-basename ${ARGS[0]})
	test -z "${TRANSACTION}" || trx=" ($(get-magnified-command "${com}") command)"
    fi

    if test -z "${txt}" ; then
	txt="No error description has been defined for this error."
    else
	if test ${err} -le 254 -o ${err} -ge 512 ; then
	    test ${TERM} == ${WADMIN_TERM} || txt="${txt} Refer to the invoked command manual: use the $(get-terminal-bold-text "help ${com}") command."
	else
	    txt=$(get-magnified-error-text "${txt}")
	fi
    fi

    echo -e "${tag}Error ${err}${trx} - ${txt}" >&2
}

error()
{
    local in_error_code=${1} error_code
    test -n "${in_error_code}" || in_error_code=255

    if test ${in_error_code} = 254 ; then
	error_code=${INDIRECT_ERROR_CODE}
    else
	error_code=${in_error_code}
    fi

    display-error ${error_code}

    if test -z "${TRANSACTION}" ; then
	exit ${in_error_code}
    else
	LAST_ERROR=${error_code}
    fi
}

display-warning()
{
    test -n "${1}" || return 1
    warn_code=${1}
    
    local warn_text=${Warnings[${warn_code}]}

    if test ${TERM} == ${WADMIN_TERM} ; then
	tag="${WARNING_TAG}"
    else
	tag='+++'
    fi

    echo "${tag} Warning ${warn_code} - ${warn_text}" >&2
}

warning()
{
    if test -z "${1}" ; then local warn_code=255 ; else local warn_code=${1} ; fi
    display-warning ${warn_code}
}

display-info()
{
    test -n "${1}" || return 1
    info_code=${1}

    test "${CLI_SILENT}" != yes || return 0

    local info_text=${Informations[${info_code}]}

    if test ${TERM} == ${WADMIN_TERM} ; then
	tag="${INFO_TAG}"
    else
	tag='---'
    fi

    echo "${tag} Information ${info_code} - ${info_text}" >&2
}

info()
{
    local info_code=${1}
    test -n "${info_code}" || info_code=255

    display-info ${info_code}
}
