Blame Scripts/Functions/cli_printMessage.sh

4c79b5
#!/bin/bash
4c79b5
#
5b396a
# cli_printMessage.sh -- This function outputs information in
db0971
# predifined formats to standard error. This function is the standard
4c79b5
# way to output information inside centos-art.sh script.
4c79b5
#
2d3646
# Copyright (C) 2009, 2010, 2011 The CentOS Project
fa95b1
#
fa95b1
# This program is free software; you can redistribute it and/or modify
fa95b1
# it under the terms of the GNU General Public License as published by
dcd347
# the Free Software Foundation; either version 2 of the License, or (at
dcd347
# your option) any later version.
fa95b1
#
74a058
# This program is distributed in the hope that it will be useful, but
74a058
# WITHOUT ANY WARRANTY; without even the implied warranty of
4c79b5
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
4c79b5
# General Public License for more details.
4c79b5
#
4c79b5
# You should have received a copy of the GNU General Public License
4c79b5
# along with this program; if not, write to the Free Software
dcd347
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
7ac5a5
#
4c79b5
# ----------------------------------------------------------------------
418249
# $Id$
4c79b5
# ----------------------------------------------------------------------
4c79b5
5b396a
function cli_printMessage {
4c79b5
49237e
    # Verify `--quiet' option.
49237e
    if [[ "$FLAG_QUIET" == 'true' ]];then
49237e
        return
49237e
    fi
49237e
6d2593
    # Define short options.
6d2593
    local ARGSS=''
c62219
6d2593
    # Define long options.
b004f3
    local ARGSL='message:,as-separator-line,as-banner-line,as-updating-line,as-cropping-line,as-tuningup-line,as-deleting-line,as-checking-line,as-creating-line,as-reading-line,as-savedas-line,as-linkto-line,as-movedto-line,as-translation-line,as-design-line,as-configuration-line,as-palette-line,as-response-line,as-request-line,as-error-line,as-toknowmore-line,as-yesornorequest-line,as-notrailingnew-line,as-stdout-line,'
c62219
5b396a
    # Initialize arguments with an empty value and set it as local
5b396a
    # variable to this function scope.
6d2593
    local ARGUMENTS=''
c62219
6d2593
    # Redefine ARGUMENTS variable using current positional parameters. 
6d2593
    cli_doParseArgumentsReDef "$@"
03f43a
6d2593
    # Redefine ARGUMENTS variable using getopt output.
6d2593
    cli_doParseArguments
c62219
6d2593
    # Redefine positional parameters using ARGUMENTS variable.
6d2593
    eval set -- "$ARGUMENTS"
f259c7
2fa569
    # Initialize message variable locally using non-option arguments.
7be3ee
    local MESSAGE=$(echo $@ | sed -r 's!^.*--[[:space:]](.+)$!\1!')
2fa569
b2f699
    # Verify message variable, it cannot have an empty value.
7be3ee
    if [[ $MESSAGE == '--' ]];then
b2f699
        cli_printMessage "`gettext "The message cannot be empty."`" --as-error-line
b2f699
    fi
b2f699
7be3ee
    # Reverse the codification performed on characters that may affect
7be3ee
    # parsing options and non-option arguments. This codification is
7be3ee
    # realized before building the ARGUMENTS variable, at
7be3ee
    # cli_doParseArgumentsReDef, and we need to reverse it back here
7be3ee
    # in order to show the correct character when the message be
7be3ee
    # printed out on the screen.
7be3ee
    MESSAGE=$(echo $MESSAGE | sed -e "s/\\\0x27/'/g")
2fa569
                
7be3ee
    # Reduce paths and leading spaces from output messages. The main
7be3ee
    # purpose for this is to free horizontal space on output messages.
7be3ee
    MESSAGE=$(echo "$MESSAGE" | sed -r \
7be3ee
        -e "s!${HOME}/artwork/(trunk|branches|tags)/!\1/!g" \
7be3ee
        -e 's!^[[:space:]]+!!')
2fa569
6d2593
    # Look for options passed through positional parameters.
6d2593
    while true; do
903248
6d2593
        case "$1" in
c62219
5b396a
            --as-separator-line )
