Blame tcar-scripts/tcar_setModuleEnvironment.sh

Alain Reguera Delgado e54180
#!/bin/bash
Alain Reguera Delgado e54180
######################################################################
Alain Reguera Delgado e54180
#
Alain Reguera Delgado e54180
#   tcar_setModuleEnvironment.sh -- This function initiates module
Alain Reguera Delgado e54180
#   environments inside the centos-art.sh script.
Alain Reguera Delgado e54180
#
Alain Reguera Delgado e54180
#   Written by:
Alain Reguera Delgado e54180
#   * Alain Reguera Delgado <al@centos.org.cu>, 2009-2013
Alain Reguera Delgado e54180
#
Alain Reguera Delgado e54180
# Copyright (C) 2009-2013 The CentOS Artwork SIG
Alain Reguera Delgado e54180
#
Alain Reguera Delgado e54180
# This program is free software; you can redistribute it and/or modify
Alain Reguera Delgado e54180
# it under the terms of the GNU General Public License as published by
Alain Reguera Delgado e54180
# the Free Software Foundation; either version 2 of the License, or (at
Alain Reguera Delgado e54180
# your option) any later version.
Alain Reguera Delgado e54180
#
Alain Reguera Delgado e54180
# This program is distributed in the hope that it will be useful, but
Alain Reguera Delgado e54180
# WITHOUT ANY WARRANTY; without even the implied warranty of
Alain Reguera Delgado e54180
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Alain Reguera Delgado e54180
# General Public License for more details.
Alain Reguera Delgado e54180
#
Alain Reguera Delgado e54180
# You should have received a copy of the GNU General Public License
Alain Reguera Delgado e54180
# along with this program; if not, write to the Free Software
Alain Reguera Delgado e54180
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Alain Reguera Delgado e54180
#
Alain Reguera Delgado e54180
######################################################################
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
function tcar_setModuleEnvironment {
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
    local ARG_MODULE_NAME=''
Alain Reguera Delgado e54180
    local ARG_MODULE_TYPE=''
Alain Reguera Delgado e54180
    local ARG_MODULE_ARGS=''
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
    # Retrieve module's name and module's type from arguments passed
Alain Reguera Delgado e54180
    # through this function positional parameters.
Alain Reguera Delgado e54180
    OPTIND=1
Alain Reguera Delgado e54180
    while getopts "m:,t:,g:" OPTION "${@}"; do
Alain Reguera Delgado e54180
        case "${OPTION}" in
Alain Reguera Delgado e54180
            m ) ARG_MODULE_NAME="${OPTARG}" ;;
Alain Reguera Delgado e54180
            t ) ARG_MODULE_TYPE="${OPTARG}" ;;
Alain Reguera Delgado e54180
            g ) ARG_MODULE_ARGS="${OPTARG} ${ARG_MODULE_ARGS}" ;;
Alain Reguera Delgado e54180
        esac
Alain Reguera Delgado e54180
    done
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
    # Clean up positional parameters to reflect the fact that options
Alain Reguera Delgado e54180
    # have been processed already.
Alain Reguera Delgado e54180
    shift $(( ${OPTIND} - 1 ))
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
    # Initialize module's global counter.
Alain Reguera Delgado e54180
    TCAR_MODULE_COUNT=${TCAR_MODULE_COUNT:-0}
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
    # When the last module in the chain of executed modules is the
Alain Reguera Delgado e54180
    # same module being currently executed, don't create a new
Alain Reguera Delgado e54180
    # position for it in the chain of modules. Instead, use the
Alain Reguera Delgado e54180
    # information it already has from its previous execution. In order
Alain Reguera Delgado e54180
    # for this to work, the current module must be executed as sibling
Alain Reguera Delgado e54180
    # module of other module or itself.
Alain Reguera Delgado e54180
    if [[ ${TCAR_MODULE_COUNT} -gt 0 ]];then
Alain Reguera Delgado e54180
        if [[ ${TCAR_MODULE_NAMES[((${TCAR_MODULE_COUNT} - 1))]} == ${ARG_MODULE_NAME} ]];then
Alain Reguera Delgado e54180
            if [[ ${ARG_MODULE_TYPE} == 'sibling' ]];then
