Blame Scripts/Bash/Functions/Commons/init.sh

beb384
#!/bin/bash
beb384
#
beb384
# init.sh -- This function initiates the application's command-line
beb384
# interface.  Variables defined in this function are accesible by all
beb384
# other functions. The cli function is the first script executed by
beb384
# the application command-line onces invoked.
beb384
#
beb384
# Copyright (C) 2009, 2010, 2011, 2012 The CentOS Project
beb384
#
beb384
# This program is free software; you can redistribute it and/or modify
beb384
# it under the terms of the GNU General Public License as published by
beb384
# the Free Software Foundation; either version 2 of the License, or (at
beb384
# your option) any later version.
beb384
#
beb384
# This program is distributed in the hope that it will be useful, but
beb384
# WITHOUT ANY WARRANTY; without even the implied warranty of
beb384
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
beb384
# General Public License for more details.
beb384
#
beb384
# You should have received a copy of the GNU General Public License
beb384
# along with this program; if not, write to the Free Software
beb384
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
beb384
#
beb384
# ----------------------------------------------------------------------
beb384
# $Id$
beb384
# ----------------------------------------------------------------------
beb384
beb384
function init {
beb384
beb384
    # Initialize global variables.
beb384
    local CLI_FUNCNAME=''
beb384
    local CLI_FUNCDIRNAM=''
beb384
    local CLI_FUNCSCRIPT=''
beb384
    local ARGUMENTS=''
beb384
beb384
    # Initialize default value to filter flag. The filter flag
beb384
    # (--filter) is used mainly to reduce the number of files to
beb384
    # process. The value of this variable is interpreted as
beb384
    # egrep-posix regular expression.  By default, everything matches.
beb384
    local FLAG_FILTER='.+'
beb384
beb384
    # Initialize default value to verbosity flag. The verbosity flag
beb384
    # (--quiet) controls whether centos-art.sh script prints messages
beb384
    # or not. By default, all messages are printed out.
beb384
    local FLAG_QUIET='false'
beb384
    
beb384
    # Initialize default value to answer flag. The answer flag
beb384
    # (--answer-yes) controls whether centos-art.sh script does or
beb384
    # does not pass confirmation request points. By default, it
beb384
    # doesn't.
beb384
    local FLAG_ANSWER='false'
beb384
beb384
    # Initialize default value to don't commit changes flag. The don't
beb384
    # commit changes flag (--dont-commit-changes) controls whether
beb384
    # centos-art.sh script syncronizes changes between the central
beb384
    # repository and the working copy. By default, it does.
beb384
    local FLAG_DONT_COMMIT_CHANGES='false'
beb384
beb384
    # Initialize list of common functionalities to load.
beb384
    local FILES=$(ls ${CLI_FUNCDIR}/Commons/*.sh)
beb384
beb384
    # Initialize common functionalities.
beb384
    for FILE in ${FILES};do
beb384
        if [[ -x ${FILE} ]];then
beb384
            . ${FILE}
beb384
            export -f $(grep '^function ' ${FILE} | cut -d' ' -f2)
beb384
        else
beb384
            echo "`eval_gettext "The \\\$FILE needs to have execution rights."`"
beb384
            exit
beb384
        fi
beb384
    done
beb384
beb384
    # Trap signals in order to terminate the script execution
beb384
    # correctly (e.g., removing all temporal files before leaving).
beb384
    # Trapping the exit signal seems to be enough by now, since it is
beb384
    # always present as part of the script execution flow. Each time
beb384
    # the centos-art.sh script is executed it will inevitably end with
beb384
    # an EXIT signal at some point of its execution, even if it is
beb384
    # interrupted in the middle of its execution (e.g., through
beb384
    # `Ctrl+C').
beb384
    trap cli_terminateScriptExecution 0
beb384
beb384
    # Redefine ARGUMENTS variable using current positional parameters. 
beb384
    cli_parseArgumentsReDef "$@"
beb384
beb384
    # Check function name. The function name is critical for
beb384
    # centos-art.sh script to do something coherent. If it is not
beb384
    # provided, execute the help functionality and end script
beb384
    # execution.
beb384
    if [[ ! "$1" ]] || [[ ! "$1" =~ '^[[:alpha:]]' ]];then
beb384
        echo 'No functionality passed as first argument.'
beb384
    #    exec ${CLI_BASEDIR}/${CLI_NAME}.sh help
beb384
        exit
beb384
    fi
beb384
beb384
    # Define function name (CLI_FUNCNAME) using the first argument in
beb384
    # the command-line.
beb384
    CLI_FUNCNAME=$(cli_getRepoName $1 -f | cut -d '-' -f 1)
beb384
beb384
    # Define function directory. 
beb384
    CLI_FUNCDIRNAM=$(cli_getRepoName $CLI_FUNCNAME -d)
beb384
beb384
    # Define function file name.
beb384
    CLI_FUNCSCRIPT=${CLI_FUNCDIR}/${CLI_FUNCDIRNAM}/${CLI_FUNCNAME}.sh
beb384
beb384
    # Check function script execution rights.
beb384
    cli_checkFiles "${CLI_FUNCSCRIPT}" --execution
beb384
beb384
    # Remove the first argument passed to centos-art.sh command-line
beb384
    # in order to build optional arguments inside functionalities. We
beb384
    # start counting from second argument (inclusive) on.
beb384
    shift 1
beb384
beb384
    # Redefine ARGUMENTS using current positional parameters.
beb384
    cli_parseArgumentsReDef "$@"
beb384
beb384
    # Define default text editors used by centos-art.sh script.
beb384
    if [[ ! "$EDITOR" =~ '/usr/bin/(vim|emacs|nano)' ]];then
beb384
        EDITOR='/usr/bin/vim'
beb384
    fi
beb384
    
beb384
    # Check text editor execution rights.
beb384
    cli_checkFiles $EDITOR --execution
beb384
beb384
    # Go for function initialization. Keep the cli_exportFunctions
beb384
    # function calling after all variables and arguments definitions.
beb384
    cli_exportFunctions "${CLI_FUNCDIR}/${CLI_FUNCDIRNAM}"
beb384
beb384
    # Execute function.
beb384
    eval $CLI_FUNCNAME
beb384
beb384
}