c62219
5b396a
                # Define width of separator line.
6d2593
                local MAX=70
6d2593
5b396a
                # Build the separator line. 
5b396a
                MESSAGE=$(\
5b396a
                    until [[ $MAX -eq 0 ]];do
5b396a
                        echo -n "$MESSAGE"
5b396a
                        MAX=$(($MAX - 1))
5b396a
                    done)
6d2593
5b396a
                # Draw the separator line.
5b396a
                echo "$MESSAGE" > /dev/stderr
6d2593
5b396a
                shift 2
6d2593
                break
6d2593
                ;;
6d2593
5b396a
            --as-banner-line )
5b396a
                cli_printMessage '-' --as-separator-line
5b396a
                cli_printMessage "$MESSAGE"
5b396a
                cli_printMessage '-' --as-separator-line
5b396a
                shift 2
6d2593
                break
6d2593
                ;;
6d2593
5b396a
            --as-updating-line )
5b396a
                cli_printMessage "`gettext "Updating"`: $MESSAGE"
5b396a
                shift 2
6d2593
                break
6d2593
                ;;
6d2593
5b396a
            --as-cropping-line )
5b396a
                cli_printMessage "`gettext "Cropping from"`: $MESSAGE"
5b396a
                shift 2
6d2593
                break
6d2593
                ;;
6d2593
5b396a
            --as-tuningup-line )
5b396a
                cli_printMessage "`gettext "Tuning-up"`: $MESSAGE"
5b396a
                shift 2
6d2593
                break
6d2593
                ;;
6d2593
5b396a
            --as-checking-line )
5b396a
                cli_printMessage "`gettext "Checking"`: $MESSAGE"
5b396a
                shift 2
6d2593
                break
6d2593
                ;;
6d2593
5b396a
            --as-creating-line )
5b396a
                cli_printMessage "`gettext "Creating"`: $MESSAGE"
5b396a
                shift 2
6d2593
                break
6d2593
                ;;
6d2593
5b396a
            --as-deleting-line )
5b396a
                cli_printMessage "`gettext "Deleting"`: $MESSAGE"
5b396a
                shift 2
6d2593
                break
6d2593
                ;;
6d2593
5b396a
            --as-reading-line )
5b396a
                cli_printMessage "`gettext "Reading"`: $MESSAGE"
5b396a
                shift 2
6d2593
                break
6d2593
                ;;
6d2593
5b396a
            --as-savedas-line )
5b396a
                cli_printMessage "`gettext "Saved as"`: $MESSAGE"
5b396a
                shift 2
6d2593
                break
6d2593
                ;;
c62219
            
5b396a
            --as-linkto-line )
5b396a
                cli_printMessage "`gettext "Linked to"`: $MESSAGE"
5b396a
                shift 2
6d2593
                break
6d2593
                ;;
c62219
        
5b396a
            --as-movedto-line )
5b396a
                cli_printMessage "`gettext "Moved to"`: $MESSAGE"
5b396a
                shift 2
6d2593
                break
6d2593
                ;;
6d2593
5b396a
            --as-translation-line )
5b396a
                cli_printMessage "`gettext "Translation"`: $MESSAGE"
5b396a
                shift 2
6d2593
                break
6d2593
                ;;
6d2593
5b396a
            --as-design-line )
5b396a
                cli_printMessage "`gettext "Design"`: $MESSAGE"
5b396a
                shift 2
6d2593
                break
6d2593
                ;;
6d2593
5b396a
            --as-configuration-line )
5b396a
                cli_printMessage "`gettext "Configuration"`: $MESSAGE"
5b396a
                shift 2
6d2593
                break
6d2593
                ;;
6d2593
5b396a
            --as-palette-line )