Alain Reguera Delgado e54180
                tcar_printMessage '~~~~~~~~~~~~~~~~~~~~~~~~~> : '"${TCAR_MODULE_NAME} ${TCAR_MODULE_ARGUMENT}" --as-debugger-line
Alain Reguera Delgado e54180
                ${ARG_MODULE_NAME} ${ARG_MODULE_ARGS} ${@}
Alain Reguera Delgado e54180
                return
Alain Reguera Delgado e54180
            fi
Alain Reguera Delgado e54180
        fi
Alain Reguera Delgado e54180
    fi
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
    tcar_printMessage '=========================>: ['${TCAR_MODULE_COUNT}'] | '${FUNCNAME[1]} --as-debugger-line
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
    # Define module's base directory. This is the directory where the
Alain Reguera Delgado e54180
    # initialization script is stored in.
Alain Reguera Delgado e54180
    local TCAR_MODULE_BASEDIR=${TCAR_SCRIPT_MODULES_BASEDIR}
Alain Reguera Delgado e54180
    if [[ ${#TCAR_MODULE_BASEDIRS[*]} -gt 0 ]];then
Alain Reguera Delgado e54180
        if [[ ${ARG_MODULE_TYPE} == "parent" ]];then
Alain Reguera Delgado e54180
            TCAR_MODULE_BASEDIR=${TCAR_SCRIPT_MODULES_BASEDIR}
Alain Reguera Delgado e54180
        elif [[ ${ARG_MODULE_TYPE} == "sibling" ]];then
Alain Reguera Delgado e54180
            if [[ ${TCAR_MODULE_TYPES[((${TCAR_MODULE_COUNT} - 1 ))]} == 'sibling' ]];then
Alain Reguera Delgado e54180
                TCAR_MODULE_BASEDIR=${TCAR_MODULE_BASEDIRS[((${TCAR_MODULE_COUNT}-2))]}
Alain Reguera Delgado e54180
            else
Alain Reguera Delgado e54180
                TCAR_MODULE_BASEDIR=${TCAR_MODULE_BASEDIRS[((${TCAR_MODULE_COUNT}-1))]}
Alain Reguera Delgado e54180
            fi
Alain Reguera Delgado e54180
        else
Alain Reguera Delgado e54180
            TCAR_MODULE_BASEDIR=${TCAR_MODULE_BASEDIRS[${TCAR_MODULE_COUNT}]}
Alain Reguera Delgado e54180
        fi
Alain Reguera Delgado e54180
    fi
Alain Reguera Delgado e54180
    tcar_printMessage "TCAR_MODULE_BASEDIR : ${TCAR_MODULE_BASEDIR}" --as-debugger-line
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
    # Define module's name.
Alain Reguera Delgado e54180
    TCAR_MODULE_NAMES[${TCAR_MODULE_COUNT}]=$(tcar_getRepoName "${ARG_MODULE_NAME:-unknown}" "-f" | cut -d '-' -f1)
Alain Reguera Delgado e54180
    local TCAR_MODULE_NAME=${TCAR_MODULE_NAMES[${TCAR_MODULE_COUNT}]}
Alain Reguera Delgado e54180
    tcar_printMessage "TCAR_MODULE_NAME : [${TCAR_MODULE_COUNT}]=${TCAR_MODULE_NAME}" --as-debugger-line
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
    # Define module's type.
Alain Reguera Delgado e54180
    TCAR_MODULE_TYPES[${TCAR_MODULE_COUNT}]="${ARG_MODULE_TYPE:-parent}"
Alain Reguera Delgado e54180
    local TCAR_MODULE_TYPE=${TCAR_MODULE_TYPES[${TCAR_MODULE_COUNT}]}
Alain Reguera Delgado e54180
    tcar_printMessage "TCAR_MODULE_TYPE : ${TCAR_MODULE_TYPE}" --as-debugger-line
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
    # Define module's arguments.  This variable is used in different
Alain Reguera Delgado e54180
    # module environments to pass positional parameters from one
Alain Reguera Delgado e54180
    # environment to another using local definitions.
Alain Reguera Delgado e54180
    TCAR_MODULE_ARGUMENTS[${TCAR_MODULE_COUNT}]="${ARG_MODULE_ARGS:-} ${@}"
Alain Reguera Delgado e54180
    local TCAR_MODULE_ARGUMENT=${TCAR_MODULE_ARGUMENTS[${TCAR_MODULE_COUNT}]}
Alain Reguera Delgado e54180
    tcar_printMessage "TCAR_MODULE_ARGUMENT : ${TCAR_MODULE_ARGUMENT}" --as-debugger-line
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
    # Check module's name possible values.
Alain Reguera Delgado e54180
    tcar_checkModuleName
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
    # Define module's directory.
Alain Reguera Delgado e54180
    TCAR_MODULE_DIRS[${TCAR_MODULE_COUNT}]=${TCAR_MODULE_BASEDIR}/$(tcar_getRepoName "${TCAR_MODULE_NAME}" "-d")
Alain Reguera Delgado e54180
    local TCAR_MODULE_DIR=${TCAR_MODULE_DIRS[${TCAR_MODULE_COUNT}]}
Alain Reguera Delgado e54180
    tcar_printMessage "TCAR_MODULE_DIR : ${TCAR_MODULE_DIR}" --as-debugger-line
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
    # Define module's directories not reused from module's parent
Alain Reguera Delgado e54180
    # directory structure.
Alain Reguera Delgado e54180
    TCAR_MODULE_DIRS_MODULES[${TCAR_MODULE_COUNT}]=${TCAR_MODULE_DIR}/Modules
Alain Reguera Delgado e54180
    local TCAR_MODULE_DIR_MODULES=${TCAR_MODULE_DIRS_MODULES[${TCAR_MODULE_COUNT}]}
Alain Reguera Delgado e54180
    tcar_printMessage "TCAR_MODULE_DIR_MODULES : ${TCAR_MODULE_DIR_MODULES}" --as-debugger-line
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
    TCAR_MODULE_DIRS_CONFIGS[${TCAR_MODULE_COUNT}]=${TCAR_MODULE_DIR}/Configs
Alain Reguera Delgado e54180
    local TCAR_MODULE_DIR_CONFIGS=${TCAR_MODULE_DIRS_CONFIGS[${TCAR_MODULE_COUNT}]}
Alain Reguera Delgado e54180
    tcar_printMessage "TCAR_MODULE_DIR_CONFIGS : ${TCAR_MODULE_DIR_CONFIGS}" --as-debugger-line
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
    # Define module's directories reused from module's parent
Alain Reguera Delgado e54180
    # directory structure.
Alain Reguera Delgado e54180
    TCAR_MODULE_DIRS_MANUALS[${TCAR_MODULE_COUNT}]=${TCAR_MODULE_DIRS[0]}/Manuals
Alain Reguera Delgado e54180
    local TCAR_MODULE_DIR_MANUALS=${TCAR_MODULE_DIRS_MANUALS[${TCAR_MODULE_COUNT}]}
Alain Reguera Delgado e54180
    tcar_printMessage "TCAR_MODULE_DIR_MANUALS : ${TCAR_MODULE_DIR_MANUALS}" --as-debugger-line
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
    TCAR_MODULE_DIRS_LOCALES[${TCAR_MODULE_COUNT}]=${TCAR_MODULE_DIRS[0]}/Locales
Alain Reguera Delgado e54180
    local TCAR_MODULE_DIR_LOCALES=${TCAR_MODULE_DIRS_LOCALES[${TCAR_MODULE_COUNT}]}
Alain Reguera Delgado e54180
    tcar_printMessage "TCAR_MODULE_DIR_LOCALES : ${TCAR_MODULE_DIR_LOCALES}" --as-debugger-line
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
    # Define module's initialization file.
Alain Reguera Delgado e54180
    TCAR_MODULE_INIT_FILES[${TCAR_MODULE_COUNT}]=${TCAR_MODULE_DIR}/${TCAR_MODULE_NAME}.sh
Alain Reguera Delgado e54180
    local TCAR_MODULE_INIT_FILE=${TCAR_MODULE_INIT_FILES[${TCAR_MODULE_COUNT}]}
Alain Reguera Delgado e54180
    tcar_printMessage "TCAR_MODULE_INIT_FILE : ${TCAR_MODULE_INIT_FILE}" --as-debugger-line
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
    # Define module's connection with their localization files. It is
Alain Reguera Delgado e54180
    # required that gettext-specific variables be defined locally, in
Alain Reguera Delgado e54180
    # order to implement per-module localization.
Alain Reguera Delgado e54180
    local TEXTDOMAIN=$(basename ${TCAR_MODULE_INIT_FILE})
Alain Reguera Delgado e54180
    tcar_printMessage "TEXTDOMAIN: ${TEXTDOMAIN}" --as-debugger-line
Alain Reguera Delgado e54180
    local TEXTDOMAINDIR=${TCAR_MODULE_DIR_LOCALES}
Alain Reguera Delgado e54180
    tcar_printMessage "TEXTDOMAINDIR: ${TEXTDOMAINDIR}" --as-debugger-line
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
    # Increment module's counter just before creating next module's
Alain Reguera Delgado e54180
    # base directory.
Alain Reguera Delgado e54180
    TCAR_MODULE_COUNT=$(( ${TCAR_MODULE_COUNT} + 1 ))
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
    # Define next module's base directory.
Alain Reguera Delgado e54180
    TCAR_MODULE_BASEDIRS[${TCAR_MODULE_COUNT}]=${TCAR_MODULE_DIR_MODULES}
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
    # Check function script execution rights.
Alain Reguera Delgado e54180
    tcar_checkFiles -ex ${TCAR_MODULE_INIT_FILE}
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
    # Load module-specific (function) scripts into current execution
Alain Reguera Delgado e54180
    # environment.  Keep the tcar_setModuleEnvironmentScripts function
Alain Reguera Delgado e54180
    # call after all variables and arguments definitions.
Alain Reguera Delgado e54180
    tcar_setModuleEnvironmentScripts
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
    # Execute module's initialization script with its arguments.
Alain Reguera Delgado e54180
    tcar_printMessage '-------------------------> : '"${TCAR_MODULE_NAME} ${TCAR_MODULE_ARGUMENT}" --as-debugger-line
Alain Reguera Delgado e54180
    ${TCAR_MODULE_NAME} ${TCAR_MODULE_ARGUMENT}
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
    # Unset module-specific environment.
Alain Reguera Delgado e54180
    tcar_printMessage '<------------------------- : '"${TCAR_MODULE_NAME} ${TCAR_MODULE_ARGUMENT}" --as-debugger-line
Alain Reguera Delgado e54180
    tcar_unsetModuleEnvironment
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
    # Decrement module counter just after unset unused module
Alain Reguera Delgado e54180
    # environments.
Alain Reguera Delgado e54180
    TCAR_MODULE_COUNT=$(( ${TCAR_MODULE_COUNT} - 1 ))
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
    # Unset array and non-array variables used in this function.
Alain Reguera Delgado e54180
    if [[ ${TCAR_MODULE_COUNT} -eq 0 ]];then
Alain Reguera Delgado e54180
        unset TCAR_MODULE_NAMES
Alain Reguera Delgado e54180
        unset TCAR_MODULE_BASEDIRS
Alain Reguera Delgado e54180
        unset TCAR_MODULE_DIRS
Alain Reguera Delgado e54180
        unset TCAR_MODULE_DIRS_MODULES
Alain Reguera Delgado e54180
        unset TCAR_MODULE_DIRS_MANUALS
Alain Reguera Delgado e54180
        unset TCAR_MODULE_DIRS_LOCALES
Alain Reguera Delgado e54180
        unset TCAR_MODULE_DIRS_CONFIGS
Alain Reguera Delgado e54180
        unset TCAR_MODULE_NAME
Alain Reguera Delgado e54180
        unset TCAR_MODULE_DIR
Alain Reguera Delgado e54180
        unset TCAR_MODULE_DIR_MODULES
Alain Reguera Delgado e54180
        unset TCAR_MODULE_DIR_MANUALS
Alain Reguera Delgado e54180
        unset TCAR_MODULE_DIR_LOCALES
Alain Reguera Delgado e54180
        unset TCAR_MODULE_DIR_CONFIGS
Alain Reguera Delgado e54180
    fi
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
    tcar_printMessage '<=========================: ['${TCAR_MODULE_COUNT}'] | '${FUNCNAME[1]} --as-debugger-line
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
}