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

4c79b5
#!/bin/bash
4c79b5
#
b76c02
# cli_checkFiles.sh -- This function standardizes file verifications
b76c02
# inside centos-art.sh script.  If file verification fails in anyway,
b76c02
# centos-art.sh script complains about it and ends up script
b76c02
# execution.
b76c02
#
7cd8e9
# Usage:
b76c02
#
7cd8e9
# cli_checkFiles FILE [TYPE]
4c79b5
#
4c8908
# Copyright (C) 2009-2011 Alain Reguera Delgado
4c79b5
# 
7cd8e9
# This program is free software; you can redistribute it and/or
7cd8e9
# modify it under the terms of the GNU General Public License as
7cd8e9
# published by the Free Software Foundation; either version 2 of the
7cd8e9
# License, or (at your option) any later version.
4c79b5
# 
4c79b5
# This program is distributed in the hope that it will be useful, but
4c79b5
# WITHOUT ANY WARRANTY; without even the implied warranty of
4c79b5
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
4c79b5
# General Public License for more details.
4c79b5
#
4c79b5
# You should have received a copy of the GNU General Public License
4c79b5
# along with this program; if not, write to the Free Software
4c79b5
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
4c79b5
# USA.
4c79b5
# 
4c79b5
# ----------------------------------------------------------------------
418249
# $Id$
4c79b5
# ----------------------------------------------------------------------
4c79b5
4c79b5
function cli_checkFiles {
4c79b5
288be2
    local FILE=''
288be2
    local FILES="$1"
4c79b5
    local TYPE="$2"
4c79b5
    local MESSAGE=''
4c79b5
b76c02
    # Check number of paramaters passed to cli_checkFiles function. At
b76c02
    # least one argument should be passed.
4c79b5
    if [[ $# -lt 1 ]];then
c8158d
        cli_printMessage "cli_checkFiles: `gettext "You need to provide one argument at least."`" 'AsErrorLine'
76ad94
        cli_printMessage "$(caller)" "AsToKnowMoreLine"
4c79b5
    fi
4c79b5
288be2
    for FILE in $FILES;do
288be2
288be2
        # Check value passed as file to cli_checkFiles function. The
288be2
        # file value cannot be empty nor have extrange values.
288be2
        cli_checkPathComponent "$FILE" '--default'
288be2
288be2
        # Perform file verification using FILE and TYPE variables.
288be2
        case $TYPE in
288be2
288be2
            d | directory )
288be2
                # File exists and is a directory
288be2
                if [[ ! -d $FILE ]];then
288be2
                    MESSAGE="`eval_gettext "The directory \\\"\\\$FILE\\\" doesn't exist."`"
288be2
                fi
288be2
                ;;
288be2
288be2
            f | regular-file )
288be2
                # File exists and is a regular file.
288be2
                if [[ ! -f $FILE ]];then
288be2
                    MESSAGE="`eval_gettext "The file \\\"\\\$FILE\\\" is not a regular file."`"
288be2
                fi
288be2
                ;;
288be2
288be2
            h | symbolic-link )
288be2
                # File exists and is a symbolic link.
b76c02
                if [[ ! -h $FILE ]];then
288be2
                    MESSAGE="`eval_gettext "The file \\\"\\\$FILE\\\" is not a symbolic link."`"
4c79b5
                fi
288be2
                ;;
4c79b5
288be2
            x | execution )
288be2
                # To exist, file should be executable.
288be2
                if [[ ! -x $FILE ]];then
288be2
                    MESSAGE="`eval_gettext "The file \\\"\\\$FILE\\\" is not executable."`"
288be2
                fi
288be2
                ;;
288be2
288be2
            fh )
288be2
                # To exist, file should be a regular file or a symbolic link.
288be2
                if [[ ! -f $FILE ]];then
288be2
                    if [[ ! -h $FILE ]];then
288be2
                        MESSAGE="`eval_gettext "The path \\\"\\\$FILE\\\" doesn't exist."`"
288be2
                    fi
288be2
                fi
288be2
                ;;
288be2
288be2
            fd )
288be2
                # To exist, file should be a regular file or a directory.
288be2
                if [[ ! -f $FILE ]];then
288be2
                    if [[ ! -d $FILE ]];then
288be2
                        MESSAGE="`eval_gettext "The path \\\"\\\$FILE\\\" doesn't exist."`"
288be2
                    fi
288be2
                fi
288be2
                ;;
288be2
288be2
            isInWorkingCopy )
288be2
                # To exist, file should be inside the working copy.
288be2
                if [[ ! $FILE =~ "^/home/centos/artwork/.+$" ]];then
288be2
                    MESSAGE="`eval_gettext "The path \\\"\\\$FILE\\\" doesn't exist inside the working copy."`"
288be2
                fi
288be2
                ;;
288be2
288be2
            * )
288be2
                # File exists.
288be2
                if [[ ! -a $FILE ]];then
0f02c4
                    MESSAGE="`eval_gettext "The path \\\"\\\$FILE\\\" doesn't exist."`"
0f02c4
                fi
288be2
288be2
        esac
288be2
288be2
    done
4c79b5
b76c02
    # If file verification fails in anyway, output message information
b76c02
    # and end up script execution. Otherwise, continue with script
b76c02
    # normal flow.
b76c02
    if [[ "$MESSAGE" != '' ]];then
c8158d
        cli_printMessage "$MESSAGE" "AsErrorLine"
76ad94
        cli_printMessage "$(caller)" "AsToKnowMoreLine"
6253fc
    fi
4c79b5
4c79b5
}