Blame Automation/Scripts/tcar_printMessage.sh

Alain Reguera Delgado 8f60cb
#!/bin/bash
Alain Reguera Delgado 615395
######################################################################
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado eb3010
#   tcar_printMessage.sh -- This function standardizes the way messages
Alain Reguera Delgado 615395
#   are printed by centos-art.sh script.
Alain Reguera Delgado 615395
#
Alain Reguera Delgado 0e5ba0
#   Written by:
Alain Reguera Delgado 615395
#   * Alain Reguera Delgado <al@centos.org.cu>, 2009-2013
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 0e5ba0
# Copyright (C) 2009-2013 The CentOS Artwork SIG
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 8f60cb
# This program is free software; you can redistribute it and/or modify
Alain Reguera Delgado 8f60cb
# it under the terms of the GNU General Public License as published by
Alain Reguera Delgado 8f60cb
# the Free Software Foundation; either version 2 of the License, or (at
Alain Reguera Delgado 8f60cb
# your option) any later version.
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 8f60cb
# This program is distributed in the hope that it will be useful, but
Alain Reguera Delgado 8f60cb
# WITHOUT ANY WARRANTY; without even the implied warranty of
Alain Reguera Delgado 8f60cb
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Alain Reguera Delgado 8f60cb
# General Public License for more details.
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 8f60cb
# You should have received a copy of the GNU General Public License
Alain Reguera Delgado 8f60cb
# along with this program; if not, write to the Free Software
Alain Reguera Delgado 8f60cb
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 615395
######################################################################
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado eb3010
function tcar_printMessage {
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 615395
    local MESSAGE="${1}"
Alain Reguera Delgado 615395
    local FORMAT="${2}"
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Verify message variable, it cannot have an empty value.
Alain Reguera Delgado 615395
    if [[ -z ${MESSAGE} ]];then
Alain Reguera Delgado eb3010
        tcar_printMessage "`gettext "The message cannot be empty."`" --as-error-line
Alain Reguera Delgado 8f60cb
    fi
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Define message horizontal width. This is the max number of
Alain Reguera Delgado 8f60cb
    # horizontal characters the message will use to be displayed on
Alain Reguera Delgado 8f60cb
    # the screen.
Alain Reguera Delgado 8f60cb
    local MESSAGE_WIDTH=66
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Remove empty spaces from message.
Alain Reguera Delgado 786ac0
    MESSAGE=$(printf %s "${MESSAGE}" | sed -r -e 's!^[[:space:]]+!!')
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Print messages that will always be printed no matter what value
Alain Reguera Delgado 615395
    # the TCAR_FLAG_QUIET variable has.
Alain Reguera Delgado 615395
    case "${FORMAT}" in
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        --as-stdout-line )
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
            # Default printing format. This is the format used when no
Alain Reguera Delgado 8f60cb
            # other specification is passed to this function. As
Alain Reguera Delgado 8f60cb
            # convenience, we transform absolute paths into relative
Alain Reguera Delgado 8f60cb
            # paths in order to free horizontal space on final output
Alain Reguera Delgado 8f60cb
            # messages.
Alain Reguera Delgado 786ac0
            printf %s "${MESSAGE}" | sed -r \
Alain Reguera Delgado 91a9c2
                -e "s!${TCAR_BASEDIR}/!!g" \
Alain Reguera Delgado 8f60cb
                -e "s!> /!> !g" \
Alain Reguera Delgado 8f60cb
                -e "s!/{2,}!/!g" \
Alain Reguera Delgado 615395
                | gawk 'BEGIN { FS=": " }
Alain Reguera Delgado 8f60cb
                    { 
Alain Reguera Delgado 8f60cb
                        if ( $0 ~ /^-+$/ )
Alain Reguera Delgado 8f60cb
                            print $0
Alain Reguera Delgado 8f60cb
                        else
Alain Reguera Delgado 06ab0f
                            printf "%-25s\t%s\n", $1, $2
Alain Reguera Delgado 8f60cb
                    }
Alain Reguera Delgado 8f60cb
                    END {}'
Alain Reguera Delgado 8f60cb
            ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        --as-error-line )
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 2e37b0
            # Build the error trail. This is very useful for tracking
Alain Reguera Delgado 2e37b0
            # the error down.
Alain Reguera Delgado 2e37b0
            tcar_printMessage '-' --as-separator-line
