Blame Scripts/tcar.sh

Alain Reguera Delgado a11735
#!/bin/bash
Alain Reguera Delgado a11735
######################################################################
Alain Reguera Delgado a11735
#
Alain Reguera Delgado a11735
#   tcar.sh -- The CentOS Artwork Repository automation tool.
Alain Reguera Delgado a11735
#
Alain Reguera Delgado a11735
#   Written by:
Alain Reguera Delgado a11735
#   * Alain Reguera Delgado <al@centos.org.cu>, 2009-2013
Alain Reguera Delgado a11735
#
Alain Reguera Delgado a11735
# Copyright (C) 2009-2013 The CentOS Artwork SIG
Alain Reguera Delgado a11735
#
Alain Reguera Delgado a11735
# This program is free software; you can redistribute it and/or modify
Alain Reguera Delgado a11735
# it under the terms of the GNU General Public License as published by
Alain Reguera Delgado a11735
# the Free Software Foundation; either version 2 of the License, or (at
Alain Reguera Delgado a11735
# your option) any later version.
Alain Reguera Delgado a11735
#
Alain Reguera Delgado a11735
# This program is distributed in the hope that it will be useful, but
Alain Reguera Delgado a11735
# WITHOUT ANY WARRANTY; without even the implied warranty of
Alain Reguera Delgado a11735
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Alain Reguera Delgado a11735
# General Public License for more details.
Alain Reguera Delgado a11735
#
Alain Reguera Delgado a11735
# You should have received a copy of the GNU General Public License
Alain Reguera Delgado a11735
# along with this program; if not, write to the Free Software
Alain Reguera Delgado a11735
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Alain Reguera Delgado a11735
#
Alain Reguera Delgado a11735
######################################################################
Alain Reguera Delgado a11735
Alain Reguera Delgado a11735
######################################################################
Alain Reguera Delgado a11735
# Identity
Alain Reguera Delgado a11735
######################################################################
Alain Reguera Delgado a11735
Alain Reguera Delgado a11735
declare -xr TCAR_SCRIPT_NAME="tcar.sh"
Alain Reguera Delgado a11735
declare -xr TCAR_SCRIPT_VERSION='0.8'
Alain Reguera Delgado a11735
declare -xr TCAR_SCRIPT_COMMAND="tcar"
Alain Reguera Delgado a11735
Alain Reguera Delgado a11735
######################################################################
Alain Reguera Delgado a11735
# Paths
Alain Reguera Delgado a11735
######################################################################
Alain Reguera Delgado a11735
Alain Reguera Delgado a11735
# Base directory where repository files are installed in.
Alain Reguera Delgado a11735
declare -xr TCAR_BASEDIR=/usr/share/tcar
Alain Reguera Delgado a11735
Alain Reguera Delgado a11735
# Base directory where final content is produced. This value should be
Alain Reguera Delgado a11735
# customized later by the user.
Alain Reguera Delgado a11735
declare -xr TCAR_WORKDIR=/tmp
Alain Reguera Delgado a11735
Alain Reguera Delgado a11735
# Base directory where automation script modules are installed in.
Alain Reguera Delgado c50bf6
declare -xr TCAR_SCRIPT_MODULES_BASEDIR=${TCAR_BASEDIR}/modules
Alain Reguera Delgado a11735
Alain Reguera Delgado a11735
# Directory to store temporal files.
Alain Reguera Delgado a11735
declare -xr TCAR_SCRIPT_TEMPDIR=$(mktemp -p /tmp -d ${TCAR_SCRIPT_NAME}-XXXXXX)
Alain Reguera Delgado a11735
Alain Reguera Delgado a11735
# Configuration files in order of reading preference. The last file in
Alain Reguera Delgado a11735
# the list overlaps options set in previous files in the list. Use
Alain Reguera Delgado a11735
# colon character to separate files in the list.
Alain Reguera Delgado a11735
declare -xr TCAR_SCRIPT_CONFIG=${HOME}/.tcar.conf
Alain Reguera Delgado a11735
Alain Reguera Delgado a11735
# Base directory where man pages are searched at.
Alain Reguera Delgado a11735
declare -x TCAR_SCRIPT_MANUALS=/usr/share/man
Alain Reguera Delgado a11735
Alain Reguera Delgado a11735
# Default text editor.
Alain Reguera Delgado a11735
declare -x  TCAR_SCRIPT_EDITOR=/usr/bin/vim
Alain Reguera Delgado a11735
Alain Reguera Delgado a11735
######################################################################
Alain Reguera Delgado a11735
# Internationalization
Alain Reguera Delgado a11735
######################################################################
Alain Reguera Delgado a11735
Alain Reguera Delgado a11735
# Set the script language information using the LC format. This format
Alain Reguera Delgado a11735
# shows both language and country information (e.g., `es_ES').
Alain Reguera Delgado a11735
declare -xr TCAR_SCRIPT_LANG_LC=$(echo ${LANG} | cut -d'.' -f1)
Alain Reguera Delgado a11735
Alain Reguera Delgado a11735
# Set the script language information using the LL format. This format
Alain Reguera Delgado a11735
# shows only the language information (e.g., `es').
Alain Reguera Delgado a11735
declare -xr TCAR_SCRIPT_LANG_LL=$(echo ${TCAR_SCRIPT_LANG_LC} | cut -d'_' -f1)
Alain Reguera Delgado a11735
Alain Reguera Delgado a11735
# Set the script language information using the CC format. This format
Alain Reguera Delgado a11735
# shows only the country information (e.g., `ES').
Alain Reguera Delgado a11735
declare -xr TCAR_SCRIPT_LANG_CC=$(echo ${TCAR_SCRIPT_LANG_LC} | cut -d'_' -f2)
Alain Reguera Delgado a11735
Alain Reguera Delgado a11735
# Set function environments required by GNU gettext system.
Alain Reguera Delgado a11735
. gettext.sh
Alain Reguera Delgado a11735
Alain Reguera Delgado a11735
# Set the script text domain. This information is used by gettext
Alain Reguera Delgado a11735
# system to retrieve translated strings from machine object (MO) files
Alain Reguera Delgado a11735
# with this name. This variable is reset each time a new module is
Alain Reguera Delgado a11735
# loaded, so the correct files can be used.
Alain Reguera Delgado a11735
declare -x TEXTDOMAIN="${TCAR_SCRIPT_NAME}"
Alain Reguera Delgado a11735
Alain Reguera Delgado a11735
# Set the script text domain directory. This information is used by
Alain Reguera Delgado a11735
# gettext system to know where the machine objects are stored in. This
Alain Reguera Delgado a11735
# variable is reset each time a new module is loaded, so the correct
Alain Reguera Delgado a11735
# files can be used.
Alain Reguera Delgado a11735
declare -x TEXTDOMAINDIR=/usr/share/locale
Alain Reguera Delgado a11735
Alain Reguera Delgado a11735
######################################################################
Alain Reguera Delgado a11735
# Global Flags
Alain Reguera Delgado a11735
######################################################################
Alain Reguera Delgado a11735
Alain Reguera Delgado a11735
# Set filter flag (-f|--filter).  This flag is mainly used to reduce
Alain Reguera Delgado a11735
# the number of files to process and is interpreted as egrep-posix
Alain Reguera Delgado a11735
# regular expression.  By default, when this flag is not provided, all
Alain Reguera Delgado a11735
# paths in the working copy will match, except files inside hidden
Alain Reguera Delgado a11735
# directories like `.svn' and `.git' that will be omitted.
Alain Reguera Delgado a11735
declare -x  TCAR_FLAG_FILTER='[[:alnum:]_/-]+'
Alain Reguera Delgado a11735
Alain Reguera Delgado a11735
# Set verbosity flag (-q|--quiet). This flag controls whether
Alain Reguera Delgado a11735
# tcar.sh script prints messages or not. By default, all
Alain Reguera Delgado a11735
# messages are suppressed except those directed to standard error.
Alain Reguera Delgado a11735
declare -x  TCAR_FLAG_QUIET='false'
Alain Reguera Delgado a11735
Alain Reguera Delgado a11735
# Set affirmative flag (-y|--yes). This flag controls whether
Alain Reguera Delgado a11735
# tcar.sh script does or does not pass confirmation request
Alain Reguera Delgado a11735
# points. By default, it doesn't.
Alain Reguera Delgado a11735
declare -x  TCAR_FLAG_YES='false'
Alain Reguera Delgado a11735
Alain Reguera Delgado a11735
# Set debugger flag (-d|--debug). This flag controls whether
Alain Reguera Delgado a11735
# tcar.sh script does or does not print debugging information.
Alain Reguera Delgado a11735
# The tcar.sh script prints debug information to standard
Alain Reguera Delgado a11735
# output.
Alain Reguera Delgado a11735
declare -x  TCAR_FLAG_DEBUG='false'
Alain Reguera Delgado a11735
Alain Reguera Delgado a11735
######################################################################
Alain Reguera Delgado a11735
# Global Functions
Alain Reguera Delgado a11735
######################################################################
Alain Reguera Delgado a11735
Alain Reguera Delgado a11735
# Export script's environment functions.
Alain Reguera Delgado c50bf6
for SCRIPT_FILE in $(ls ${TCAR_BASEDIR}/tcar_*.sh);do
Alain Reguera Delgado a11735
    if [[ -x ${SCRIPT_FILE} ]];then
