Blame Automation/Scripts/tcar_checkFiles.sh

Alain Reguera Delgado 8f60cb
#!/bin/bash
Alain Reguera Delgado 615395
######################################################################
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado eb3010
#   tcar_checkFiles.sh -- This function standardizes the way file
Alain Reguera Delgado 615395
#   conditional expressions are applied to files.  Here is where
Alain Reguera Delgado 615395
#   centos-art.sh script answers questions like: is the file a regular
Alain Reguera Delgado 615395
#   file or a directory?  Or, is it a symbolic link? Or even, does it
Alain Reguera Delgado 615395
#   have execution rights, etc.  If the verification fails somehow at
Alain Reguera Delgado 615395
#   any point, an error message is output and centos-art.sh script
Alain Reguera Delgado 615395
#   finishes its execution. 
Alain Reguera Delgado 615395
#
Alain Reguera Delgado 0e5ba0
#   Written by:
Alain Reguera Delgado 615395
#   * Alain Reguera Delgado <al@centos.org.cu>, 2009-2013
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 0e5ba0
# Copyright (C) 2009-2013 The CentOS Artwork SIG
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 8f60cb
# This program is free software; you can redistribute it and/or modify
Alain Reguera Delgado 8f60cb
# it under the terms of the GNU General Public License as published by
Alain Reguera Delgado 8f60cb
# the Free Software Foundation; either version 2 of the License, or (at
Alain Reguera Delgado 8f60cb
# your option) any later version.
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 8f60cb
# This program is distributed in the hope that it will be useful, but
Alain Reguera Delgado 8f60cb
# WITHOUT ANY WARRANTY; without even the implied warranty of
Alain Reguera Delgado 8f60cb
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Alain Reguera Delgado 8f60cb
# General Public License for more details.
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 8f60cb
# You should have received a copy of the GNU General Public License
Alain Reguera Delgado 8f60cb
# along with this program; if not, write to the Free Software
Alain Reguera Delgado 8f60cb
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 615395
######################################################################
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado eb3010
function tcar_checkFiles {
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 0fdaad
    # Define gettext-specific variables locally, to provide
Alain Reguera Delgado 0fdaad
    # per-function localization.
Alain Reguera Delgado 0fdaad
    local TEXTDOMAIN="${FUNCNAME}.sh"
Alain Reguera Delgado 0fdaad
    local TEXTDOMAINDIR="${TCAR_SCRIPT_BASEDIR}/Scripts/Locales"
Alain Reguera Delgado 0fdaad
Alain Reguera Delgado 615395
    # Initialize local array variables.
Alain Reguera Delgado 8f60cb
    local -a CONDITION_COMMAND
Alain Reguera Delgado 8f60cb
    local -a CONDITION_PATTERN
Alain Reguera Delgado 8f60cb
    local -a CONDITION_MESSAGE
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 615395
    # Initialize local counter.
Alain Reguera Delgado 8f60cb
    local COUNTER=0
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 786ac0
    OPTIND=1
Alain Reguera Delgado 06ab0f
    while getopts "i:,r,m:,n,d,e,f,h,x" OPTION "${@}"; do
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 786ac0
        case "${OPTION}" in
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 786ac0
            d )
