#!/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 file for VO
# 
# Input Parameters:
#       - VO name
#       - LFC host name
# Requisites:
#	- LFC server
#       - 1 configured VO
#
##############################################################################
# meta: preconfig=../../LFC-config
# meta: proxy=true

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

PROXY_NEEDED

GUID=""

# Information
date
echo "Test LFC CLI remove file lfc-rm <LFN>"
echo "VO=$VO"
echo "FILE=$FILE"
echo "SE=$SE_HOST"
echo "LCG_GFAL_INFOSYS=$LCG_GFAL_INFOSYS"

# Directory where we are going to execute the tests
main_dir="$LFCDIR/rmfile"

echo "Strong check if directory nonexists"
echo "List of nonexist directory"
lfc-ls -l $main_dir
lfc_clear $main_dir
echo "OK"

echo
echo "2. Remove ordinary file"

echo "2.1. Create main directory: $main_dir"
lfc-mkdir -p $main_dir
fname="test_file"
LFN="$main_dir/$fname"
echo "SE=$SE_HOST, LFN=$LFN, file=$FILE"

echo "2.2. Create guid and lfn:$LFN"

fguid=`lcg-cr -v -l $LFN -d $SE_HOST --vo $VO file:$FILE`
GUID=$fguid
echo $fguid
echo $fguid > $LFC_TEST_TMP

grep "guid:" $LFC_TEST_TMP > /dev/null
if [ $? == 1 ]; then
    cat $LFC_TEST_TMP
    lfc_clear $main_dir
    TEST_FAILED "Noncorrect create GUID, exit"
fi
echo "OK"
                    
echo "2.3. Remove 'read only' file"
echo "2.3.1. Change access filr mode to 400"
lfc-chmod 400 $LFN
lfc-rm -f $LFN 2> $LFC_TEST_ERR
cat $LFC_TEST_ERR
grep "File exists" $LFC_TEST_ERR > /dev/null    # Because GUID exists!!!

if [ $? == 1 ]; then 
   cat $LFC_TEST_ERR
   lfc-chmod 777 $LFN
   lfc_delGuid $GUID
   lfc_clear $main_dir
   TEST_FAILED "Non correct lfc-rm result"
fi                    
echo "Restore file protection to 777"
lfc-chmod 777 $LFN

                    
echo "Remove created LFN and guid"
echo "File list before:"
lfc-ls $main_dir
echo "Remove LFN without removing GUID"

lfc-rm $LFN 2> $LFC_TEST_ERR
cat $LFC_TEST_ERR

grep "File exists"  $LFC_TEST_ERR  > /dev/null
if [ $? == 1 ]; then
    echo "Non correct lfc-rename result"
    echo "-TEST FAILED-"
    lfc_delGuid $GUID
    lfc_clear $main_dir
    exit -1
fi
echo "OK"


echo "2. SCENARIO: Delete nonexisting file: $main_dir/nofile"

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

if [ $? == 1 ]; then 
   cat $LFC_TEST_ERR
   lfc_delGuid $GUID
   lfc_clear $main_dir
   TEST_FAILED "Noncorrect diagnose, exit"
fi

echo "OK"

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

TEST_PASSED

