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

beb384
#!/bin/bash
beb384
#
b3d22e
# cli.sh -- This function initiates the centos-art.sh script
b3d22e
# command-line interface.  Variables defined in this function are
b3d22e
# accesible by all other functions. The cli function is the first
b3d22e
# script executed by centos-art.sh, onces executed in a terminal.
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
8d8a61
function cli {
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
20cf65
    # egrep-posix regular expression.  By default, when the --filter
20cf65
    # option is not provided, all paths in the working copy must match
20cf65
    # except files under hidden directories like `.svn'. We do this in
20cf65
    # conjunction with `cli_getFilesList', when building the list of
20cf65
    # files that will be processed.
20cf65
    local FLAG_FILTER='[[:alnum:]_/-]+'
beb384
beb384
    # Initialize default value to verbosity flag. The verbosity flag
e7df34
    # (-v | --verbose) controls whether centos-art.sh script prints
e7df34
    # messages or not. By default, all messages are suppressed except
e7df34
    # those directed to standard error.
3cfe81
    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
05dfab
    # Initialize default value to commit changes flag. This flag
3b9515
    # (--synchronize) controls whether version control system is
3b9515
    # triggered or not after realizing changes to source files under
3b9515
    # version control.
3b9515
    local FLAG_SYNCHRONIZE='false'
05dfab
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
48eb6a
        cli_runFnEnvironment help --read --format="texinfo" tcar-fs:::
beb384
        exit
beb384
    fi
beb384
beb384
    # Define function name (CLI_FUNCNAME) using the first argument in
beb384
    # the command-line.
05dfab
    CLI_FUNCNAME=$(cli_getRepoName $1 -f | cut -d '-' -f1)
beb384
05dfab
    # 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.
05dfab
    cli_checkFiles -x "${CLI_FUNCSCRIPT}"
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.
05dfab
    cli_checkFiles -x ${EDITOR}
beb384
beb384
    # Go for function initialization. Keep the cli_exportFunctions
beb384
    # function calling after all variables and arguments definitions.
a98679
    cli_exportFunctions "${CLI_FUNCDIRNAM}/${CLI_FUNCNAME}"
beb384
beb384
    # Execute function.
3b9515
    $CLI_FUNCNAME
beb384
beb384
}