Alain Reguera Delgado a11735
        . ${SCRIPT_FILE}
Alain Reguera Delgado a11735
        export -f $(grep '^function ' ${SCRIPT_FILE} | cut -d' ' -f2)
Alain Reguera Delgado a11735
    else
Alain Reguera Delgado a11735
        echo "${SCRIPT_FILE} `gettext "has not execution rights."`"
Alain Reguera Delgado a11735
        exit 1
Alain Reguera Delgado a11735
    fi
Alain Reguera Delgado a11735
done
Alain Reguera Delgado a11735
Alain Reguera Delgado a11735
######################################################################
Alain Reguera Delgado a11735
# Signals
Alain Reguera Delgado a11735
######################################################################
Alain Reguera Delgado a11735
Alain Reguera Delgado a11735
# Trap signals in order to terminate the script execution correctly
Alain Reguera Delgado a11735
# (e.g., removing all temporal files before leaving).  Trapping the
Alain Reguera Delgado a11735
# exit signal seems to be enough by now, since it is always present as
Alain Reguera Delgado a11735
# part of the script execution flow. Each time the tcar.sh
Alain Reguera Delgado a11735
# script is executed it will inevitably end with an EXIT signal at
Alain Reguera Delgado a11735
# some point of its execution, even if it is interrupted in the middle
Alain Reguera Delgado a11735
# of its execution (e.g., through `Ctrl+C').
Alain Reguera Delgado a11735
trap tcar_terminateScriptExecution 0
Alain Reguera Delgado a11735
Alain Reguera Delgado a11735
######################################################################
Alain Reguera Delgado a11735
# Parse Command-line Arguments
Alain Reguera Delgado a11735
######################################################################
Alain Reguera Delgado a11735
Alain Reguera Delgado a11735
declare -x TCAR_MODULE_NAME=''
Alain Reguera Delgado a11735
declare -x TCAR_MODULE_ARGUMENT=''
Alain Reguera Delgado a11735
declare -x TCAR_SCRIPT_ARGUMENT=''
Alain Reguera Delgado a11735
Alain Reguera Delgado a11735
# Retrieve module's name using the first argument of tcar.sh
Alain Reguera Delgado a11735
# script as reference.
Alain Reguera Delgado a11735
if [[ ! ${1} =~ '^-' ]];then
Alain Reguera Delgado a11735
    TCAR_MODULE_NAME="${1}"; shift 1
