Blame Automation/centos-art.sh-cli/Scripts/cli_printMessage.sh

Alain Reguera Delgado 8f60cb
#!/bin/bash
Alain Reguera Delgado 615395
######################################################################
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 615395
#   cli_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 615395
#   Written by: 
Alain Reguera Delgado 615395
#   * Alain Reguera Delgado <al@centos.org.cu>, 2009-2013
Alain Reguera Delgado 615395
#     Key fingerprint = D67D 0F82 4CBD 90BC 6421  DF28 7CCE 757C 17CA 3951
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 8f60cb
# Copyright (C) 2009-2013 The CentOS Project
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 8f60cb
function cli_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 8f60cb
        cli_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 615395
    MESSAGE=$(echo ${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 615395
            echo "${MESSAGE}" | sed -r \
Alain Reguera Delgado 615395
                -e "s!${TCAR_USER_WRKDIR}/!!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 8f60cb
                            printf "%-15s\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 8f60cb
            # Build the error message.
Alain Reguera Delgado 615395
            cli_printMessage "${TCAR_CLI_COMMAND} ($(cli_printCaller 2)): ${MESSAGE}" --as-stderr-line
Alain Reguera Delgado 615395
            cli_printMessage "$(cli_printCaller "2" "--name")" --as-toknowmore-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 8f60cb
        --as-suggestion-line )
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
            # Build the error message.
Alain Reguera Delgado 615395
            cli_printMessage "${TACAR_CLI_COMMAND} ($(cli_printCaller 2)):" --as-stderr-line
Alain Reguera Delgado 615395
            cli_printMessage "`gettext "The path provided cannot be processed the way you entered it."`" --as-stderr-line
Alain Reguera Delgado 615395
            cli_printMessage "`gettext "Instead, try the following equivalence:"` ${MESSAGE}" --as-stderr-line
Alain Reguera Delgado 615395
            cli_printMessage "${CLI_FUNCTION_NAME}" --as-toknowmore-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 8f60cb
        --as-toknowmore-line )
Alain Reguera Delgado 615395
            cli_printMessage "`gettext "To know more, run"` ${TCAR_CLI_COMMAND} help --id=${MESSAGE} " --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 615395
                cli_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 615395
            cli_printMessage "--> ${MESSAGE}" --as-stderr-line
Alain Reguera Delgado 8f60cb
            ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        --as-request-line )
Alain Reguera Delgado 8f60cb
            cli_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 615395
                -e "s!${TCAR_USER_WRKDIR}/!!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 615395
                -e "s!${TCAR_USER_WRKDIR}/!!g" 1>&2
Alain Reguera Delgado 8f60cb
            ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    esac
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Verify verbose option. The verbose option controls whether
Alain Reguera Delgado 8f60cb
    # messages 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 8f60cb
                done)
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
            # Draw the separator line.
Alain Reguera Delgado 615395
            echo "${MESSAGE}"
Alain Reguera Delgado 8f60cb
            ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        --as-banner-line )
Alain Reguera Delgado 8f60cb
            cli_printMessage '-' --as-separator-line
Alain Reguera Delgado 615395
            cli_printMessage "${MESSAGE}" --as-stdout-line
Alain Reguera Delgado 8f60cb
            cli_printMessage '-' --as-separator-line
Alain Reguera Delgado 8f60cb
            ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        --as-processing-line )
Alain Reguera Delgado 615395
            cli_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 615395
            cli_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 615395
            cli_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 615395
            cli_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 615395
            cli_printMessage "`gettext "Combining"`: ${MESSAGE}" --as-stdout-line
Alain Reguera Delgado 8f60cb
            ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        --as-creating-line | --as-updating-line )
Alain Reguera Delgado 615395
            if [[ -a "${MESSAGE}" ]];then
Alain Reguera Delgado 615395
                cli_printMessage "`gettext "Updating"`: ${MESSAGE}" --as-stdout-line
Alain Reguera Delgado 8f60cb
            else
Alain Reguera Delgado 615395
                cli_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 615395
            cli_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 615395
            cli_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 615395
            cli_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 615395
            cli_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 615395
            cli_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 615395
            cli_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 615395
            cli_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 615395
            cli_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 615395
            cli_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 615395
            cli_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 615395
            cli_printMessage "`gettext "Palette"`: ${MESSAGE}" --as-stdout-line
Alain Reguera Delgado 8f60cb
            ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        --as-inkscape-line )
Alain Reguera Delgado 615395
            cli_printMessage "${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
}