Alain Reguera Delgado 2e37b0
            tcar_printMessage "${FUNCNAME[*]}" --as-tree-line
Alain Reguera Delgado 2e37b0
Alain Reguera Delgado 8f60cb
            # Build the error message.
Alain Reguera Delgado 2e37b0
            tcar_printMessage '-' --as-separator-line
Alain Reguera Delgado 788260
            tcar_printMessage "$(tcar_printCaller 1) ${MESSAGE}" --as-stderr-line
Alain Reguera Delgado 2e37b0
            tcar_printMessage '-' --as-separator-line
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
            # Finish script execution with exit status 1 (SIGHUP) to
Alain Reguera Delgado 8f60cb
            # imply the script finished because an error.  We are
Alain Reguera Delgado 8f60cb
            # using this as convention to finish the script execution.
Alain Reguera Delgado 8f60cb
            # So, don't remove the following line, please.
Alain Reguera Delgado 8f60cb
            exit 1
Alain Reguera Delgado 8f60cb
            ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado f4d81a
        --as-debugger-line )
Alain Reguera Delgado f4d81a
            if [[ ${TCAR_FLAG_DEBUG} == 'true' ]];then
Alain Reguera Delgado f4d81a
                tcar_printMessage "${MESSAGE}" --as-stdout-line
Alain Reguera Delgado f4d81a
            else
Alain Reguera Delgado f4d81a
                return
Alain Reguera Delgado f4d81a
            fi
Alain Reguera Delgado f4d81a
            ;;
Alain Reguera Delgado f4d81a
Alain Reguera Delgado 2e37b0
        --as-tree-line )
Alain Reguera Delgado 2e37b0
            local NAME
Alain Reguera Delgado 2e37b0
            local -a FN
Alain Reguera Delgado 2e37b0
            for NAME in ${MESSAGE};do
