Blame Scripts/Bash/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
#
7cd8e9
# Copyright (C) 2009, 2010 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
b76c02
    # Get absolute path to file/directory.
18d741
    local FILE="$1"
b76c02
b76c02
    # Get verification type.
4c79b5
    local TYPE="$2"
b76c02
b76c02
    # Initialize message output.
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
b76c02
    # Check value passed as file to cli_checkFiles function. The file
b76c02
    # value cannot be empty.
18d741
    if [[ $FILE == '' ]];then
c8158d
        cli_printMessage "cli_checkFiles: `gettext "The first argument cannot be empty."`" 'AsErrorLine'
76ad94
        cli_printMessage "$(caller)" "AsToKnowMoreLine"
18d741
    fi
4c79b5
b76c02
    # Perform file verification using FILE and TYPE variables.
18d741
    case $TYPE in
4c79b5
18d741
        d | directory )
18d741
            # File exists and is a directory
18d741
            if [[ ! -d $FILE ]];then
b76c02
                cli_printMessage "`eval_gettext "The directory \\\"\\\$FILE\\\" doesn't exist."`"
b76c02
                cli_printMessage "`gettext "Do you want to create it now?"`" 'AsYesOrNoRequestLine'
b76c02
                mkdir -p $FILE
18d741
            fi
18d741
            ;;
4c79b5
18d741
        f | regular-file )
18d741
            # File exists and is a regular file.
18d741
            if [[ ! -f $FILE ]];then
18d741
                MESSAGE="`eval_gettext "The file \\\"\\\$FILE\\\" is not a regular file."`"
18d741
            fi
18d741
            ;;
4c79b5
18d741
        h | symbolic-link )
18d741
            # File exists and is a symbolic link.
18d741
            if [[ ! -h $FILE ]];then
18d741
                MESSAGE="`eval_gettext "The file \\\"\\\$FILE\\\" is not a symbolic link."`"
18d741
            fi
18d741
            ;;
4c79b5
b76c02
        x | execution )
b76c02
            # To exist, file should be executable.
b76c02
            if [[ ! -x $FILE ]];then
b76c02
                MESSAGE="`eval_gettext "The file \\\"\\\$FILE\\\" is not executable."`"
b76c02
            fi
b76c02
            ;;
b76c02
18d741
        fh )
18d741
            # To exist, file should be a regular file or a symbolic link.
b76c02
            if [[ ! -f $FILE ]];then
b76c02
                if [[ ! -h $FILE ]];then
c8158d
                    MESSAGE="`eval_gettext "The path \\\"\\\$FILE\\\" doesn't exist."`"
4c79b5
                fi
18d741
            fi
18d741
            ;;
4c79b5
0f02c4
        fd )
0f02c4
            # To exist, file should be a regular file or a directory.
0f02c4
            if [[ ! -f $FILE ]];then
0f02c4
                if [[ ! -d $FILE ]];then
0f02c4
                    MESSAGE="`eval_gettext "The path \\\"\\\$FILE\\\" doesn't exist."`"
0f02c4
                fi
0f02c4
            fi
0f02c4
            ;;
0f02c4
b8ce89
        isInWorkingCopy )
b8ce89
            # To exist, file should be inside the working copy.
b8ce89
            if [[ $FILE =~ "^/home/centos/artwork/.+$" ]];then
b8ce89
                MESSAGE="`eval_gettext "The path \\\"\\\$FILE\\\" doesn't exist inside the working copy."`"
b8ce89
            fi
b8ce89
            ;;
b8ce89
18d741
        * )
18d741
            # File exists.
18d741
            if [[ ! -a $FILE ]];then
c8158d
                MESSAGE="`eval_gettext "The path \\\"\\\$FILE\\\" doesn't exist."`"
18d741
            fi
4c79b5
18d741
    esac
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
}