Blame Scripts/tcar_getFilesList.sh

Alain Reguera Delgado e54180
#!/bin/bash
Alain Reguera Delgado e54180
######################################################################
Alain Reguera Delgado e54180
#
Alain Reguera Delgado e54180
#   tcar_getFilesList.sh -- This function standardizes the way list of
Alain Reguera Delgado e54180
#   files are built inside centos-art.sh script. This function outputs
Alain Reguera Delgado e54180
#   a sorted and unique list of files based on the options and
Alain Reguera Delgado e54180
#   locations passed as argument.
Alain Reguera Delgado e54180
#
Alain Reguera Delgado e54180
#   Written by:
Alain Reguera Delgado e54180
#   * Alain Reguera Delgado <al@centos.org.cu>, 2009-2013
Alain Reguera Delgado e54180
#
Alain Reguera Delgado e54180
# Copyright (C) 2009-2013 The CentOS Artwork SIG
Alain Reguera Delgado e54180
#
Alain Reguera Delgado e54180
# This program is free software; you can redistribute it and/or modify
Alain Reguera Delgado e54180
# it under the terms of the GNU General Public License as published by
Alain Reguera Delgado e54180
# the Free Software Foundation; either version 2 of the License, or (at
Alain Reguera Delgado e54180
# your option) any later version.
Alain Reguera Delgado e54180
#
Alain Reguera Delgado e54180
# This program is distributed in the hope that it will be useful, but
Alain Reguera Delgado e54180
# WITHOUT ANY WARRANTY; without even the implied warranty of
Alain Reguera Delgado e54180
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Alain Reguera Delgado e54180
# General Public License for more details.
Alain Reguera Delgado e54180
#
Alain Reguera Delgado e54180
# You should have received a copy of the GNU General Public License
Alain Reguera Delgado e54180
# along with this program; if not, write to the Free Software
Alain Reguera Delgado e54180
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Alain Reguera Delgado e54180
#
Alain Reguera Delgado e54180
######################################################################
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
function tcar_getFilesList {
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
    # Define short options.
Alain Reguera Delgado e54180
    local ARGSS=''
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
    # Define long options.
Alain Reguera Delgado e54180
    local ARGSL='pattern:,mindepth:,maxdepth:,type:,uid:'
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
    # Initialize pattern used to reduce the find output.
Alain Reguera Delgado e54180
    local PATTERN="${TCAR_FLAG_FILTER}"
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
    # Initialize options used with find command.
Alain Reguera Delgado e54180
    local OPTIONS=''
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
    # Initialize arguments with an empty value and set it as local
Alain Reguera Delgado e54180
    # variable to this function scope. Doing this is very important to
Alain Reguera Delgado e54180
    # avoid any clash with higher execution environments.
Alain Reguera Delgado e54180
    local TCAR_MODULE_ARGUMENT=''
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
    # Process all arguments currently available in this function
Alain Reguera Delgado e54180
    # environment. If either ARGSS or ARGSL local variables have been
Alain Reguera Delgado e54180
    # defined, argument processing goes through getopt for validation.
Alain Reguera Delgado e54180
    tcar_setModuleArguments "${@}"
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
    # Redefine positional parameters using TCAR_MODULE_ARGUMENT variable.
Alain Reguera Delgado e54180
    eval set -- "${TCAR_MODULE_ARGUMENT}"
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
    while true;do
Alain Reguera Delgado e54180
        case "${1}" in
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
            --pattern )
Alain Reguera Delgado e54180
                PATTERN="${2}"
Alain Reguera Delgado e54180
                shift 2
Alain Reguera Delgado e54180
                ;;
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
            --maxdepth )
Alain Reguera Delgado e54180
                OPTIONS="${OPTIONS} -maxdepth ${2}"
Alain Reguera Delgado e54180
                shift 2
Alain Reguera Delgado e54180
                ;;
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
            --mindepth )
Alain Reguera Delgado e54180
                OPTIONS="${OPTIONS} -mindepth ${2}"
Alain Reguera Delgado e54180
                shift 2
Alain Reguera Delgado e54180
                ;;
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
            --type )
Alain Reguera Delgado e54180
                OPTIONS="${OPTIONS} -type ${2}"
Alain Reguera Delgado e54180
                shift 2
Alain Reguera Delgado e54180
                ;;
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
            --uid )
Alain Reguera Delgado e54180
                OPTIONS="${OPTIONS} -uid ${2}"
Alain Reguera Delgado e54180
                shift 2
Alain Reguera Delgado e54180
                ;;
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
            -- )
Alain Reguera Delgado e54180
                shift 1
Alain Reguera Delgado e54180
                break
Alain Reguera Delgado e54180
                ;;
Alain Reguera Delgado e54180
        esac
Alain Reguera Delgado e54180
    done
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
    # At this point all options arguments have been processed and
Alain Reguera Delgado e54180
    # removed from positional parameters. Only non-option arguments
Alain Reguera Delgado e54180
    # remain so we use them as source location for find command to
Alain Reguera Delgado e54180
    # look files for.
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
    # Verify that locations does exist.
Alain Reguera Delgado e54180
    tcar_checkFiles -e ${@}
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
    # Redefine pattern as regular expression. When we use regular
Alain Reguera Delgado e54180
    # expressions with find, regular expressions are evaluated against
Alain Reguera Delgado e54180
    # the whole file path.  This way, when the regular expression is
Alain Reguera Delgado e54180
    # specified, we need to build it in a way that matches the whole
Alain Reguera Delgado e54180
    # path we are using. Doing so, every time we pass the `--filter'
Alain Reguera Delgado e54180
    # option in the command-line could be a tedious task.  Instead, in
Alain Reguera Delgado e54180
    # the sake of reducing some typing, we prepare the regular
Alain Reguera Delgado e54180
    # expression here to match the whole path using the regular
Alain Reguera Delgado e54180
    # expression provided by the user as pattern. Do not use locations
Alain Reguera Delgado e54180
    # as part of regular expression so it could be possible to use
Alain Reguera Delgado e54180
    # path expansion.  Using path expansion reduce the amount of
Alain Reguera Delgado e54180
    # places to find out things and so the time required to finish the
Alain Reguera Delgado e54180
    # task.
Alain Reguera Delgado e54180
    #
Alain Reguera Delgado e54180
    # Don't do such path expansion here. Instead, do it when you call
Alain Reguera Delgado e54180
    # this function. Otherwise you would be prohibiting the
Alain Reguera Delgado e54180
    # application of exact patterns. 
Alain Reguera Delgado e54180
    #PATTERN="^/.*${PATTERN}$"
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
    # Define list of files to process. At this point we cannot verify
Alain Reguera Delgado e54180
    # whether the location is a directory or a file since path
Alain Reguera Delgado e54180
    # expansion could be introduced to it. The best we can do is
Alain Reguera Delgado e54180
    # verifying exit status and go on.
Alain Reguera Delgado e54180
    find ${@} -regextype posix-egrep ${OPTIONS} -regex "${PATTERN}" | sort | uniq
Alain Reguera Delgado e54180
Alain Reguera Delgado e54180
}