Blame tcar-scripts/tcar_setModuleEnvironment.sh

Alain Reguera Delgado 66223d
#!/bin/bash
Alain Reguera Delgado 66223d
######################################################################
Alain Reguera Delgado 66223d
#
Alain Reguera Delgado 66223d
#   tcar_setModuleEnvironment.sh -- This function initiates module
Alain Reguera Delgado 66223d
#   environments inside the tcar.sh script.
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_setModuleEnvironment {
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
    local ARG_MODULE_NAME=''
Alain Reguera Delgado 66223d
    local ARG_MODULE_TYPE=''
Alain Reguera Delgado 66223d
    local ARG_MODULE_ARGS=''
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
    # Retrieve module's name and module's type from arguments passed
Alain Reguera Delgado 66223d
    # through this function positional parameters.
Alain Reguera Delgado 66223d
    OPTIND=1
Alain Reguera Delgado 66223d
    while getopts "m:,t:,g:" OPTION "${@}"; do
Alain Reguera Delgado 66223d
        case "${OPTION}" in
Alain Reguera Delgado 66223d
            m ) ARG_MODULE_NAME="${OPTARG}" ;;
Alain Reguera Delgado 66223d
            t ) ARG_MODULE_TYPE="${OPTARG}" ;;
Alain Reguera Delgado 66223d
            g ) ARG_MODULE_ARGS="${OPTARG} ${ARG_MODULE_ARGS}" ;;
Alain Reguera Delgado 66223d
        esac
Alain Reguera Delgado 66223d
    done
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
    # Clean up positional parameters to reflect the fact that options
Alain Reguera Delgado 66223d
    # have been processed already.
Alain Reguera Delgado 66223d
    shift $(( ${OPTIND} - 1 ))
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
    # Initialize module's global counter.
Alain Reguera Delgado 66223d
    TCAR_MODULE_COUNT=${TCAR_MODULE_COUNT:-0}
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
    # When the last module in the chain of executed modules is the
Alain Reguera Delgado 66223d
    # same module being currently executed, don't create a new
Alain Reguera Delgado 66223d
    # position for it in the chain of modules. Instead, use the
Alain Reguera Delgado 66223d
    # information it already has from its previous execution. In order
Alain Reguera Delgado 66223d
    # for this to work, the current module must be executed as sibling
Alain Reguera Delgado 66223d
    # module of other module or itself.
Alain Reguera Delgado 66223d
    if [[ ${TCAR_MODULE_COUNT} -gt 0 ]];then
Alain Reguera Delgado 66223d
        if [[ ${TCAR_MODULE_NAMES[((${TCAR_MODULE_COUNT} - 1))]} == ${ARG_MODULE_NAME} ]];then
Alain Reguera Delgado 66223d
            if [[ ${ARG_MODULE_TYPE} == 'sibling' ]];then
Alain Reguera Delgado 66223d
                tcar_printMessage '~~~~~~~~~~~~~~~~~~~~~~~~~> : '"${TCAR_MODULE_NAME} ${TCAR_MODULE_ARGUMENT}" --as-debugger-line
Alain Reguera Delgado 66223d
                ${ARG_MODULE_NAME} ${ARG_MODULE_ARGS} ${@}
Alain Reguera Delgado 66223d
                return
Alain Reguera Delgado 66223d
            fi
Alain Reguera Delgado 66223d
        fi
Alain Reguera Delgado 66223d
    fi
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
    tcar_printMessage '=========================>: ['${TCAR_MODULE_COUNT}'] | '${FUNCNAME[1]} --as-debugger-line
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
    # Define module's base directory. This is the directory where the
Alain Reguera Delgado 66223d
    # initialization script is stored in.
Alain Reguera Delgado 66223d
    local TCAR_MODULE_BASEDIR=${TCAR_SCRIPT_MODULES_BASEDIR}
