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

878a2b
#!/bin/bash
878a2b
#
878a2b
# cli_checkFiles.sh -- This function standardizes the way file
878a2b
# conditional expressions are applied inside centos-art.sh script.
878a2b
# Here is where we answer questions like: is the file a regular file
0b8c8e
# or a directory?  Or, is it a symbolic link? Or even, does it have
878a2b
# execution rights, etc.  If the verification fails somehow at any
80c9e6
# point, an error message is output and centos-art.sh script finishes
80c9e6
# its execution. 
878a2b
#
03486a
# Copyright (C) 2009, 2010, 2011, 2012 The CentOS Project
878a2b
#
878a2b
# This program is free software; you can redistribute it and/or modify
878a2b
# it under the terms of the GNU General Public License as published by
878a2b
# the Free Software Foundation; either version 2 of the License, or (at
878a2b
# your option) any later version.
878a2b
#
878a2b
# This program is distributed in the hope that it will be useful, but
878a2b
# WITHOUT ANY WARRANTY; without even the implied warranty of
878a2b
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
878a2b
# General Public License for more details.
878a2b
#
878a2b
# You should have received a copy of the GNU General Public License
878a2b
# along with this program; if not, write to the Free Software
878a2b
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
878a2b
#
878a2b
# ----------------------------------------------------------------------
878a2b
# $Id$
878a2b
# ----------------------------------------------------------------------
878a2b
878a2b
function cli_checkFiles {
878a2b
878a2b
    # Define short options.
f73bab
    local ARGSS='i:,r,d,e,f,h,x'
878a2b
878a2b
    # Define long options.
f73bab
    local ARGSL='mime:,is-versioned'
3b0a5c
3b0a5c
    # Initialize array variables.
0b8c8e
    local -a CONDITION_COMMAND
3b0a5c
    local -a CONDITION_PATTERN
3b0a5c
    local -a CONDITION_MESSAGE
3b0a5c
3b0a5c
    # Initialize array variable counter.
3b0a5c
    local COUNTER=0
878a2b
878a2b
    # Initialize arguments with an empty value and set it as local
3b0a5c
    # variable to this function scope. Doing this is very important to
3b0a5c
    # avoid any clash with higher execution environments.
878a2b
    local ARGUMENTS=''
878a2b
3b0a5c
    # Prepare ARGUMENTS for getopt.
878a2b
    cli_parseArgumentsReDef "$@"
878a2b
3b0a5c
    # Redefine ARGUMENTS using getopt(1) command parser.
878a2b
    cli_parseArguments
878a2b
878a2b
    # Redefine positional parameters using ARGUMENTS variable.
878a2b
    eval set -- "$ARGUMENTS"
878a2b
878a2b
    # Look for options passed through positional parameters.
878a2b
    while true; do
878a2b
878a2b
        case "$1" in
878a2b
aa68b3
            -d )
