Blame Scripts/Bash/Functions/cli_getFilesList.sh

b7b189
#!/bin/bash
b7b189
#
b7b189
# cli_getFilesList.sh -- This function defines the list of FILES to
b7b189
# process.
b7b189
#
92e46a
# Copyright (C) 2009-2011 Alain Reguera Delgado
b7b189
# 
b7b189
# This program is free software; you can redistribute it and/or
b7b189
# modify it under the terms of the GNU General Public License as
b7b189
# published by the Free Software Foundation; either version 2 of the
b7b189
# License, or (at your option) any later version.
b7b189
# 
b7b189
# This program is distributed in the hope that it will be useful, but
b7b189
# WITHOUT ANY WARRANTY; without even the implied warranty of
b7b189
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
b7b189
# General Public License for more details.
b7b189
#
b7b189
# You should have received a copy of the GNU General Public License
b7b189
# along with this program; if not, write to the Free Software
b7b189
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
b7b189
# USA.
b7b189
# 
b7b189
# ----------------------------------------------------------------------
b7b189
# $Id$
b7b189
# ----------------------------------------------------------------------
b7b189
b7b189
function cli_getFilesList {
b7b189
8eeb3a
    local LOCATION=''
f5c4be
    local FILTER=''
f5c4be
    local FILES=''
8eeb3a
8eeb3a
    # If first argument is provided to cli_getFilesList, use it as
8eeb3a
    # default location. Otherwise, if first argument is not provided,
8eeb3a
    # location takes the action value (ACTIONVAL) as default.
8eeb3a
    if [[ "$1" != '' ]];then
8eeb3a
        LOCATION="$1"
8eeb3a
    else
8eeb3a
        LOCATION="$ACTIONVAL"
8eeb3a
    fi
8eeb3a
f5c4be
    # If second argument is provided to cli_getFilesList, use it as
f5c4be
    # default extension to look for files. Otherwise, if second
f5c4be
    # argument is not provided, use flag filter instead.
f5c4be
    if [[ "$2" != '' ]];then
f5c4be
        FILTER="$2"
f5c4be
    else
f5c4be
        FILTER="$FLAG_FILTER"
f5c4be
    fi
f5c4be
f5c4be
    # Define filter as regular expression pattern. When we use regular
f5c4be
    # expressions with find, regular expressions are evaluated against
f5c4be
    # the whole file path.  This way, when the regular expression is
4a9a32
    # specified, we need to build it in a way that matches the whole
f5c4be
    # path. Doing so, everytime we pass the `--filter' option in the
f5c4be
    # command-line might be a tedious task. Instead, in the sake of
f5c4be
    # reducing some typing, we prepare the regular expression here to
f5c4be
    # match the whole path using the regular expression provided by
f5c4be
    # the user as pattern.
f5c4be
    FILTER="^${LOCATION}/${FILTER}$"
8eeb3a
8eeb3a
    # Define list of files to process.
8eeb3a
    if [[ -d $LOCATION ]];then
f5c4be
        FILES=$(find $LOCATION -regextype posix-egrep -type f -regex "${FILTER}" | sort)
8eeb3a
    elif [[ -f $LOCATION ]];then
8eeb3a
        FILES=$LOCATION
8eeb3a
    fi
8eeb3a
7b9a4d
    # Check list of files to process. If we have an empty list of
7b9a4d
    # files, inform about it and stop script execution. Otherwise
7b9a4d
    # print list of files.
7b9a4d
    if [[ "$FILES" == '' ]];then
7b9a4d
        cli_printMessage "`gettext "There is no file to process."`" 'AsErrorLine'
7b9a4d
        cli_printMessage "$(caller)" 'AsToKnowMoreLine'
7b9a4d
    else
7b9a4d
        echo "$FILES"
7b9a4d
    fi
b7b189
b7b189
}