#!/bin/bash
##############################################################################
# Copyright (c) Members of the EGEE Collaboration. 2004.
# See http://www.eu-egee.org/partners/ for details on the copyright
# holders.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS
# OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##############################################################################
#
# AUTHORS: Victor Galaktionov e-mail: victor.galaktionov@cern.ch 
#
# Description of the test: Change access mode of  LFC subdirectories for VO
#
# Input Parameters:
#       - VO name
#       - LFC host name
# Requisites:
#	- LFC server
#       - 1 configured VO
#
##############################################################################
# meta: preconfig=../../LFC-config
# meta: proxy=true

access=( --- --x -w- -wx r-- r-x rw- rwx )

source ./lfc-common
SCRIPTDIR="$(dirname "$(readlink -f ${BASH_SOURCE})")"
source "${SCRIPTDIR}/../../Macros"

PROXY_NEEDED

# Directory where we are going to test
main_dir="$LFCDIR/test-chdir"

# Some useful information
date
echo "Test LFC CLI chmod for directory"
echo "VO=$VO"
echo "LFCDIR=$LFCDIR"
echo

# First scenario: directory does not exist
echo "SCENARIO: Directory doesn't exist"

cdir="$main_dir/test-qwerty"

echo "1. List of nonexist directory"
lfc-ls -l $cdir &> /dev/null
if [ $? -eq 0 ]; then
  TEST_FAILED "The directory $main_dir seems to exist, or lfc-ls does not finish properly"
fi
echo "OK"

echo "2. Remove nonexist directory"
lfc-chmod 777 $cdir &> /dev/null
lfc_clear $cdir &> /dev/null
if [ $? -eq 0 ]; then
  TEST_FAILED "The directory $cdir does not exists, but lfc-rm does not report an error"
fi
echo "OK"

echo "3. Change mode for nonexist directory"
lfc-chmod 777 $cdir > /dev/null 2> $LFC_TEST_TMP
grep "No such file or directory" $LFC_TEST_TMP > /dev/null
if [ $? == 1 ]; then 
  cat $LFC_TEST_TMP >&2
  TEST_FAILED "The file does not exists, but lfc-chmod does not report properly"
fi
echo "OK"

# Second scenario: directory exists
echo
echo "SCENARIO: check mode for existing directory"
echo "SHORT=$SHORT, MODE=$MODE"

echo "1. Create main directory: $main_dir"
lfc-mkdir -p $main_dir
if [ $? -ne 0 ]; then
  TEST_FAILED "The directory $main_dir could not be created"
fi

cdir=$main_dir/test-subdir
echo "2. Create test subdirectory: $cdir"
lfc-mkdir $cdir
if [ $? -ne 0 ]; then
  TEST_FAILED "The subdirectory $cdir could not be created"
fi

if [ $SHORT == 1 ]; then
    echo "Short mode: 000, 777"
    lfc_checkMode 0 0 0 $cdir
    lfc_checkMode 2 2 2 $cdir
    lfc_checkMode 3 3 3 $cdir
    lfc_checkMode 6 6 6 $cdir
    lfc_checkMode 7 7 7 $cdir
else
    echo "Long mode: all mode 000-nnn, n<$MODE"
    UMAX=$MODE
    
    owner=0
    while [ $owner -lt $UMAX ]; do              # cycle for owner access
        group=0
        echo
        while [ $group -lt $UMAX ]; do          # cycle for group access
            user=0
            echo
            while [ $user -lt $UMAX ]; do       # cycle for all users
                lfc_checkMode $owner $group $user $cdir
                let user=$user+1
            done
            let group=$group+1
        done
        let owner=$owner+1
    done
fi

echo
echo "Cleaning tasks"
lfc-chmod 777 $cdir
lfc_clear $main_dir

rm -rf $LFC_TEST_TMP
rm -rf $LFC_TEST_ERR

TEST_PASSED