Alain Reguera Delgado 2e37b0
                FN[++((${#FN[*]}))]=${NAME}
Alain Reguera Delgado 2e37b0
            done
Alain Reguera Delgado 2e37b0
            local COUNT=$(( ${#FN[*]} - 2 ))
Alain Reguera Delgado 2e37b0
            local SEPARATOR='`--'
Alain Reguera Delgado 2e37b0
            local SPACES=0
Alain Reguera Delgado 2e37b0
            echo "${TCAR_SCRIPT_BASEDIR}/${TCAR_SCRIPT_NAME}" 1>&2
Alain Reguera Delgado 2e37b0
            while [[ ${COUNT} -gt 0  ]];do
Alain Reguera Delgado 2e37b0
                if [[ ${COUNT} -eq $(( ${#FN[*]} - 2 )) ]];then
Alain Reguera Delgado 788260
                    echo ${SEPARATOR} ${FN[${COUNT}]} 1>&2
Alain Reguera Delgado 2e37b0
                else
Alain Reguera Delgado 2e37b0
                    echo ${FN[${COUNT}]} \
Alain Reguera Delgado 788260
                        | gawk '{ printf "%'${SPACES}'s%s %s\n", "", "'${SEPARATOR}'", $1 }' 1>&2
Alain Reguera Delgado 2e37b0
                fi
Alain Reguera Delgado 2e37b0
                COUNT=$((${COUNT} - 1))
Alain Reguera Delgado 2e37b0
                SPACES=$((${SPACES} + 4))
Alain Reguera Delgado 2e37b0
            done
Alain Reguera Delgado 2e37b0
            ;;
Alain Reguera Delgado 2e37b0
Alain Reguera Delgado 8f60cb
        --as-toknowmore-line )
Alain Reguera Delgado 788260
            tcar_printMessage "`gettext "To know more, run"` ${TCAR_SCRIPT_COMMAND} ${MESSAGE} --help" --as-stderr-line
Alain Reguera Delgado 8f60cb
            ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        --as-yesornorequest-line )
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
            # Define positive answer.
Alain Reguera Delgado 8f60cb
            local Y="`gettext "yes"`"
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
            # Define negative answer.
Alain Reguera Delgado 8f60cb
            local N="`gettext "no"`"
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
            # Define default answer.
Alain Reguera Delgado 8f60cb
            local ANSWER=${N}
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 615395
            if [[ ${TCAR_FLAG_YES} == 'true' ]];then
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
                ANSWER=${Y}
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
            else
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
                # Print the question to standard error.
Alain Reguera Delgado eb3010
                tcar_printMessage "${MESSAGE} [${Y}/${N}]" --as-request-line
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
                # Redefine default answer based on user's input.
Alain Reguera Delgado 8f60cb
                read ANSWER
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
            fi
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
            # Verify user's answer. Only positive answer let the
Alain Reguera Delgado 8f60cb
            # script flow to continue. Otherwise, if something
Alain Reguera Delgado 8f60cb
            # different from positive answer is passed, the script
Alain Reguera Delgado 8f60cb
            # terminates its execution immediately.
Alain Reguera Delgado 8f60cb
            if [[ ! ${ANSWER} =~ "^${Y}" ]];then
Alain Reguera Delgado 8f60cb
                exit
Alain Reguera Delgado 8f60cb
            fi
Alain Reguera Delgado 8f60cb
            ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        --as-selection-line )
Alain Reguera Delgado 8f60cb
            # Create selection based on message.
Alain Reguera Delgado 8f60cb
            local NAME=''
Alain Reguera Delgado 8f60cb
            select NAME in ${MESSAGE};do
Alain Reguera Delgado 615395
                echo ${NAME}
Alain Reguera Delgado 8f60cb
                break
Alain Reguera Delgado 8f60cb
            done
Alain Reguera Delgado 8f60cb
            ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        --as-response-line )
Alain Reguera Delgado 06ab0f
            tcar_printMessage "--> ${MESSAGE}" --as-stderr-line
Alain Reguera Delgado 8f60cb
            ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        --as-request-line )
Alain Reguera Delgado eb3010
            tcar_printMessage "${MESSAGE}:\040" --as-notrailingnew-line
Alain Reguera Delgado 8f60cb
            ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        --as-notrailingnew-line )
Alain Reguera Delgado 8f60cb
            echo -e -n "${MESSAGE}" | sed -r \
Alain Reguera Delgado 91a9c2
                -e "s!${TCAR_BASEDIR}/!!g" 1>&2
Alain Reguera Delgado 8f60cb
            ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        --as-stderr-line )
Alain Reguera Delgado 615395
            echo "${MESSAGE}" | sed -r \
Alain Reguera Delgado 91a9c2
                -e "s!${TCAR_BASEDIR}/!!g" 1>&2
Alain Reguera Delgado 8f60cb
            ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    esac
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado f4d81a
    # Verify quiet option. The quiet option controls whether messages
Alain Reguera Delgado f4d81a
    # are printed or not.
Alain Reguera Delgado 615395
    if [[ "${TCAR_FLAG_QUIET}" == 'true' ]];then
Alain Reguera Delgado 8f60cb
        return
Alain Reguera Delgado 8f60cb
    fi
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 615395
    # Print messages that will be printed only when the TCAR_FLAG_QUIET
Alain Reguera Delgado 8f60cb
    # variable is provided to centos-art.sh script.
Alain Reguera Delgado 615395
    case "${FORMAT}" in
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        --as-separator-line )
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
            # Build the separator line.
Alain Reguera Delgado 8f60cb
            MESSAGE=$(\
Alain Reguera Delgado 615395
                until [[ ${MESSAGE_WIDTH} -eq 0 ]];do
Alain Reguera Delgado 615395
                    echo -n "$(echo ${MESSAGE} | sed -r 's!(.).*!\1!')"
Alain Reguera Delgado 615395
                    MESSAGE_WIDTH=$((${MESSAGE_WIDTH} - 1))
Alain Reguera Delgado 2e37b0
                done) 
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
            # Draw the separator line.
Alain Reguera Delgado 2e37b0
            echo "${MESSAGE}" 1>&2
Alain Reguera Delgado 8f60cb
            ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        --as-banner-line )
Alain Reguera Delgado eb3010
            tcar_printMessage '-' --as-separator-line
Alain Reguera Delgado eb3010
            tcar_printMessage "${MESSAGE}" --as-stdout-line
Alain Reguera Delgado eb3010
            tcar_printMessage '-' --as-separator-line
Alain Reguera Delgado 8f60cb
            ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        --as-processing-line )
Alain Reguera Delgado eb3010
            tcar_printMessage "`gettext "Processing"`: ${MESSAGE}" --as-stdout-line
Alain Reguera Delgado 8f60cb
            ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        --as-cropping-line )
Alain Reguera Delgado eb3010
            tcar_printMessage "`gettext "Cropping from"`: ${MESSAGE}" --as-stdout-line
Alain Reguera Delgado 8f60cb
            ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        --as-tuningup-line )
