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
5b396a
    # Verify number of positional parameters. The first argument is
5b396a
    # required.
5b396a
    if [[ $# -eq 0 ]];then
5b396a
        cli_printMessage "`gettext "The first argument is required."`" --as-error-line
5b396a
    fi
5b396a
6d2593
    # Define short options.
6d2593
    local ARGSS=''
c62219
6d2593
    # Define long options.
6d2593
    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-regular-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.
4a4a01
    local MESSAGE=$(echo $@ | sed -r 's!^(.*[[:space:]]*--[[:space:]]+)?!!')
2fa569
b2f699
    # Verify message variable, it cannot have an empty value.
b2f699
    if [[ $MESSAGE == '' ]];then
b2f699
        cli_printMessage "`gettext "The message cannot be empty."`" --as-error-line
b2f699
    fi
b2f699
2fa569
    # Reverse character codification performed when the list of
2fa569
    # arguments wast built at cli_doParseArgumentsReDef.sh.
2fa569
    MESSAGE=$(echo $MESSAGE | sed "s/\\\0x27/'/g")
2fa569
                
2fa569
    # Reduce paths inside output messages. The main purpose for this
2fa569
    # is to free horizontal space in output messages.
2fa569
    MESSAGE=$(echo "$MESSAGE" \
2fa569
        | sed -r "s!${HOME}/artwork/(trunk|branches|tags)/!\1/!g")
2fa569
2fa569
    # Remove leading blank spaces from output messages.
2fa569
    MESSAGE=$(echo "$MESSAGE" | sed -r '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 )
5b396a
                cli_printMessage "--> $MESSAGE"
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.
6d2593
                echo "${CLI_PROGRAM} (${FUNCNAME[1]}): ${MESSAGE}" > /dev/stderr
5b396a
                cli_printMessage "${FUNCDIRNAM}" --as-toknowmore-line
5b396a
                shift 2
6d2593
                break
6d2593
                ;;
6d2593
5b396a
            --as-toknowmore-line )
6d2593
                # This option receives the output of bash's caller
6d2593
                # built-in as message value and produces the
6d2593
                # documentation entry from it.
6d2593
                MESSAGE="trunk/Scripts/Functions/$MESSAGE"
5b396a
                cli_printMessage '-' --as-separator-line
5b396a
                cli_printMessage "`gettext "To know more, run the following command"`:"
5b396a
                cli_printMessage "centos-art help --read $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
5b396a
            --as-regular-line | * )
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
}