Blame Automation/Scripts/tcar_exportFunctions.sh

Alain Reguera Delgado 8f60cb
#!/bin/bash
Alain Reguera Delgado 615395
######################################################################
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado eb3010
#   tcar_exportFunctions.sh -- This function standardizes the way
Alain Reguera Delgado 615395
#   specific functionalities are exported to centos-art.sh script
Alain Reguera Delgado 615395
#   environment.
Alain Reguera Delgado 615395
#
Alain Reguera Delgado 615395
#   Written by: 
Alain Reguera Delgado 615395
#   * Alain Reguera Delgado <al@centos.org.cu>, 2009-2013
Alain Reguera Delgado 615395
#     Key fingerprint = D67D 0F82 4CBD 90BC 6421  DF28 7CCE 757C 17CA 3951
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 8f60cb
# Copyright (C) 2009-2013 The CentOS Project
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 8f60cb
# This program is free software; you can redistribute it and/or modify
Alain Reguera Delgado 8f60cb
# it under the terms of the GNU General Public License as published by
Alain Reguera Delgado 8f60cb
# the Free Software Foundation; either version 2 of the License, or (at
Alain Reguera Delgado 8f60cb
# your option) any later version.
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 8f60cb
# This program is distributed in the hope that it will be useful, but
Alain Reguera Delgado 8f60cb
# WITHOUT ANY WARRANTY; without even the implied warranty of
Alain Reguera Delgado 8f60cb
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Alain Reguera Delgado 8f60cb
# General Public License for more details.
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 8f60cb
# You should have received a copy of the GNU General Public License
Alain Reguera Delgado 8f60cb
# along with this program; if not, write to the Free Software
Alain Reguera Delgado 8f60cb
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 615395
######################################################################
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado eb3010
function tcar_exportFunctions {
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Retrieve export identifier for the function we want to export.
Alain Reguera Delgado 0de111
    local MODULE_INIT_FILE="${1}"
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 0de111
    # Verify the export identification existence.
Alain Reguera Delgado eb3010
    tcar_checkFiles -x ${MODULE_INIT_FILE}
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Define the source location where function files are placed in.
Alain Reguera Delgado 0de111
    local MODULE_SCRIPTS_DIR=$(dirname ${MODULE_INIT_FILE})/Scripts
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Define suffix used to retrieve function files.
Alain Reguera Delgado 0de111
    local MODULE_INIT_FILENAME=$(basename "${MODULE_INIT_FILE}" | sed -r 's/\.sh$//')
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Define the pattern used to retrieve function names from function
Alain Reguera Delgado 8f60cb
    # files.
Alain Reguera Delgado 0de111
    local FUNCTION_PATTERN="^function[[:space:]]+${MODULE_INIT_FILENAME}(_[[:alnum:]]+)?[[:space:]]+{[[:space:]]*$"
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Define the list of files.
Alain Reguera Delgado ddf7d2
    local MODULE_SCRIPTS="${MODULE_INIT_FILE}
Alain Reguera Delgado eb3010
        $(tcar_getFilesList ${MODULE_SCRIPTS_DIR} \
Alain Reguera Delgado 0de111
        --pattern="${MODULE_DIR}/.+\.sh$" --maxdepth='1' \
Alain Reguera Delgado 0de111
        --mindepth='1' --type='f')"
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Verify the list of files. If no function file exists for the
Alain Reguera Delgado 8f60cb
    # location specified stop the script execution. Otherwise the
Alain Reguera Delgado 8f60cb
    # script will surely try to execute a function that haven't been
Alain Reguera Delgado 8f60cb
    # exported yet and report an error about it.
Alain Reguera Delgado ddf7d2
    if [[ -z ${MODULE_SCRIPTS} ]];then
Alain Reguera Delgado eb3010
        tcar_printMessage "${FUNCNAME}: `gettext "No function file was found."`" --as-error-line
Alain Reguera Delgado 8f60cb
    fi
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Process the list of files.
Alain Reguera Delgado ddf7d2
    for MODULE_SCRIPT in ${MODULE_SCRIPTS};do
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        # Verify the execution rights for function file.
Alain Reguera Delgado eb3010
        tcar_checkFiles -x ${MODULE_SCRIPT}
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        # Verify that function files have not been already exported.
Alain Reguera Delgado 8f60cb
        # If they have been already exported don't export them again.
Alain Reguera Delgado 8f60cb
        # Instead, continue with the next function file in the list.
Alain Reguera Delgado ddf7d2
        declare -F | gawk '{ print $3 }' | egrep "^${MODULE_SCRIPT}$" > /dev/null
Alain Reguera Delgado 8f60cb
        if [[ $? -eq 0 ]];then
Alain Reguera Delgado 8f60cb
            continue
Alain Reguera Delgado 8f60cb
        fi
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        # Initialize the function file.
Alain Reguera Delgado ddf7d2
        . ${MODULE_SCRIPT}
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        # Export the function names inside the file to current shell
Alain Reguera Delgado 8f60cb
        # script environment.
Alain Reguera Delgado ddf7d2
        export -f $(egrep "${FUNCTION_PATTERN}" ${MODULE_SCRIPT} | gawk '{ print $2 }')
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    done
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
}