Alain Reguera Delgado eb3010
            tcar_printMessage "`gettext "Tuning-up"`: ${MESSAGE}" --as-stdout-line
Alain Reguera Delgado 8f60cb
            ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        --as-checking-line )
Alain Reguera Delgado eb3010
            tcar_printMessage "`gettext "Checking"`: ${MESSAGE}" --as-stdout-line
Alain Reguera Delgado 8f60cb
            ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        --as-combining-line )
Alain Reguera Delgado eb3010
            tcar_printMessage "`gettext "Combining"`: ${MESSAGE}" --as-stdout-line
Alain Reguera Delgado 8f60cb
            ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 52ee2e
        --as-editing-line )
Alain Reguera Delgado 52ee2e
            tcar_printMessage "`gettext "Editing"`: ${MESSAGE}" --as-stdout-line
Alain Reguera Delgado 52ee2e
            ;;
Alain Reguera Delgado 52ee2e
Alain Reguera Delgado 8f60cb
        --as-creating-line | --as-updating-line )
Alain Reguera Delgado 615395
            if [[ -a "${MESSAGE}" ]];then
Alain Reguera Delgado eb3010
                tcar_printMessage "`gettext "Updating"`: ${MESSAGE}" --as-stdout-line
Alain Reguera Delgado 8f60cb
            else
Alain Reguera Delgado eb3010
                tcar_printMessage "`gettext "Creating"`: ${MESSAGE}" --as-stdout-line
Alain Reguera Delgado 8f60cb
            fi
Alain Reguera Delgado 8f60cb
            ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        --as-deleting-line )
Alain Reguera Delgado eb3010
            tcar_printMessage "`gettext "Deleting"`: ${MESSAGE}" --as-stdout-line
Alain Reguera Delgado 8f60cb
            ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        --as-reading-line )
Alain Reguera Delgado eb3010
            tcar_printMessage "`gettext "Reading"`: ${MESSAGE}" --as-stdout-line
Alain Reguera Delgado 8f60cb
            ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        --as-savedas-line )
Alain Reguera Delgado eb3010
            tcar_printMessage "`gettext "Saved as"`: ${MESSAGE}" --as-stdout-line
Alain Reguera Delgado 8f60cb
            ;;
Alain Reguera Delgado 8f60cb
            
Alain Reguera Delgado 8f60cb
        --as-linkto-line )
Alain Reguera Delgado eb3010
            tcar_printMessage "`gettext "Linked to"`: ${MESSAGE}" --as-stdout-line
Alain Reguera Delgado 8f60cb
            ;;
Alain Reguera Delgado 8f60cb
        
Alain Reguera Delgado 8f60cb
        --as-movedto-line )
Alain Reguera Delgado eb3010
            tcar_printMessage "`gettext "Moved to"`: ${MESSAGE}" --as-stdout-line
Alain Reguera Delgado 8f60cb
            ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        --as-translation-line )
Alain Reguera Delgado eb3010
            tcar_printMessage "`gettext "Translation"`: ${MESSAGE}" --as-stdout-line
Alain Reguera Delgado 8f60cb
            ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        --as-translating-line )
Alain Reguera Delgado eb3010
            tcar_printMessage "`gettext "Translating"`: ${MESSAGE}" --as-stdout-line
Alain Reguera Delgado 8f60cb
            ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        --as-validating-line )
Alain Reguera Delgado eb3010
            tcar_printMessage "`gettext "Validating"`: ${MESSAGE}" --as-stdout-line
Alain Reguera Delgado 8f60cb
            ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        --as-template-line )
Alain Reguera Delgado eb3010
            tcar_printMessage "`gettext "Template"`: ${MESSAGE}" --as-stdout-line
Alain Reguera Delgado 8f60cb
            ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        --as-configuration-line )
Alain Reguera Delgado eb3010
            tcar_printMessage "`gettext "Configuration"`: ${MESSAGE}" --as-stdout-line
Alain Reguera Delgado 8f60cb
            ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        --as-palette-line )
Alain Reguera Delgado eb3010
            tcar_printMessage "`gettext "Palette"`: ${MESSAGE}" --as-stdout-line
Alain Reguera Delgado 8f60cb
            ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    esac
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
}