Alain Reguera Delgado a11735
else
Alain Reguera Delgado a11735
    TCAR_MODULE_NAME=""
Alain Reguera Delgado a11735
fi
Alain Reguera Delgado a11735
Alain Reguera Delgado a11735
# Initialize tcar.sh script specific options. The way tcar.sh script
Alain Reguera Delgado a11735
# retrieves its options isn't as sophisticated (e.g., it doesn't
Alain Reguera Delgado a11735
# provide valid-option verifications) as it is provided by getopt
Alain Reguera Delgado a11735
# command. I cannot use getopt here because it is already used when
Alain Reguera Delgado a11735
# loading module-specific options. Using more than one invocation of
Alain Reguera Delgado a11735
# getopt in the same script is not possible (e.g., one of the
Alain Reguera Delgado a11735
# invocations may enter in conflict with the other one when different
Alain Reguera Delgado a11735
# option definitions are expected in the command-line.)
Alain Reguera Delgado a11735
while true; do
Alain Reguera Delgado a11735
Alain Reguera Delgado a11735
    # Store non-option arguments passed to tcar.sh script.
Alain Reguera Delgado a11735
    if [[ ! ${1} =~ '^-' ]];then
Alain Reguera Delgado a11735
        TCAR_SCRIPT_ARGUMENT="${1} ${TCAR_SCRIPT_ARGUMENT}"
Alain Reguera Delgado a11735
        shift 1
