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
# 
6253fc
# This program is free software; you can redistribute it and/or modify
6253fc
# it under the terms of the GNU General Public License as published by
6253fc
# the Free Software Foundation; either version 2 of the License, or
6253fc
# (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
4c79b5
    # Define variables as local to avoid conflicts outside.
18d741
    local FILE="$1"
4c79b5
    local TYPE="$2"
4c79b5
    local ACTION="$3"
6253fc
    local OPTIONS="$4"
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"`"
6253fc
        cli_printMessage "${ACTION}: $MESSAGE" "AsRegularLine"
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.
11fc17
            cli_checkFiles $FILE 'f' '' '--quiet'
11fc17
            if [[ $? -ne 0 ]];then
11fc17
                cli_checkFiles $FILE 'h' '' '--quiet'
11fc17
                if [[ $? -ne 0 ]];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
6253fc
    # If there is some message print it when there is no `--quiet'
6253fc
    # option passed as fourth argument. 
6253fc
    if [[ "$MESSAGE" == '' ]];then
6253fc
        for OPTION in $OPTIONS;do 
6253fc
            case $OPTION in
6253fc
                '--quiet' )
6253fc
                    return 0
6253fc
            esac
6253fc
        done
6253fc
        cli_printMessage "${ACTION}: $FILE" "AsRegularLine"
6253fc
        return 0
6253fc
    else
6253fc
        for OPTION in $OPTIONS;do 
6253fc
            case $OPTION in
6253fc
                '--quiet' )
6253fc
                    return 1
6253fc
            esac
6253fc
        done
6253fc
        cli_printMessage "${ACTION}: $MESSAGE" "AsRegularLine"
6253fc
        return 1
6253fc
    fi
4c79b5
4c79b5
}