Alain Reguera Delgado 65f472
                CONDITION_COMMAND[((++${#CONDITION_COMMAND[*]}))]='/usr/bin/test'
Alain Reguera Delgado 8f60cb
                CONDITION_PATTERN[((++${#CONDITION_PATTERN[*]}))]='-d'
Alain Reguera Delgado 8f60cb
                CONDITION_MESSAGE[((++${#CONDITION_MESSAGE[*]}))]="`gettext "isn't a directory."`"
Alain Reguera Delgado 8f60cb
                ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 786ac0
            e )
Alain Reguera Delgado 65f472
                CONDITION_COMMAND[((++${#CONDITION_COMMAND[*]}))]='/usr/bin/test'
Alain Reguera Delgado 8f60cb
                CONDITION_PATTERN[((++${#CONDITION_PATTERN[*]}))]='-e'
Alain Reguera Delgado 8f60cb
                CONDITION_MESSAGE[((++${#CONDITION_MESSAGE[*]}))]="`gettext "doesn't exist."`"
Alain Reguera Delgado 8f60cb
                ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 786ac0
            f )
Alain Reguera Delgado 65f472
                CONDITION_COMMAND[((++${#CONDITION_COMMAND[*]}))]='/usr/bin/test'
Alain Reguera Delgado 8f60cb
                CONDITION_PATTERN[((++${#CONDITION_PATTERN[*]}))]='-f'
Alain Reguera Delgado 8f60cb
                CONDITION_MESSAGE[((++${#CONDITION_MESSAGE[*]}))]="`gettext "isn't a regular file."`"
Alain Reguera Delgado 8f60cb
                ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 786ac0
            h )
Alain Reguera Delgado 65f472
                CONDITION_COMMAND[((++${#CONDITION_COMMAND[*]}))]='/usr/bin/test'
Alain Reguera Delgado 8f60cb
                CONDITION_PATTERN[((++${#CONDITION_PATTERN[*]}))]='-h'
Alain Reguera Delgado 8f60cb
                CONDITION_MESSAGE[((++${#CONDITION_MESSAGE[*]}))]="`gettext "isn't a symbolic link."`"
Alain Reguera Delgado 8f60cb
                ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 786ac0
            x )
Alain Reguera Delgado 65f472
                CONDITION_COMMAND[((++${#CONDITION_COMMAND[*]}))]='/usr/bin/test'
Alain Reguera Delgado 8f60cb
                CONDITION_PATTERN[((++${#CONDITION_PATTERN[*]}))]='-x'
Alain Reguera Delgado 8f60cb
                CONDITION_MESSAGE[((++${#CONDITION_MESSAGE[*]}))]="`gettext "isn't an executable file."`"
Alain Reguera Delgado 8f60cb
                ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 786ac0
            i )
Alain Reguera Delgado 786ac0
                local MIME=${OPTARG}
Alain Reguera Delgado 65f472
                CONDITION_COMMAND[((++${#CONDITION_COMMAND[*]}))]='/usr/bin/file'
Alain Reguera Delgado 8f60cb
                CONDITION_PATTERN[((++${#CONDITION_PATTERN[*]}))]='-bi'
Alain Reguera Delgado 0e5ba0
                CONDITION_MESSAGE[((++${#CONDITION_MESSAGE[*]}))]="`eval_gettext "isn't a \\\"\\\$MIME\\\" file."`"
Alain Reguera Delgado 8f60cb
                ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 786ac0
            m )
Alain Reguera Delgado 8f60cb
                CONDITION_COMMAND[((++${#CONDITION_COMMAND[*]}))]='match'
Alain Reguera Delgado 786ac0
                CONDITION_PATTERN[((++${#CONDITION_PATTERN[*]}))]="${OPTARG}"
Alain Reguera Delgado 06ab0f
                CONDITION_MESSAGE[((++${#CONDITION_MESSAGE[*]}))]="`gettext "doesn't match its pattern."`"
Alain Reguera Delgado 8f60cb
                ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 786ac0
            n )
Alain Reguera Delgado 65f472
                CONDITION_COMMAND[((++${#CONDITION_COMMAND[*]}))]="/bin/rpm"
Alain Reguera Delgado 8f60cb
                CONDITION_PATTERN[((++${#CONDITION_PATTERN[*]}))]="-q --quiet"
Alain Reguera Delgado 8f60cb
                CONDITION_MESSAGE[((++${#CONDITION_MESSAGE[*]}))]="`gettext "isn't installed in the system."`"
Alain Reguera Delgado 8f60cb
                ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        esac
Alain Reguera Delgado 8f60cb
    done
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 786ac0
    # Clean up positional parameters to reflect the fact that options
Alain Reguera Delgado 786ac0
    # have been processed already.
Alain Reguera Delgado 786ac0
    shift $(( ${OPTIND} - 1 ))
Alain Reguera Delgado 786ac0
Alain Reguera Delgado 8f60cb
    # Define list of files we want to apply verifications to, now that
Alain Reguera Delgado 8f60cb
    # all option-like arguments have been removed from positional
Alain Reguera Delgado 8f60cb
    # parameters list so we are free to go with the verifications.
Alain Reguera Delgado 8f60cb
    local FILE=''
Alain Reguera Delgado 615395
    local FILES=${@}
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 788260
    # Verify existence of files to prevent centos-art.sh script from
Alain Reguera Delgado 788260
    # using the current location in cases when it shouldn't (e.g.,
Alain Reguera Delgado 788260
    # here it is expecting a list of files to process.).
Alain Reguera Delgado 788260
    if [[ -z ${FILES} ]];then
Alain Reguera Delgado 788260
        tcar_printMessage "`gettext "No file for processing found."`" --as-error-line
Alain Reguera Delgado 788260
    fi
Alain Reguera Delgado 788260
Alain Reguera Delgado 615395
    for FILE in ${FILES};do
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        until [[ ${COUNTER} -eq ${#CONDITION_PATTERN[*]} ]];do
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 615395
            case ${CONDITION_COMMAND[${COUNTER}]} in
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 65f472
                "/usr/bin/test" | "/bin/rpm" )
Alain Reguera Delgado 615395
                ${CONDITION_COMMAND[${COUNTER}]} ${CONDITION_PATTERN[${COUNTER}]} ${FILE} \
Alain Reguera Delgado eb3010
                    || tcar_printMessage "${FILE} ${CONDITION_MESSAGE[${COUNTER}]}" --as-error-line
Alain Reguera Delgado 8f60cb
                ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 65f472
                "/usr/bin/file" )
Alain Reguera Delgado 786ac0
                if [[ ! $(${CONDITION_COMMAND[${COUNTER}]} ${CONDITION_PATTERN[${COUNTER}]} ${FILE}) =~ "^${MIME}" ]];then
Alain Reguera Delgado eb3010
                    tcar_printMessage "${FILE} ${CONDITION_MESSAGE[${COUNTER}]}" --as-error-line
Alain Reguera Delgado 8f60cb
                fi
Alain Reguera Delgado 8f60cb
                ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
                "match" )
Alain Reguera Delgado 615395
                if [[ ! ${FILE} =~ "${CONDITION_PATTERN[${COUNTER}]}" ]];then
Alain Reguera Delgado eb3010
                    tcar_printMessage "${FILE} ${CONDITION_MESSAGE[${COUNTER}]}" --as-error-line
Alain Reguera Delgado 8f60cb
                fi
Alain Reguera Delgado 8f60cb
                ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
                * )
Alain Reguera Delgado eb3010
                tcar_printMessage "`gettext "The condition command provided isn't supported."`" --as-error-line
Alain Reguera Delgado 8f60cb
                ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
            esac
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 615395
            COUNTER=$((${COUNTER} + 1))
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
        done
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    done
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
}