Alain Reguera Delgado 66223d
    if [[ ${#TCAR_MODULE_BASEDIRS[*]} -gt 0 ]];then
Alain Reguera Delgado 66223d
        if [[ ${ARG_MODULE_TYPE} == "parent" ]];then
Alain Reguera Delgado 66223d
            TCAR_MODULE_BASEDIR=${TCAR_SCRIPT_MODULES_BASEDIR}
Alain Reguera Delgado 66223d
        elif [[ ${ARG_MODULE_TYPE} == "sibling" ]];then
Alain Reguera Delgado 66223d
            if [[ ${TCAR_MODULE_TYPES[((${TCAR_MODULE_COUNT} - 1 ))]} == 'sibling' ]];then
Alain Reguera Delgado 66223d
                TCAR_MODULE_BASEDIR=${TCAR_MODULE_BASEDIRS[((${TCAR_MODULE_COUNT}-2))]}
Alain Reguera Delgado 66223d
            else
Alain Reguera Delgado 66223d
                TCAR_MODULE_BASEDIR=${TCAR_MODULE_BASEDIRS[((${TCAR_MODULE_COUNT}-1))]}
Alain Reguera Delgado 66223d
            fi
Alain Reguera Delgado 66223d
        else
Alain Reguera Delgado 66223d
            TCAR_MODULE_BASEDIR=${TCAR_MODULE_BASEDIRS[${TCAR_MODULE_COUNT}]}
Alain Reguera Delgado 66223d
        fi
Alain Reguera Delgado 66223d
    fi
Alain Reguera Delgado 66223d
    tcar_printMessage "TCAR_MODULE_BASEDIR : ${TCAR_MODULE_BASEDIR}" --as-debugger-line
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
    # Define module's name.
Alain Reguera Delgado 66223d
    TCAR_MODULE_NAMES[${TCAR_MODULE_COUNT}]=$(tcar_getRepoName "${ARG_MODULE_NAME:-unknown}" "-f" | cut -d '-' -f1)
Alain Reguera Delgado 66223d
    local TCAR_MODULE_NAME=${TCAR_MODULE_NAMES[${TCAR_MODULE_COUNT}]}
Alain Reguera Delgado 66223d
    tcar_printMessage "TCAR_MODULE_NAME : [${TCAR_MODULE_COUNT}]=${TCAR_MODULE_NAME}" --as-debugger-line
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
    # Define module's type.
Alain Reguera Delgado 66223d
    TCAR_MODULE_TYPES[${TCAR_MODULE_COUNT}]="${ARG_MODULE_TYPE:-parent}"
Alain Reguera Delgado 66223d
    local TCAR_MODULE_TYPE=${TCAR_MODULE_TYPES[${TCAR_MODULE_COUNT}]}
Alain Reguera Delgado 66223d
    tcar_printMessage "TCAR_MODULE_TYPE : ${TCAR_MODULE_TYPE}" --as-debugger-line
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
    # Define module's arguments.  This variable is used in different
Alain Reguera Delgado 66223d
    # module environments to pass positional parameters from one
Alain Reguera Delgado 66223d
    # environment to another using local definitions.
Alain Reguera Delgado 66223d
    TCAR_MODULE_ARGUMENTS[${TCAR_MODULE_COUNT}]="${ARG_MODULE_ARGS:-} ${@}"
Alain Reguera Delgado 66223d
    local TCAR_MODULE_ARGUMENT=${TCAR_MODULE_ARGUMENTS[${TCAR_MODULE_COUNT}]}
Alain Reguera Delgado 66223d
    tcar_printMessage "TCAR_MODULE_ARGUMENT : ${TCAR_MODULE_ARGUMENT}" --as-debugger-line
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
    # Check module's name possible values.
Alain Reguera Delgado 66223d
    tcar_checkModuleName
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
    # Define module's directory.
Alain Reguera Delgado d8927e
    TCAR_MODULE_DIRS[${TCAR_MODULE_COUNT}]=${TCAR_MODULE_BASEDIR}/$(tcar_getRepoName "${TCAR_MODULE_NAME}" "-d")
Alain Reguera Delgado 66223d
    local TCAR_MODULE_DIR=${TCAR_MODULE_DIRS[${TCAR_MODULE_COUNT}]}
Alain Reguera Delgado 66223d
    tcar_printMessage "TCAR_MODULE_DIR : ${TCAR_MODULE_DIR}" --as-debugger-line
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
    # Define module's directories not reused from module's parent
Alain Reguera Delgado 66223d
    # directory structure.
Alain Reguera Delgado d8927e
    TCAR_MODULE_DIRS_MODULES[${TCAR_MODULE_COUNT}]=${TCAR_MODULE_DIR}/Modules
Alain Reguera Delgado 66223d
    local TCAR_MODULE_DIR_MODULES=${TCAR_MODULE_DIRS_MODULES[${TCAR_MODULE_COUNT}]}
Alain Reguera Delgado 66223d
    tcar_printMessage "TCAR_MODULE_DIR_MODULES : ${TCAR_MODULE_DIR_MODULES}" --as-debugger-line
Alain Reguera Delgado 66223d
Alain Reguera Delgado d8927e
    TCAR_MODULE_DIRS_CONFIGS[${TCAR_MODULE_COUNT}]=${TCAR_MODULE_DIR}/Configs
Alain Reguera Delgado 66223d
    local TCAR_MODULE_DIR_CONFIGS=${TCAR_MODULE_DIRS_CONFIGS[${TCAR_MODULE_COUNT}]}
Alain Reguera Delgado 66223d
    tcar_printMessage "TCAR_MODULE_DIR_CONFIGS : ${TCAR_MODULE_DIR_CONFIGS}" --as-debugger-line
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
    # Define module's directories reused from module's parent
Alain Reguera Delgado 66223d
    # directory structure.
Alain Reguera Delgado d8927e
    TCAR_MODULE_DIRS_MANUALS[${TCAR_MODULE_COUNT}]=${TCAR_MODULE_DIRS[0]}/Manuals
Alain Reguera Delgado 66223d
    local TCAR_MODULE_DIR_MANUALS=${TCAR_MODULE_DIRS_MANUALS[${TCAR_MODULE_COUNT}]}
Alain Reguera Delgado 66223d
    tcar_printMessage "TCAR_MODULE_DIR_MANUALS : ${TCAR_MODULE_DIR_MANUALS}" --as-debugger-line
Alain Reguera Delgado 66223d
Alain Reguera Delgado d8927e
    TCAR_MODULE_DIRS_LOCALES[${TCAR_MODULE_COUNT}]=${TCAR_MODULE_DIRS[0]}/Locales
Alain Reguera Delgado 66223d
    local TCAR_MODULE_DIR_LOCALES=${TCAR_MODULE_DIRS_LOCALES[${TCAR_MODULE_COUNT}]}
Alain Reguera Delgado 66223d
    tcar_printMessage "TCAR_MODULE_DIR_LOCALES : ${TCAR_MODULE_DIR_LOCALES}" --as-debugger-line
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
    # Define module's initialization file.
Alain Reguera Delgado 66223d
    TCAR_MODULE_INIT_FILES[${TCAR_MODULE_COUNT}]=${TCAR_MODULE_DIR}/${TCAR_MODULE_NAME}.sh
Alain Reguera Delgado 66223d
    local TCAR_MODULE_INIT_FILE=${TCAR_MODULE_INIT_FILES[${TCAR_MODULE_COUNT}]}
Alain Reguera Delgado 66223d
    tcar_printMessage "TCAR_MODULE_INIT_FILE : ${TCAR_MODULE_INIT_FILE}" --as-debugger-line
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
    # Define module's connection with their localization files. It is
Alain Reguera Delgado 66223d
    # required that gettext-specific variables be defined locally, in
Alain Reguera Delgado 66223d
    # order to implement per-module localization.
Alain Reguera Delgado 66223d
    local TEXTDOMAIN=$(basename ${TCAR_MODULE_INIT_FILE})
Alain Reguera Delgado 66223d
    tcar_printMessage "TEXTDOMAIN: ${TEXTDOMAIN}" --as-debugger-line
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
    # Increment module's counter just before creating next module's
Alain Reguera Delgado 66223d
    # base directory.
Alain Reguera Delgado 66223d
    TCAR_MODULE_COUNT=$(( ${TCAR_MODULE_COUNT} + 1 ))
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
    # Define next module's base directory.
Alain Reguera Delgado 66223d
    TCAR_MODULE_BASEDIRS[${TCAR_MODULE_COUNT}]=${TCAR_MODULE_DIR_MODULES}
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
    # Check function script execution rights.
Alain Reguera Delgado 66223d
    tcar_checkFiles -ex ${TCAR_MODULE_INIT_FILE}
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
    # Load module-specific (function) scripts into current execution
Alain Reguera Delgado 66223d
    # environment.  Keep the tcar_setModuleEnvironmentScripts function
Alain Reguera Delgado 66223d
    # call after all variables and arguments definitions.
Alain Reguera Delgado 66223d
    tcar_setModuleEnvironmentScripts
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
    # Execute module's initialization script with its arguments.
Alain Reguera Delgado 66223d
    tcar_printMessage '-------------------------> : '"${TCAR_MODULE_NAME} ${TCAR_MODULE_ARGUMENT}" --as-debugger-line
Alain Reguera Delgado 66223d
    ${TCAR_MODULE_NAME} ${TCAR_MODULE_ARGUMENT}
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
    # Unset module-specific environment.
Alain Reguera Delgado 66223d
    tcar_printMessage '<------------------------- : '"${TCAR_MODULE_NAME} ${TCAR_MODULE_ARGUMENT}" --as-debugger-line
Alain Reguera Delgado 66223d
    tcar_unsetModuleEnvironment
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
    # Decrement module counter just after unset unused module
Alain Reguera Delgado 66223d
    # environments.
Alain Reguera Delgado 66223d
    TCAR_MODULE_COUNT=$(( ${TCAR_MODULE_COUNT} - 1 ))
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
    # Unset array and non-array variables used in this function.
Alain Reguera Delgado 66223d
    if [[ ${TCAR_MODULE_COUNT} -eq 0 ]];then
Alain Reguera Delgado 66223d
        unset TCAR_MODULE_NAMES
Alain Reguera Delgado 66223d
        unset TCAR_MODULE_BASEDIRS
Alain Reguera Delgado 66223d
        unset TCAR_MODULE_DIRS
Alain Reguera Delgado 66223d
        unset TCAR_MODULE_DIRS_MODULES
Alain Reguera Delgado 66223d
        unset TCAR_MODULE_DIRS_MANUALS
Alain Reguera Delgado 66223d
        unset TCAR_MODULE_DIRS_LOCALES
Alain Reguera Delgado 66223d
        unset TCAR_MODULE_DIRS_CONFIGS
Alain Reguera Delgado 66223d
        unset TCAR_MODULE_NAME
Alain Reguera Delgado 66223d
        unset TCAR_MODULE_DIR
Alain Reguera Delgado 66223d
        unset TCAR_MODULE_DIR_MODULES
Alain Reguera Delgado 66223d
        unset TCAR_MODULE_DIR_MANUALS
Alain Reguera Delgado 66223d
        unset TCAR_MODULE_DIR_LOCALES
Alain Reguera Delgado 66223d
        unset TCAR_MODULE_DIR_CONFIGS
Alain Reguera Delgado 66223d
    fi
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
    tcar_printMessage '<=========================: ['${TCAR_MODULE_COUNT}'] | '${FUNCNAME[1]} --as-debugger-line
Alain Reguera Delgado 66223d
Alain Reguera Delgado 66223d
}