5b396a
                cli_printMessage "`gettext "Palette"`: $MESSAGE"
5b396a
                shift 2
6d2593
                break
6d2593
                ;;
6d2593
5b396a
            --as-response-line )
f2cc86
                echo "--> $MESSAGE" > /dev/stderr
5b396a
                shift 2
6d2593
                break
6d2593
                ;;
6d2593
5b396a
            --as-request-line )
5b396a
                cli_printMessage "${MESSAGE}: " --as-notrailingnew-line
5b396a
                shift 2
6d2593
                break
6d2593
                ;;
6d2593
5b396a
            --as-error-line )
6d2593
                # This option is used to print error messsages.
7be3ee
                echo "${CLI_PROGRAM} ($(caller 1 | gawk '{ print $2 " " $1 }')): ${MESSAGE}"
5b396a
                cli_printMessage "${FUNCDIRNAM}" --as-toknowmore-line
5b396a
                shift 2
6d2593
                break
6d2593
                ;;
6d2593
5b396a
            --as-toknowmore-line )
5b396a
                cli_printMessage '-' --as-separator-line
5b396a
                cli_printMessage "`gettext "To know more, run the following command"`:"
7be3ee
                cli_printMessage "centos-art help --read trunk/Scripts/Functions/$MESSAGE"
5b396a
                cli_printMessage '-' --as-separator-line
6d2593
                exit # <-- ATTENTION: Do not remove this line. We use this
6d2593
                     #                option as convenction to end script
6d2593
                     #                execution.
6d2593
                ;;
c62219
    
5b396a
            --as-yesornorequest-line )
6d2593
                # Define positive answer.
6d2593
                local Y="`gettext "yes"`"
bc9c90
6d2593
                # Define negative answer.
6d2593
                local N="`gettext "no"`"
bc9c90
6d2593
                # Define default answer.
6d2593
                local ANSWER=${N}
3ccb0a
6d2593
                if [[ $FLAG_ANSWER == 'true' ]];then
3ccb0a
6d2593
                    ANSWER=${Y}
bc9c90
6d2593
                else
bc9c90
6d2593
                    # Print the question.
5b396a
                    cli_printMessage "$MESSAGE [${Y}/${N}]: " --as-notrailingnew-line
bc9c90
6d2593
                    # Redefine default answer based on user's input.
6d2593
                    read ANSWER
bc9c90
6d2593
                fi
bc9c90
6d2593
                # Verify user's answer. Only positive answer let the
6d2593
                # script flow to continue. Otherwise, if something
6d2593
                # different from possitive answer is passed, the
6d2593
                # script terminates its execution immediatly.
6d2593
                if [[ ! ${ANSWER} =~ "^${Y}" ]];then
6d2593
                    exit
6d2593
                fi
bc9c90
5b396a
                shift 2
6d2593
                break
6d2593
                ;;
bc9c90
5b396a
            --as-notrailingnew-line )
6d2593
                printf "$MESSAGE" > /dev/stderr
5b396a
                shift 2
6d2593
                break
6d2593
                ;;
bc9c90
b004f3
            --as-stdout-line )
b004f3
                echo "$MESSAGE"
b004f3
                shift 2
b004f3
                break
b004f3
                ;;
b004f3
b004f3
            * )
6d2593
                echo "$MESSAGE" \
6d2593
                        | awk 'BEGIN { FS=": " }
6d2593
                            { 
6d2593
                            if ( $0 ~ /^-+$/ ) 
6d2593
                                print $0
6d2593
                            else
6d2593
                                printf "%-15s\t%s\n", $1, $2 
6d2593
                            }
6d2593
                            END {}' > /dev/stderr
5b396a
                shift 2
6d2593
                break
6d2593
                ;;
bc9c90
6d2593
        esac
bc9c90
6d2593
    done
4c79b5
5b396a
    # Redefine ARGUMENTS variable using current positional parameters. 
5b396a
    cli_doParseArgumentsReDef "$@"
5b396a
4c79b5
}