Blame Scripts/tcar_printMessage.sh

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