Blob Blame History Raw
#!/bin/bash
######################################################################
#
#   tcar_setModuleEnvironment.sh -- This function initiates module
#   environments inside the centos-art.sh script.
#
#   Written by:
#   * Alain Reguera Delgado <al@centos.org.cu>, 2009-2013
#
# Copyright (C) 2009-2013 The CentOS Artwork SIG
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
######################################################################

function tcar_setModuleEnvironment {

    local ARG_MODULE_NAME=''
    local ARG_MODULE_TYPE=''
    local ARG_MODULE_ARGS=''

    # Retrieve module's name and module's type from arguments passed
    # through this function positional parameters.
    OPTIND=1
    while getopts "m:,t:,g:" OPTION "${@}"; do
        case "${OPTION}" in
            m ) ARG_MODULE_NAME="${OPTARG}" ;;
            t ) ARG_MODULE_TYPE="${OPTARG}" ;;
            g ) ARG_MODULE_ARGS="${OPTARG} ${ARG_MODULE_ARGS}" ;;
        esac
    done

    # Clean up positional parameters to reflect the fact that options
    # have been processed already.
    shift $(( ${OPTIND} - 1 ))

    # Define default values for module name and type passed from
    # arguments.
    ARG_MODULE_NAME=${ARG_MODULE_NAME:-'unknown'}
    ARG_MODULE_TYPE=${ARG_MODULE_TYPE:-'top-module'}
    ARG_MODULE_ARGS=${ARG_MODULE_ARGS:-''}

    # Initialize module's global counter.
    TCAR_MODULE_COUNT=${TCAR_MODULE_COUNT:-0}

    tcar_printMessage "FUNCNAME : ${FUNCNAME[1]} ( ${ARG_MODULE_NAME}, ${ARG_MODULE_TYPE} )" --as-debugger-line

    # Define module's base directory. This is the directory where the
    # initialization script is stored in.
    local TCAR_MODULE_BASEDIR=${TCAR_SCRIPT_MODULES_BASEDIR}
    if [[ ${#TCAR_MODULE_BASEDIRS[*]} -gt 0 ]];then
        if [[ ${ARG_MODULE_TYPE} == "top-module" ]];then
            TCAR_MODULE_BASEDIR=${TCAR_SCRIPT_MODULES_BASEDIR}
        elif [[ ${ARG_MODULE_TYPE} == "sib-module" ]];then
            TCAR_MODULE_BASEDIR=${TCAR_MODULE_BASEDIRS[((${TCAR_MODULE_COUNT}-1))]}
        else
            TCAR_MODULE_BASEDIR=${TCAR_MODULE_BASEDIRS[${TCAR_MODULE_COUNT}]}
        fi
    fi
    tcar_printMessage "TCAR_MODULE_BASEDIR : ${TCAR_MODULE_BASEDIR}" --as-debugger-line

    # Define module's name.
    TCAR_MODULE_NAMES[${TCAR_MODULE_COUNT}]=$(tcar_getRepoName "${ARG_MODULE_NAME}" "-f" | cut -d '-' -f1)
    local TCAR_MODULE_NAME=${TCAR_MODULE_NAMES[${TCAR_MODULE_COUNT}]}
    tcar_printMessage "TCAR_MODULE_NAME : [${TCAR_MODULE_COUNT}]=${TCAR_MODULE_NAME} | ${#TCAR_MODULE_NAMES[*]}" --as-debugger-line

    # Define module's arguments.  This variable is used in different
    # module environments to pass positional parameters from one
    # environment to another using local definitions.
    TCAR_MODULE_ARGUMENTS[${TCAR_MODULE_COUNT}]="${ARG_MODULE_ARGS} ${@}"
    local TCAR_MODULE_ARGUMENT=${TCAR_MODULE_ARGUMENTS[${TCAR_MODULE_COUNT}]}
    tcar_printMessage "TCAR_MODULE_ARGUMENT : ${TCAR_MODULE_ARGUMENT}" --as-debugger-line

    # Check module's name possible values.
    tcar_checkModuleName

    # Define module's directory.
    TCAR_MODULE_DIRS[${TCAR_MODULE_COUNT}]=${TCAR_MODULE_BASEDIR}/$(tcar_getRepoName "${TCAR_MODULE_NAME}" "-d")
    local TCAR_MODULE_DIR=${TCAR_MODULE_DIRS[${TCAR_MODULE_COUNT}]}
    tcar_printMessage "TCAR_MODULE_DIR : ${TCAR_MODULE_DIR}" --as-debugger-line

    # Define module's related directories.
    TCAR_MODULE_DIRS_MODULES[${TCAR_MODULE_COUNT}]=${TCAR_MODULE_DIR}/Modules
    local TCAR_MODULE_DIR_MODULES=${TCAR_MODULE_DIRS_MODULES[${TCAR_MODULE_COUNT}]}
    tcar_printMessage "TCAR_MODULE_DIR_MODULES : ${TCAR_MODULE_DIR_MODULES}" --as-debugger-line

    TCAR_MODULE_DIRS_MANUALS[${TCAR_MODULE_COUNT}]=${TCAR_MODULE_DIR}/Manuals
    local TCAR_MODULE_DIR_MANUALS=${TCAR_MODULE_DIRS_MANUALS[${TCAR_MODULE_COUNT}]}
    tcar_printMessage "TCAR_MODULE_DIR_MANUALS : ${TCAR_MODULE_DIR_MANUALS}" --as-debugger-line

    TCAR_MODULE_DIRS_LOCALES[${TCAR_MODULE_COUNT}]=${TCAR_MODULE_DIR}/Locales
    local TCAR_MODULE_DIR_LOCALES=${TCAR_MODULE_DIRS_LOCALES[${TCAR_MODULE_COUNT}]}
    tcar_printMessage "TCAR_MODULE_DIR_LOCALES : ${TCAR_MODULE_DIR_LOCALES}" --as-debugger-line

    TCAR_MODULE_DIRS_CONFIGS[${TCAR_MODULE_COUNT}]=${TCAR_MODULE_DIR}/Configs
    local TCAR_MODULE_DIR_CONFIGS=${TCAR_MODULE_DIRS_CONFIGS[${TCAR_MODULE_COUNT}]}
    tcar_printMessage "TCAR_MODULE_DIR_CONFIGS : ${TCAR_MODULE_DIR_CONFIGS}" --as-debugger-line

    # Define module's initialization file.
    TCAR_MODULE_INIT_FILES[${TCAR_MODULE_COUNT}]=${TCAR_MODULE_DIR}/${TCAR_MODULE_NAME}.sh
    local TCAR_MODULE_INIT_FILE=${TCAR_MODULE_INIT_FILES[${TCAR_MODULE_COUNT}]}
    tcar_printMessage "TCAR_MODULE_INIT_FILE : ${TCAR_MODULE_INIT_FILE}" --as-debugger-line

    # Define module's connection with their localization files. It is
    # required that gettext-specific variables be defined locally, in
    # order to implement per-module localization.
    local TEXTDOMAIN=$(basename ${TCAR_MODULE_INIT_FILE})
    tcar_printMessage "TEXTDOMAIN: ${TEXTDOMAIN}" --as-debugger-line
    local TEXTDOMAINDIR=${TCAR_MODULE_DIR_LOCALES}
    tcar_printMessage "TEXTDOMAINDIR: ${TEXTDOMAINDIR}" --as-debugger-line

    # Increment module's counter just before creating next module's
    # base directory.
    TCAR_MODULE_COUNT=$(( ${TCAR_MODULE_COUNT} + 1 ))
    tcar_printMessage "TCAR_MODULE_COUNT: ${TCAR_MODULE_COUNT}" --as-debugger-line

    # Define next module's base directory.
    TCAR_MODULE_BASEDIRS[${TCAR_MODULE_COUNT}]=${TCAR_MODULE_DIR_MODULES}

    # Check function script execution rights.
    tcar_checkFiles -ex ${TCAR_MODULE_INIT_FILE}

    # Load module-specific (function) scripts into current execution
    # environment.  Keep the tcar_setModuleEnvironmentScripts function
    # call after all variables and arguments definitions.
    tcar_setModuleEnvironmentScripts

    # Execute module's initialization script.
    tcar_printMessage "=========================>: ${TCAR_MODULE_NAME} ${TCAR_MODULE_ARGUMENT}" --as-debugger-line
    ${TCAR_MODULE_NAME} ${TCAR_MODULE_ARGUMENT}

    # Unset module-specific environment.
    tcar_printMessage "<=========================: ${TCAR_MODULE_NAME}" --as-debugger-line
    tcar_unsetModuleEnvironment

    # Decrement module counter just after unset unused module
    # environments.
    TCAR_MODULE_COUNT=$(( ${TCAR_MODULE_COUNT} - 1 ))
    tcar_printMessage "TCAR_MODULE_COUNT: ${TCAR_MODULE_COUNT}" --as-debugger-line

    # Unset array and non-array variables used in this function.
    if [[ ${TCAR_MODULE_COUNT} -eq 0 ]];then
        unset TCAR_MODULE_NAMES
        unset TCAR_MODULE_BASEDIRS
        unset TCAR_MODULE_DIRS
        unset TCAR_MODULE_DIRS_MODULES
        unset TCAR_MODULE_DIRS_MANUALS
        unset TCAR_MODULE_DIRS_LOCALES
        unset TCAR_MODULE_DIRS_CONFIGS
        unset TCAR_MODULE_NAME
        unset TCAR_MODULE_DIR
        unset TCAR_MODULE_DIR_MODULES
        unset TCAR_MODULE_DIR_MANUALS
        unset TCAR_MODULE_DIR_LOCALES
        unset TCAR_MODULE_DIR_CONFIGS
    fi
}