Blame Scripts/tcar_setModuleEnvironmentScripts.sh

Alain Reguera Delgado 66223d
#!/bin/bash
Alain Reguera Delgado 66223d
######################################################################
Alain Reguera Delgado 66223d
#
Alain Reguera Delgado b33753
#   tcar - The CentOS Artwork Repository automation tool.
Alain Reguera Delgado b33753
#   Copyright © 2014 The CentOS Artwork SIG
Alain Reguera Delgado 66223d
#
Alain Reguera Delgado b33753
#   This program is free software; you can redistribute it and/or
Alain Reguera Delgado b33753
#   modify it under the terms of the GNU General Public License as
Alain Reguera Delgado b33753
#   published by the Free Software Foundation; either version 2 of the
Alain Reguera Delgado b33753
#   License, or (at your option) any later version.
Alain Reguera Delgado 66223d
#
Alain Reguera Delgado b33753
#   This program is distributed in the hope that it will be useful,
Alain Reguera Delgado b33753
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
Alain Reguera Delgado b33753
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Alain Reguera Delgado b33753
#   General Public License for more details.
Alain Reguera Delgado 66223d
#
Alain Reguera Delgado b33753
#   You should have received a copy of the GNU General Public License
Alain Reguera Delgado b33753
#   along with this program; if not, write to the Free Software
Alain Reguera Delgado b33753
#   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Alain Reguera Delgado 66223d
#
Alain Reguera Delgado b33753
#   Alain Reguera Delgado <al@centos.org.cu>
Alain Reguera Delgado b33753
#   39 Street No. 4426 Cienfuegos, Cuba.
Alain Reguera Delgado 66223d
#
Alain Reguera Delgado 66223d
######################################################################
Alain Reguera Delgado 66223d
Alain Reguera Delgado b33753
# Standardize the way specific functionalities are exported to tcar.sh
Alain Reguera Delgado b33753
# script environment.
Alain Reguera Delgado 66223d
function tcar_setModuleEnvironmentScripts {
Alain Reguera Delgado 66223d
Alain Reguera Delgado ed92ff
    # Reset text domain locally, in order to prevent this function
Alain Reguera Delgado ed92ff
    # from using the last text domain definition. By default all
Alain Reguera Delgado ed92ff
    # common functions do use the same MO file.
Alain Reguera Delgado b33753
    local TEXTDOMAIN="${TCAR_SCRIPT_NAME}"
Alain Reguera Delgado ed92ff
Alain Reguera Delgado 66223d
    # Define the pattern used to retrieve function names from function
Alain Reguera Delgado 66223d
    # files.
Alain Reguera Delgado 66223d
    local FUNCTION_PATTERN="^function[[:space:]]+${TCAR_MODULE_NAME}(_[[:alnum:]]+)?[[:space:]]+{[[:space:]]*$"
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
    # Define the list of files.
Alain Reguera Delgado 66223d
    local TCAR_MODULE_SCRIPT=''
Alain Reguera Delgado 66223d
    local TCAR_MODULE_SCRIPTS="${TCAR_MODULE_INIT_FILE}"
Alain Reguera Delgado 66223d
    if [[ -d ${TCAR_MODULE_DIR} ]];then
Alain Reguera Delgado 66223d
        TCAR_MODULE_SCRIPTS="${TCAR_MODULE_SCRIPTS}
Alain Reguera Delgado 1b4ebe
            $(tcar_getFilesList \
Alain Reguera Delgado 1b4ebe
                -p "${TCAR_MODULE_DIR}/${TCAR_MODULE_NAME}_[[:alnum:]]+\.sh$" \
Alain Reguera Delgado 1b4ebe
                -t 'f' ${TCAR_MODULE_DIR})"
Alain Reguera Delgado 66223d
    fi
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
    # Verify the list of files. If no function file exists for the
Alain Reguera Delgado 66223d
    # location specified stop the script execution. Otherwise the
Alain Reguera Delgado 66223d
    # script will surely try to execute a function that haven't been
Alain Reguera Delgado 66223d
    # exported yet and report an error about it.
Alain Reguera Delgado 66223d
    if [[ -z ${TCAR_MODULE_SCRIPTS} ]];then
Alain Reguera Delgado 66223d
        tcar_printMessage "${FUNCNAME}: `gettext "No function file was found."`" --as-error-line
Alain Reguera Delgado 66223d
    fi
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
    # Process the list of files.
Alain Reguera Delgado 66223d
    for TCAR_MODULE_SCRIPT in ${TCAR_MODULE_SCRIPTS};do
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
        # Verify the execution rights for function file.
Alain Reguera Delgado 66223d
        tcar_checkFiles -ex ${TCAR_MODULE_SCRIPT}
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
        # Retrieve the function's name from function's file.
Alain Reguera Delgado 66223d
        local TCAR_MODULE_SCRIPT_FN=$(egrep "${FUNCTION_PATTERN}" ${TCAR_MODULE_SCRIPT} \
Alain Reguera Delgado 66223d
            | gawk '{ print $2 }')
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
        # Verify function's name. It cannot be an empty value.
Alain Reguera Delgado 66223d
        if [[ -z "${TCAR_MODULE_SCRIPT_FN}" ]];then
Alain Reguera Delgado 66223d
            tcar_printMessage "`gettext "No function definition found."`" --as-error-line
Alain Reguera Delgado 66223d
        fi
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
        # Verify that function files have not been already exported.
Alain Reguera Delgado 66223d
        # If they have been already exported don't export them again.
Alain Reguera Delgado 66223d
        # Instead, continue with the next function file in the list.
Alain Reguera Delgado 66223d
        declare -F | gawk '{ print $3 }' | egrep "^${TCAR_MODULE_SCRIPT}$" > /dev/null
Alain Reguera Delgado 66223d
        if [[ $? -eq 0 ]];then
Alain Reguera Delgado 66223d
            continue
Alain Reguera Delgado 66223d
        fi
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
        # Initialize the function file.
Alain Reguera Delgado 66223d
        . ${TCAR_MODULE_SCRIPT}
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
        # Export the function names inside the file to current shell
Alain Reguera Delgado 66223d
        # script environment.
Alain Reguera Delgado 66223d
        export -f ${TCAR_MODULE_SCRIPT_FN}
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
        tcar_printMessage "export -f : ${TCAR_MODULE_SCRIPT_FN}" --as-debugger-line
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
    done
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
}