#!/bin/bash
# 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"

NO_PROXY_NEEDED

if [ "x$1" = "x" ]; then
 if [ ! "x$DPM_HOST" = "x" ]; then
   node=$DPM_HOST
 else
  TEST_FAILED " No target node defined ! Exiting."
 fi
else
 node=$1
fi

if [ "x$2" = "x" ]; then
	if [ "x$VO" = "x" ]; then
		TEST_FAILED "No VO specified ! Exiting."
	fi
else
 VO=$2
fi

echo "Target node is $node. `date`"
echo "Launching RFIO CLI tests:"

mydomain=`echo $node | cut -d "." -f 2-`
mypath=/dpm/$mydomain/home/$VO/rfioclitest$RANDOM
myfile="$mypath/testfile"
myfile2="$mypath/testfile2"

export DPM_HOST=$node
export DPNS_HOST=$node
myglobalerror=0

report() {
 echo "  $message"
 if [ $ret -eq 0 ]; then
   echo SUCCESS
 else
   echo  FAILURE
   myglobalerror=1
 fi
}

message="Creating directory [ rfmkdir ] "
rfmkdir $mypath 
ret=$?
report


message="Copying file       [ rfcp ] "
rfcp /etc/hosts $myfile 2>&1 1>/dev/null
ret=$?
report

message="Reading file       [ rfcat ] "
rfcat $myfile 2>&1 1>/dev/null
ret=$?
report

message="Renaming file      [ rfrename ] "
rfrename $myfile ${myfile2}
ret=$?
report

message="Acessing file      [ rfstat ] "
rfstat ${myfile2} 2>&1 1>/dev/null
ret=$?
report

message="Changing permission[ rfstat ] "
rfchmod 777 ${myfile2} 2>&1 1>/dev/null
ret=$?
report

message="Removing file      [ rfrm ] "
rfrm ${myfile2}
ret=$?
report

message="Permission denied  [ rfmkdir ] "
rfmkdir /dpm/alma/korte 2>/dev/null 1>/dev/null
ret=$? 
 if [ $ret -ne 0 ]; then ret=0 
  else ret=1; fi
report


message="Removing dir       [ rfrename ] "
rfrm -f -r $mypath
ret=$?
report


message="Overall RFIO CLI test result:"
if [ $myglobalerror -ne 0 ]; then
  TEST_FAILED
else
  TEST_PASSED
fi

