#!/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.
##############################################################################
# Date: 09/11/2007
# Author: Victor Galaktionov e-mail: victor.galaktionov@cern.ch
# Description of the test: Delete  LFC subdirectories for VO
# 
# Input Parameters:
#       - VO name
#       - LFC host name
# Requisites:
#	- LFC server 
#       - 1 configured VO 
# 
###############################################################################
# meta: preconfig=../../LFC-config

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

NO_PROXY_NEEDED

# Info
date
echo "Test LFC CLI remove diectory. lfc-rm -r <dirname>"
echo "VO=$VO"

# Where to test
main_dir="$LFCDIR/rmdir"

# First scenario: the directory exists
echo "Scenario: Directory exists"
echo "1. Create main directory: $main_dir"
lfc-mkdir -p $main_dir &> /dev/null
if [ $? -ne 0 ]; then
  TEST_FAILED "$main_dir could not be created"
fi

rdon="$main_dir/rdonly"
echo "2. Remove 'read only' directory"
lfc-mkdir -m 400 $rdon
lfc-rm -f -r $rdon 2> $LFC_TEST_ERR
grep "Permission denied" $LFC_TEST_ERR > /dev/null

if [ $? == 0 ]; then 
   lfc-chmod 777 $rdon &> /dev/null
   lfc_clear $main_dir
   TEST_FAILED "Non correct lfc-rm result"
fi
lfc-chmod 777 $rdon
lfc_clear $rdon
echo "OK"

echo
echo "3.  Create $DMAX subdirectories"
counter=0
dir=$main_dir
pdir=$main_dir

while [ $counter -lt $DMAX ]; do
  subdir="subdir-$counter"
  dir=$dir/$subdir
  echo "new dir:$dir"
  lfc-mkdir $dir
  lfc-ls $pdir > $LFC_TEST_TMP
  grep $subdir $LFC_TEST_TMP > /dev/null
  
  if [ $? == 1 ]; then 
    TEST_FAILED "$subdir could not be created"
  fi
  
  pdir=$dir
  let counter=counter+1
done

echo "4. Remove all subdirectories, use -R option"
lfc-rm -R  -f $main_dir
echo "5. Check deletion result"

lfc-ls $main_dir 2> $LFC_TEST_ERR
grep "No such file or directory" $LFC_TEST_ERR > /dev/null
 
if [ $? == 1 ]; then 
  lfc_clear $main_dir
  TEST_FAILED "The directory wasn't removed"
fi 
echo "OK"

echo "2. SCENARIO: Delete nonexisting directory: $main_dir"

lfc-rm -r -f $main_dir  2> $LFC_TEST_ERR
cat $LFC_TEST_ERR
grep "No such file or directory" $LFC_TEST_ERR > /dev/null

if [ $? == 0 ]; then 
  lfc_clear $main_dir
  TEST_FAILED "The directory did not exist, but lfc-rm failed to report"
fi

echo "OK"

echo
echo "Cleaning tasks"
lfc_clear $main_dir
rm -rf $LFC_TEST_TMP
rm -rf $LFC_TEST_ERR

TEST_PASSED