Alain Reguera Delgado a11735
        if [[ $# -gt 0 ]];then
Alain Reguera Delgado a11735
            continue
Alain Reguera Delgado a11735
        else
Alain Reguera Delgado a11735
            break
Alain Reguera Delgado a11735
        fi
Alain Reguera Delgado a11735
    fi
Alain Reguera Delgado a11735
Alain Reguera Delgado a11735
    case "${1}" in
Alain Reguera Delgado a11735
Alain Reguera Delgado a11735
        --help* )
Alain Reguera Delgado a11735
Alain Reguera Delgado a11735
            if [[ -z ${TCAR_MODULE_NAME} ]];then
Alain Reguera Delgado a11735
                # Print tcar.sh script's help. Consider that the
Alain Reguera Delgado a11735
                # --help option can receive an argument by using the
Alain Reguera Delgado a11735
                # equal sign (e.g.,
Alain Reguera Delgado a11735
                # --help=tcar_setModuleEnvironment.sh).  However, it
Alain Reguera Delgado a11735
                # is not possible to use spaces instead of equal sign
Alain Reguera Delgado a11735
                # because that would confuse other options from being
Alain Reguera Delgado a11735
                # parsed.
Alain Reguera Delgado a11735
                tcar_printHelp "${1}"
Alain Reguera Delgado a11735
                exit 0
Alain Reguera Delgado a11735
            else
Alain Reguera Delgado a11735
                # Store the argument for further processing inside the
Alain Reguera Delgado a11735
                # module environment that will be executed later.
Alain Reguera Delgado a11735
                TCAR_MODULE_ARGUMENT="-g ${1} ${TCAR_MODULE_ARGUMENT}"
Alain Reguera Delgado a11735
                shift 1
Alain Reguera Delgado a11735
            fi
Alain Reguera Delgado a11735
            ;;
Alain Reguera Delgado a11735
Alain Reguera Delgado a11735
        --version )
Alain Reguera Delgado a11735
Alain Reguera Delgado a11735
            # Print tcar.sh script's version.
Alain Reguera Delgado a11735
            if [[ -z ${TCAR_MODULE_NAME} ]];then
Alain Reguera Delgado a11735
                tcar_printVersion
Alain Reguera Delgado a11735
                exit 0
Alain Reguera Delgado a11735
            else
Alain Reguera Delgado a11735
                TCAR_MODULE_ARGUMENT="-g ${1} ${TCAR_MODULE_ARGUMENT}"
Alain Reguera Delgado a11735
                shift 1
Alain Reguera Delgado a11735
            fi
Alain Reguera Delgado a11735
            ;;
Alain Reguera Delgado a11735
Alain Reguera Delgado a11735
        --quiet )
Alain Reguera Delgado a11735
Alain Reguera Delgado a11735
            TCAR_FLAG_QUIET='true'
Alain Reguera Delgado a11735
            shift 1
Alain Reguera Delgado a11735
            ;;
Alain Reguera Delgado a11735
Alain Reguera Delgado a11735
        --yes )
Alain Reguera Delgado a11735
Alain Reguera Delgado a11735
            TCAR_FLAG_YES='true'
Alain Reguera Delgado a11735
            shift 1
Alain Reguera Delgado a11735
            ;;
Alain Reguera Delgado a11735
Alain Reguera Delgado a11735
        --debug )
Alain Reguera Delgado a11735
Alain Reguera Delgado a11735
            TCAR_FLAG_DEBUG='true'
Alain Reguera Delgado a11735
            shift 1
Alain Reguera Delgado a11735
            ;;
Alain Reguera Delgado a11735
Alain Reguera Delgado a11735
        * ) 
Alain Reguera Delgado a11735
Alain Reguera Delgado a11735
            # Store module-specific option arguments. This is, all
Alain Reguera Delgado a11735
            # arguments not considered part of tcar.sh script
Alain Reguera Delgado a11735
            # itself. The module-specific option arguments are passed,
Alain Reguera Delgado a11735
            # later, to getopt for option processing, inside the
Alain Reguera Delgado a11735
            # module-specific environments.
Alain Reguera Delgado a11735
            TCAR_MODULE_ARGUMENT="-g ${1} ${TCAR_MODULE_ARGUMENT}"
Alain Reguera Delgado a11735
            shift 1
Alain Reguera Delgado a11735
            if [[ $# -gt 0 ]];then
Alain Reguera Delgado a11735
                continue
Alain Reguera Delgado a11735
            else
Alain Reguera Delgado a11735
                break
Alain Reguera Delgado a11735
            fi
Alain Reguera Delgado a11735
            ;;
Alain Reguera Delgado a11735
    esac
Alain Reguera Delgado a11735
done
Alain Reguera Delgado a11735
Alain Reguera Delgado a11735
# Initiate module-specific environment.
Alain Reguera Delgado a11735
tcar_setModuleEnvironment -m "${TCAR_MODULE_NAME}" ${TCAR_MODULE_ARGUMENT} ${TCAR_SCRIPT_ARGUMENT}
Alain Reguera Delgado a11735
Alain Reguera Delgado a11735
# At this point everything has been done without errors. So, exit
Alain Reguera Delgado a11735
# tcar.sh script successfully.
Alain Reguera Delgado a11735
exit 0