Blame Automation/centos-art.sh-mods/Cli/cli_checkFiles.sh

Alain Reguera Delgado 8f60cb
#!/bin/bash
Alain Reguera Delgado 8f60cb
#
Alain Reguera Delgado 8f60cb
# cli_checkFiles.sh -- This function standardizes the way file
Alain Reguera Delgado 8f60cb
# conditional expressions are applied inside centos-art.sh script.
Alain Reguera Delgado 8f60cb
# Here is where we answer questions like: is the file a regular file
Alain Reguera Delgado 8f60cb
# or a directory?  Or, is it a symbolic link? Or even, does it have
Alain Reguera Delgado 8f60cb
# execution rights, etc.  If the verification fails somehow at any
Alain Reguera Delgado 8f60cb
# point, an error message is output and centos-art.sh script finishes
Alain Reguera Delgado 8f60cb
# its execution. 
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 8f60cb
# ----------------------------------------------------------------------
Alain Reguera Delgado 8f60cb
# $Id$
Alain Reguera Delgado 8f60cb
# ----------------------------------------------------------------------
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 8f60cb
    # Initialize 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 8f60cb
    # Initialize array variable 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 8f60cb
    # avoid any clash with higher execution environments.
Alain Reguera Delgado 8f60cb
    local ARGUMENTS=''
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Prepare ARGUMENTS for getopt.
Alain Reguera Delgado 8f60cb
    cli_parseArgumentsReDef "$@"
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Redefine ARGUMENTS using getopt(1) command parser.
Alain Reguera Delgado 8f60cb
    cli_parseArguments
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    # Redefine positional parameters using ARGUMENTS variable.
Alain Reguera Delgado 8f60cb
    eval set -- "$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 8f60cb
        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 8f60cb
                local MIME=$2
Alain Reguera Delgado 8f60cb
                CONDITION_COMMAND[((++${#CONDITION_COMMAND[*]}))]='file'
Alain Reguera Delgado 8f60cb
                CONDITION_PATTERN[((++${#CONDITION_PATTERN[*]}))]='-bi'
Alain Reguera Delgado 8f60cb
                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 8f60cb
                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 8f60cb
    local FILES=$@
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
    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 8f60cb
            case ${CONDITION_COMMAND[$COUNTER]} in
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
                "test" | "rpm" )
Alain Reguera Delgado 8f60cb
                ${CONDITION_COMMAND[$COUNTER]} ${CONDITION_PATTERN[$COUNTER]} ${FILE} \
Alain Reguera Delgado 8f60cb
                    || 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 8f60cb
                cli_runFnEnvironment ${CONDITION_PATTERN[$COUNTER]} ${FILE}
Alain Reguera Delgado 8f60cb
                ;;
Alain Reguera Delgado 8f60cb
Alain Reguera Delgado 8f60cb
                "file" )
Alain Reguera Delgado 8f60cb
                if [[ ! $(${CONDITION_COMMAND[$COUNTER]} ${CONDITION_PATTERN[$COUNTER]} ${FILE}) == "$MIME" ]];then
Alain Reguera Delgado 8f60cb
                    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 8f60cb
                if [[ ! ${FILE} =~ "${CONDITION_PATTERN[$COUNTER]}" ]];then
Alain Reguera Delgado 8f60cb
                    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 8f60cb
            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
}