Blame tcar-scripts/tcar_setModuleEnvironmentScripts.sh

Alain Reguera Delgado 66223d
#!/bin/bash
Alain Reguera Delgado 66223d
######################################################################
Alain Reguera Delgado 66223d
#
Alain Reguera Delgado 66223d
#   tcar_setModuleEnvironmentScripts.sh -- This function standardizes
Alain Reguera Delgado 66223d
#   the way specific functionalities are exported to tcar.sh
Alain Reguera Delgado 66223d
#   script environment.
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_setModuleEnvironmentScripts {
Alain Reguera Delgado 66223d
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 66223d
            $(tcar_getFilesList ${TCAR_MODULE_DIR} \
Alain Reguera Delgado 66223d
            --pattern="${TCAR_MODULE_DIR}/${TCAR_MODULE_NAME}_[[:alnum:]]+\.sh$" --type='f')"
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
}