#!/bin/bash
# meta: proxy=True
# meta: preconfig=../../DPM-config
##############################################################################
# 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: Dimitar Shiyachki <Dimitar.Shiyachki@cern.ch>
#
##############################################################################

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

PROXY_NEEDED

TEST_DIR=d`date +%s%N`
TEST_SUBDIR1=s1`date +%s%N`
TEST_SUBDIR2=s2`date +%s%N`
TEMP_FILE=/tmp/_dpm_test_temp`date +%s%N`

cleanup ()
{
   lcg-del -v -b --nolfc srm://$DPM_HOST:$SRMV2_PORT/srm/managerv2\?SFN=/$DPNS_HOME/$TEST_DIR/bash_copy_1
   dpns-rm $DPNS_HOME/$TEST_SUBDIR1/link_1
   dpns-rm $DPNS_HOME/$TEST_SUBDIR1/link_2
   dpns-rm -r $TEST_DIR
   rm -f $TEMP_FILE
}

echo "Creating directory $TEST_DIR/$TEST_SUBDIR1/$TEST_SUBDIR2"
dpns-mkdir -p $TEST_DIR/$TEST_SUBDIR1/$TEST_SUBDIR2

echo "Copying a file in $TEST_DIR/$TEST_SUBDIR1"
lcg-cp -v -b -D srmv2 file:/bin/bash srm://$DPM_HOST:$SRMV2_PORT/srm/managerv2\?SFN=/$DPNS_HOME/$TEST_DIR/bash_copy_1

echo "Creating a link named link_1 in $TEST_DIR/$TEST_SUBDIR1/$TEST_SUBDIR2 pointing to bash_copy_1"
dpns-ln -s $DPNS_HOME/$TEST_DIR/bash_copy_1 $TEST_DIR/$TEST_SUBDIR1/link_1

echo "Renaming link link_1 to link_2"
dpns-rename $TEST_DIR/$TEST_SUBDIR1/link_1 $DPNS_HOME/$TEST_DIR/$TEST_SUBDIR1/link_2
ret=$?
if [ $ret -ne 0 ]; then
   cleanup
   TEST_FAILED "dpns-rename exits with code $ret. Failure"
fi

echo "Trying to copy the file locally using the renamed link"
lcg-cp -v -b -D srmv2 srm://$DPNS_HOST:$SRMV2_PORT/srm/managerv2\?SFN=/$DPNS_HOME/$TEST_DIR/$TEST_SUBDIR1/link_2 file:/$TEMP_FILE
if [ $? -ne 0 ]; then
   cleanup
   TEST_FAILED "lcg-cp operation targeting the renamed link failed. Test has failed."
fi

echo "Comparing the file transfered back with the original"
if ! cmp $TEMP_FILE /bin/bash; then
   cleanup
   TEST_FAILED "File contents mismatch"
fi

echo "Renaming the directory where bash_copy_1 resides"
dpns-rename $TEST_DIR ${TEST_DIR}_renamed
ret=$?
if [ $ret -ne 0 ]; then
   cleanup
   TEST_FAILED "dpns-renamed exited with code $ret during directory rename operation"
fi

echo "Trying to fetch bash_copy_1 from the renamed parent directory"
lcg-cp -v -b -D srmv2 srm://$DPNS_HOST:$SRMV2_PORT/srm/managerv2\?SFN=/$DPNS_HOME/${TEST_DIR}_renamed/bash_copy_1 file:/$TEMP_FILE
if [ $? -ne 0 ]; then
   cleanup
   TEST_FAILED "lcg-cp operation targeting the renamed link failed. Test has failed."
fi

echo "Comparing the file transfered back with the original"
if ! cmp $TEMP_FILE /bin/bash; then
   cleanup
   TEST_FAILED "File contents mismatch"
fi

echo "Renaming the directory back to its original name"
dpns-rename ${TEST_DIR}_renamed $TEST_DIR

echo
echo "OVERALL RESULT:"
TEST_PASSED

