Blame Automation/Modules/Cli/Scripts/cli_checkFiles.sh

Alain Reguera Delgado 8f60cb
#!/bin/bash
Alain Reguera Delgado 615395
######################################################################
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 615395
#   cli_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 615395
#   Written by: 
Alain Reguera Delgado 615395
#   * Alain Reguera Delgado <al@centos.org.cu>, 2009-2013
Alain Reguera Delgado 615395
#     Key fingerprint = D67D 0F82 4CBD 90BC 6421  DF28 7CCE 757C 17CA 3951
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 8f60cb
# Copyright (C) 2009-2013 The CentOS Project
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 8f60cb
function cli_checkFiles {
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Define short options.
Alain Reguera Delgado 8f60cb
    local ARGSS='i:,r,m:,n,d,e,f,h,x'
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Define long options.
Alain Reguera Delgado 8f60cb
    local ARGSL='mime:,is-versioned,match:,is-installed'
Alain Reguera Delgado 8f60cb
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 8f60cb
    # Initialize arguments with an empty value and set it as local
Alain Reguera Delgado 8f60cb
    # variable to this function scope. Doing this is very important to
Alain Reguera Delgado 615395
    # avoid any clash with higher execution environments. This
Alain Reguera Delgado 615395
    # variable is shared for different function environments.
Alain Reguera Delgado ddf7d2
    local TCAR_ARGUMENTS=''
Alain Reguera Delgado 615395
    
Alain Reguera Delgado 615395
    # Redefine arguments using current positional parameters. 
Alain Reguera Delgado 615395
    cli_setArguments "${@}"
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 615395
    # Redefine positional parameters using arguments variable.
Alain Reguera Delgado ddf7d2
    eval set -- "${TCAR_ARGUMENTS}"
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Look for options passed through positional parameters.
Alain Reguera Delgado 8f60cb
    while true; do
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 615395
        case "${1}" in
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
            -d )
Alain Reguera Delgado 8f60cb
                CONDITION_COMMAND[((++${#CONDITION_COMMAND[*]}))]='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
                shift 1
Alain Reguera Delgado 8f60cb
                ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
            -e )
Alain Reguera Delgado 8f60cb
                CONDITION_COMMAND[((++${#CONDITION_COMMAND[*]}))]='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
                shift 1
Alain Reguera Delgado 8f60cb
                ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
            -f )
Alain Reguera Delgado 8f60cb
                CONDITION_COMMAND[((++${#CONDITION_COMMAND[*]}))]='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
                shift 1
Alain Reguera Delgado 8f60cb
                ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
            -h )
Alain Reguera Delgado 8f60cb
                CONDITION_COMMAND[((++${#CONDITION_COMMAND[*]}))]='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
                shift 1
Alain Reguera Delgado 8f60cb
                ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
            -x )
Alain Reguera Delgado 8f60cb
                CONDITION_COMMAND[((++${#CONDITION_COMMAND[*]}))]='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
                shift 1
Alain Reguera Delgado 8f60cb
                ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
            -i | --mime )
Alain Reguera Delgado 615395
                local MIME=${2}
Alain Reguera Delgado 8f60cb
                CONDITION_COMMAND[((++${#CONDITION_COMMAND[*]}))]='file'
Alain Reguera Delgado 8f60cb
                CONDITION_PATTERN[((++${#CONDITION_PATTERN[*]}))]='-bi'
Alain Reguera Delgado 615395
                CONDITION_MESSAGE[((++${#CONDITION_MESSAGE[*]}))]="`eval_gettext "isn't a \\\"\\\${MIME}\\\" file."`"
Alain Reguera Delgado 8f60cb
                shift 2
Alain Reguera Delgado 8f60cb
                ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
            -m | --match )
Alain Reguera Delgado 8f60cb
                CONDITION_COMMAND[((++${#CONDITION_COMMAND[*]}))]='match'
Alain Reguera Delgado 615395
                CONDITION_PATTERN[((++${#CONDITION_PATTERN[*]}))]="${2}"
Alain Reguera Delgado 8f60cb
                CONDITION_MESSAGE[((++${#CONDITION_MESSAGE[*]}))]="`eval_gettext "doesn't match its pattern."`"
Alain Reguera Delgado 8f60cb
                shift 2
Alain Reguera Delgado 8f60cb
               ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
            -r | --is-versioned )
Alain Reguera Delgado 8f60cb
                CONDITION_COMMAND[((++${#CONDITION_COMMAND[*]}))]="centos-art"
Alain Reguera Delgado 8f60cb
                CONDITION_PATTERN[((++${#CONDITION_PATTERN[*]}))]="vcs --is-versioned"
Alain Reguera Delgado 8f60cb
                CONDITION_MESSAGE[((++${#CONDITION_MESSAGE[*]}))]=""
Alain Reguera Delgado 8f60cb
                shift 1
Alain Reguera Delgado 8f60cb
                ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
            -n | --is-installed )
Alain Reguera Delgado 8f60cb
                CONDITION_COMMAND[((++${#CONDITION_COMMAND[*]}))]="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
                shift 1
Alain Reguera Delgado 8f60cb
                ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
            -- )
Alain Reguera Delgado 8f60cb
                shift 1
Alain Reguera Delgado 8f60cb
                break
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 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 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 8f60cb
                "test" | "rpm" )
Alain Reguera Delgado 615395
                ${CONDITION_COMMAND[${COUNTER}]} ${CONDITION_PATTERN[${COUNTER}]} ${FILE} \
Alain Reguera Delgado 615395
                    || cli_printMessage "${FILE} ${CONDITION_MESSAGE[${COUNTER}]}" --as-error-line
Alain Reguera Delgado 8f60cb
                ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
                "centos-art" )
Alain Reguera Delgado 8f60cb
                # Don't create another level for error messages here
Alain Reguera Delgado 8f60cb
                # (that would duplicate them unnecessarily).  Instead,
Alain Reguera Delgado 8f60cb
                # set error messages inside specific functionalities
Alain Reguera Delgado 8f60cb
                # and use them directly from there.
Alain Reguera Delgado 615395
                cli_runFnEnvironment ${CONDITION_PATTERN[${COUNTER}]} ${FILE}
Alain Reguera Delgado 8f60cb
                ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
                "file" )
Alain Reguera Delgado 615395
                if [[ ! $(${CONDITION_COMMAND[${COUNTER}]} ${CONDITION_PATTERN[${COUNTER}]} ${FILE}) == "${MIME}" ]];then
Alain Reguera Delgado 615395
                    cli_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 615395
                    cli_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 8f60cb
                cli_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
}