Blame Automation/Scripts/tcar_setModuleArguments.sh

Alain Reguera Delgado 615395
#!/bin/bash
Alain Reguera Delgado 615395
######################################################################
Alain Reguera Delgado 615395
#
Alain Reguera Delgado 06ab0f
#   tcar_setModuleArguments.sh -- This function uses getopt to process
Alain Reguera Delgado 615395
#   arguments passed to centos-art.sh script.
Alain Reguera Delgado 615395
#
Alain Reguera Delgado 615395
#   This function works with the following three variables:
Alain Reguera Delgado 615395
#
Alain Reguera Delgado 615395
#       ARGSS
Alain Reguera Delgado 615395
#           Stores getopt short arguments definition.
Alain Reguera Delgado 615395
#
Alain Reguera Delgado 615395
#       ARGSL
Alain Reguera Delgado 615395
#           Stores getopt long arguments definition.  
Alain Reguera Delgado 615395
#
Alain Reguera Delgado 06ab0f
#       TCAR_MODULE_ARGUMENT
Alain Reguera Delgado 615395
#           Stores arguments passed to functions or command-line
Alain Reguera Delgado 615395
#           interface depending the context it is defined.
Alain Reguera Delgado 615395
#
Alain Reguera Delgado 615395
#   These three variables are not defined in this function but the
Alain Reguera Delgado 615395
#   function environment you want to provide option parsing for,
Alain Reguera Delgado 615395
#   through getopt command. Using local definition for these three
Alain Reguera Delgado 615395
#   variables let you to nest option parsing inside different
Alain Reguera Delgado 615395
#   function-environment levels.
Alain Reguera Delgado 615395
#
Alain Reguera Delgado 0e5ba0
#   Written by:
Alain Reguera Delgado 615395
#   * Alain Reguera Delgado <al@centos.org.cu>, 2009-2013
Alain Reguera Delgado 615395
#
Alain Reguera Delgado 0e5ba0
# Copyright (C) 2009-2013 The CentOS Artwork SIG
Alain Reguera Delgado 615395
#
Alain Reguera Delgado 615395
# This program is free software; you can redistribute it and/or modify
Alain Reguera Delgado 615395
# it under the terms of the GNU General Public License as published by
Alain Reguera Delgado 615395
# the Free Software Foundation; either version 2 of the License, or (at
Alain Reguera Delgado 615395
# your option) any later version.
Alain Reguera Delgado 615395
#
Alain Reguera Delgado 615395
# This program is distributed in the hope that it will be useful, but
Alain Reguera Delgado 615395
# WITHOUT ANY WARRANTY; without even the implied warranty of
Alain Reguera Delgado 615395
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Alain Reguera Delgado 615395
# General Public License for more details.
Alain Reguera Delgado 615395
#
Alain Reguera Delgado 615395
# You should have received a copy of the GNU General Public License
Alain Reguera Delgado 615395
# along with this program; if not, write to the Free Software
Alain Reguera Delgado 0e5ba0
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Alain Reguera Delgado 615395
#
Alain Reguera Delgado 615395
######################################################################
Alain Reguera Delgado 615395
Alain Reguera Delgado 06ab0f
function tcar_setModuleArguments {
Alain Reguera Delgado 615395
Alain Reguera Delgado 0fdaad
    # Define gettext-specific variables locally, to provide
Alain Reguera Delgado 0fdaad
    # per-function localization.
Alain Reguera Delgado 0fdaad
    local TEXTDOMAIN="${FUNCNAME}.sh"
Alain Reguera Delgado 0fdaad
    local TEXTDOMAINDIR="${TCAR_SCRIPT_BASEDIR}/Scripts/Locales"
Alain Reguera Delgado 0fdaad
Alain Reguera Delgado 615395
    local ARGUMENT=''
Alain Reguera Delgado 615395
Alain Reguera Delgado 615395
    # Fill up arguments global variable with current positional
Alain Reguera Delgado 615395
    # parameter  information. To avoid interpretation problems, use
Alain Reguera Delgado 06ab0f
    # single quotes to enclose each argument (TCAR_MODULE_ARGUMENT) from
Alain Reguera Delgado 615395
    # command-line individually.
Alain Reguera Delgado 615395
    for ARGUMENT in "${@}"; do
Alain Reguera Delgado 615395
Alain Reguera Delgado 615395
        # Remove any single quote from arguments passed to
Alain Reguera Delgado 615395
        # centos-art.sh script. We will use single quotes for grouping
Alain Reguera Delgado 615395
        # option values so white space can be passed through them.
Alain Reguera Delgado 786ac0
        ARGUMENT=$(printf %s "${ARGUMENT}" | tr -d "'")
Alain Reguera Delgado 615395
Alain Reguera Delgado 615395
        # Concatenate arguments and enclose them to let getopt to
Alain Reguera Delgado 615395
        # process them when they have spaces inside.
Alain Reguera Delgado 06ab0f
        TCAR_MODULE_ARGUMENT="${TCAR_MODULE_ARGUMENT} '${ARGUMENT}'"
Alain Reguera Delgado 615395
Alain Reguera Delgado 615395
    done
Alain Reguera Delgado 615395
Alain Reguera Delgado 615395
    # Verify non-option arguments passed to command-line. If there
Alain Reguera Delgado 06ab0f
    # isn't any or dot is provided, redefine the TCAR_MODULE_ARGUMENT
Alain Reguera Delgado ddf7d2
    # variable to use the current location the centos-art.sh script
Alain Reguera Delgado ddf7d2
    # was called from.
Alain Reguera Delgado 06ab0f
    if [[ -z "${TCAR_MODULE_ARGUMENT}" ]];then
Alain Reguera Delgado 06ab0f
        TCAR_MODULE_ARGUMENT=${PWD}
Alain Reguera Delgado 615395
    fi
Alain Reguera Delgado 615395
Alain Reguera Delgado ddf7d2
    # Verify presence of either short or long options in the
Alain Reguera Delgado ddf7d2
    # environment. If they are present apply option validation through
Alain Reguera Delgado ddf7d2
    # getopt.
Alain Reguera Delgado 615395
    if [[ ! -z ${ARGSS} ]] || [[ ! -z ${ARGSL} ]];then
Alain Reguera Delgado ddf7d2
Alain Reguera Delgado 06ab0f
        # Redefine positional parameters using TCAR_MODULE_ARGUMENT variable.
Alain Reguera Delgado 06ab0f
        eval set -- "${TCAR_MODULE_ARGUMENT}"
Alain Reguera Delgado ddf7d2
Alain Reguera Delgado ddf7d2
        # Process positional parameters using getopt's option validation.
Alain Reguera Delgado 06ab0f
        TCAR_MODULE_ARGUMENT=$(getopt -o "${ARGSS}" -l "${ARGSL}" \
Alain Reguera Delgado 06ab0f
            -n "${TCAR_SCRIPT_COMMAND} (${TCAR_MODULE_NAME})" -- "${@}")
Alain Reguera Delgado ddf7d2
Alain Reguera Delgado ddf7d2
        # Verify getopt's exit status and finish the script execution
Alain Reguera Delgado ddf7d2
        # with an error message, if it failed.
Alain Reguera Delgado 615395
        if [[ $? -ne 0 ]];then
Alain Reguera Delgado eb3010
            tcar_printMessage "`gettext "The argument verification failed."`" --as-error-line
Alain Reguera Delgado 615395
        fi
Alain Reguera Delgado ddf7d2
Alain Reguera Delgado 615395
    fi
Alain Reguera Delgado 615395
Alain Reguera Delgado 615395
}