#!/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: Rename  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

main_dir="$LFCDIR/renamedir"

echo "Scenario: Directory exists"
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


rdname="rdonly"
rdon="$main_dir/$rdname"
newDn="newDname"
newDname="$main_dir/$newDn"

echo "2. Rename 'read only' directory"
echo "This requires write permission in the parent directories, not current"

lfc-mkdir -m 400 $rdon
lfc-rename $rdon $newDname 2> $LFC_TEST_ERR
cat $LFC_TEST_ERR >&2
grep "Permission denied" $LFC_TEST_ERR > /dev/null
if [ $? == 0 ]; then	# non correct (the name should have changed)
   lfc-ls -l "$main_dir"
   lfc-chmod 777 $rdon
   lfc_clear $main_dir
   TEST_FAILED "Non correct lfc-rename result, Test try rename read-only directory and with write permission for parent directory, lfc-rename MUST rename this directory!!!"
fi
echo "OK"
lfc_clear $newDname

echo
echo "2. Rename ordinary directory"
lfc-mkdir -m 400 $rdon

if [ $? -ne 0 ]; then
  TEST_FAILED "$rdon could not be created"
fi

lfc-chmod 777 $rdon
lfc-rename $rdon $newDname

if [ $? -ne 0 ]; then
  TEST_FAILED "$rdon could not be renamed"
fi

lfc-ls $main_dir > $LFC_TEST_TMP

grep $newDn $LFC_TEST_TMP > /dev/null

if [ $? == 1 ]; then 
   lfc_clear $main_dir
   TEST_FAILED "Non correct lfc-rename result"
fi

#clear $newDname
echo "OK"

echo "2. Rename by using LFC_HOME"

export LFC_HOME=$main_dir
newdir="newdirname"
lfc-rename $newDn $newdir
lfc-ls $main_dir > $LFC_TEST_TMP
grep $newdir $LFC_TEST_TMP  > /dev/null

if [ $? == 1 ]; then 
   lfc_clear $main_dir
   TEST_FAILED "Non correct lfc-rename result"
fi
echo "OK"


echo "3. SCENARIO: Rename nonexisting directory: $rdon"

lfc-rename $rdon $newDname 2> $LFC_TEST_ERR
grep "No such file or directory" $LFC_TEST_ERR > /dev/null

if [ $? == 1 ]; then 
   cat $LFC_TEST_ERR >& 2
   lfc_clear $main_dir
   TEST_FAILED "Noncorrect diagnose, exit"
fi
echo "OK"

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

TEST_PASSED

