Blame Scripts/Bash/Functions/cli_checkFiles.sh

4c79b5
#!/bin/bash
4c79b5
#
18d741
# cli_checkFiles.sh -- This function checks file existence.
4c79b5
#
4c79b5
# Copyright (C) 2009-2010 Alain Reguera Delgado
4c79b5
# 
4c79b5
# This program is free software; you can redistribute it and/or
4c79b5
# modify it under the terms of the GNU General Public License as
4c79b5
# published by the Free Software Foundation; either version 2 of the
4c79b5
# 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
# ----------------------------------------------------------------------
4c79b5
# $Id: cli_checkFiles.sh 98 2010-09-19 16:01:53Z al $
4c79b5
# ----------------------------------------------------------------------
4c79b5
4c79b5
function cli_checkFiles {
4c79b5
4c79b5
    # Define variables as local to avoid conflicts outside.
18d741
    local FILE="$1"
4c79b5
    local TYPE="$2"
4c79b5
    local ACTION="$3"
4c79b5
    local MESSAGE=''
4c79b5
4c79b5
    # Check amount of paramaters passed. At least one argument should
4c79b5
    # be passed.
4c79b5
    if [[ $# -lt 1 ]];then
18d741
        cli_printMessage "cli_checkFiles: `gettext "You need to provide one argument at least."`"
18d741
        cli_printMessages "$(caller)" "AsToKnowMoreLine"
4c79b5
    fi
4c79b5
1f1b3c
    # Define default action to take, if it is not specified.
4c79b5
    if [[ ! "$3" ]] || [[ "$3" == '' ]];then
4c79b5
        ACTION=`gettext "Checking"`
4c79b5
    fi
4c79b5
18d741
    # Check file provided.
18d741
    if [[ $FILE == '' ]];then
18d741
        MESSAGE="`gettext "Unknown"`"
18d741
        cli_printMessage "${ACTION}: $MESSAGE"
18d741
        return 1
18d741
    fi
4c79b5
18d741
    # Check file types.
18d741
    case $TYPE in
4c79b5
18d741
        d | directory )
18d741
            # File exists and is a directory
18d741
            if [[ ! -d $FILE ]];then
18d741
                MESSAGE="`eval_gettext "The directory \\\"\\\$FILE\\\" doesn't exist."`"
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
18d741
        fh )
18d741
            # To exist, file should be a regular file or a symbolic link.
18d741
            if [[ ! -f $FILE ]];then
18d741
                if [[ ! -h $FILE ]];then
18d741
                    MESSAGE="`eval_gettext "The file \\\"\\\$FILE\\\" doesn't exist."`"
4c79b5
                fi
18d741
            fi
18d741
            ;;
4c79b5
18d741
        * )
18d741
            # File exists.
18d741
            if [[ ! -a $FILE ]];then
18d741
                MESSAGE="`eval_gettext "The file \\\"\\\$FILE\\\" doesn't exist."`"
18d741
            fi
4c79b5
18d741
    esac
4c79b5
18d741
   # If there is some message print it. 
18d741
   if [[ "$MESSAGE" == '' ]];then
18d741
       cli_printMessage "${ACTION}: $FILE"
18d741
       return 0
18d741
   else
18d741
       cli_printMessage "${ACTION}: $MESSAGE"
18d741
       return 1
18d741
   fi
4c79b5
4c79b5
}