Blame tcar-scripts/tcar_printMessage.sh

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