0b8c8e
                CONDITION_COMMAND[((++${#CONDITION_COMMAND[*]}))]='test'
3b0a5c
                CONDITION_PATTERN[((++${#CONDITION_PATTERN[*]}))]='-d'
3b0a5c
                CONDITION_MESSAGE[((++${#CONDITION_MESSAGE[*]}))]="`gettext "isn't a directory."`"
878a2b
                shift 1
878a2b
                ;;
878a2b
aa68b3
            -e )
0b8c8e
                CONDITION_COMMAND[((++${#CONDITION_COMMAND[*]}))]='test'
aa68b3
                CONDITION_PATTERN[((++${#CONDITION_PATTERN[*]}))]='-e'
aa68b3
                CONDITION_MESSAGE[((++${#CONDITION_MESSAGE[*]}))]="`gettext "doesn't exist."`"
aa68b3
                ;;
aa68b3
aa68b3
            -f )
0b8c8e
                CONDITION_COMMAND[((++${#CONDITION_COMMAND[*]}))]='test'
3b0a5c
                CONDITION_PATTERN[((++${#CONDITION_PATTERN[*]}))]='-f'
3b0a5c
                CONDITION_MESSAGE[((++${#CONDITION_MESSAGE[*]}))]="`gettext "isn't a regular file."`"
878a2b
                shift 1
878a2b
                ;;
878a2b
aa68b3
            -h )
0b8c8e
                CONDITION_COMMAND[((++${#CONDITION_COMMAND[*]}))]='test'
3b0a5c
                CONDITION_PATTERN[((++${#CONDITION_PATTERN[*]}))]='-h'
3b0a5c
                CONDITION_MESSAGE[((++${#CONDITION_MESSAGE[*]}))]="`gettext "isn't a symbolic link."`"
878a2b
                shift 1
878a2b
                ;;
878a2b
aa68b3
            -x )
0b8c8e
                CONDITION_COMMAND[((++${#CONDITION_COMMAND[*]}))]='test'
3b0a5c
                CONDITION_PATTERN[((++${#CONDITION_PATTERN[*]}))]='-x'
3b0a5c
                CONDITION_MESSAGE[((++${#CONDITION_MESSAGE[*]}))]="`gettext "isn't an executable file."`"
878a2b
                shift 1
878a2b
                ;;
878a2b
0b8c8e
            -i | --mime )
0b8c8e
                local MIME=$2
0b8c8e
                CONDITION_COMMAND[((++${#CONDITION_COMMAND[*]}))]='file'
0b8c8e
                CONDITION_PATTERN[((++${#CONDITION_PATTERN[*]}))]='-bi'
0b8c8e
                CONDITION_MESSAGE[((++${#CONDITION_MESSAGE[*]}))]="`eval_gettext "isn't a \\\"\\\$MIME\\\" file."`"
0b8c8e
                shift 2
0b8c8e
                ;;
0b8c8e
f73bab
            -r | --is-versioned )
f73bab
                CONDITION_COMMAND[((++${#CONDITION_COMMAND[*]}))]="${CLI_NAME} svn"
f73bab
                CONDITION_PATTERN[((++${#CONDITION_PATTERN[*]}))]='--is-versioned'
f73bab
                CONDITION_MESSAGE[((++${#CONDITION_MESSAGE[*]}))]="`gettext "isn't under version control."`"
f73bab
                shift 1
f73bab
                ;;
f73bab
878a2b
            -- )
878a2b
                shift 1
878a2b
                break
878a2b
                ;;
878a2b
878a2b
        esac
878a2b
    done
878a2b
3b0a5c
    # Define list of files we want to apply verifications to, now that
3b0a5c
    # all option-like arguments have been removed from positional
f73bab
    # parameters list.
3b0a5c
    local FILE=''
3b0a5c
    local FILES=$@
3b0a5c
3b0a5c
    for FILE in $FILES;do
3b0a5c
3b0a5c
        while [[ ${COUNTER} -lt ${#CONDITION_PATTERN[*]} ]];do
3b0a5c
0b8c8e
            case ${CONDITION_COMMAND[$COUNTER]} in
0b8c8e
f73bab
                "${CLI_NAME} svn" )
f73bab
                if [[ $(${CONDITION_COMMAND[$COUNTER]} ${CONDITION_PATTERN[$COUNTER]} ${FILE}) -ne 0 ]];then \
f73bab
                    cli_printMessage "${FILE} ${CONDITION_MESSAGE[$COUNTER]}" --as-error-line
f73bab
                fi
f73bab
                ;;
f73bab
0b8c8e
                test )
0b8c8e
                ${CONDITION_COMMAND[$COUNTER]} ${CONDITION_PATTERN[$COUNTER]} ${FILE} \
0b8c8e
                    || cli_printMessage "${FILE} ${CONDITION_MESSAGE[$COUNTER]}" --as-error-line
0b8c8e
                ;;
0b8c8e
0b8c8e
                file )
0b8c8e
                if [[ ! $(${CONDITION_COMMAND[$COUNTER]} ${CONDITION_PATTERN[$COUNTER]} ${FILE}) == "$MIME" ]];then
0b8c8e
                    cli_printMessage "${FILE} ${CONDITION_MESSAGE[$COUNTER]}" --as-error-line
0b8c8e
                fi
0b8c8e
                ;;
0b8c8e
0b8c8e
                * )
0b8c8e
                cli_printMessage "`gettext "The condition command provided isn't supported."`" --as-error-line
0b8c8e
                ;;
0b8c8e
            esac
3b0a5c
3b0a5c
            COUNTER=$(($COUNTER + 1))
3b0a5c
3b0a5c
        done
3b0a5c
3b0a5c
    done
3b0a5c
878a2b
}