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

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

tokendesc="TOKEN`date +%s`"
echo "Token description: $tokendesc"

filename="bug45711_test_`date +%s`"
echo "Using the file $filename"

test_bug45711_pre () {

   if [ -z "${VO}" ]; then
     echo "VO is not defined."
     return 1
   fi

   if [ -z "${DPM_HOST}" ]; then
     echo "DPM_HOST is not defined."
     return 1
   fi
   
   return 0
}

test_bug45711_post () {

   if dpm-listspaces | grep -q "$tokendesc"; then 
      DPM_HOST_EXEC "dpm-releasespace --token_desc $tokendesc"
   fi

   if dpns-ls /dpm/cern.ch/home/$VO/$filename | grep -q "$filename"; then
      lcg-del -l srm://$DPM_HOST/dpm/cern.ch/home/$VO/$filename
   fi

   return 0
}

test_bug45711 () {

   DPM_HOST_EXEC "dpm-reservespace --gspace 16M  --token_desc $tokendesc --group $VO"
   if [ $? -ne 0 ]; then
      echo "Error reserving space"
      return 1
   else
      export space_reserved="yes"
   fi

   lcg-cp -v -b -D srmv2 --dst $tokendesc file:/bin/bash srm://$DPM_HOST:$SRMV2_PORT/srm/managerv2\?SFN=/dpm/cern.ch/home/$VO/$filename
   if [ $? -ne 0 ]; then
      echo "Error copying file to space"
      return 1
   fi

   size=$(dpm-listspaces -g | gawk -v spacename="$tokendesc" 'BEGIN {
                                                       located = 0
                                                       pattern2 = "^dn: GlueSALocalID=" spacename ":"
                                                    }
                                                    $0 ~ pattern2 { located = 1 }
                                                    located == 1 && $0 ~ /^GlueSAStateUsedSpace: / {
                                                       result = $0
                                                       located = 0
                                                    }
                                                    $0 == "" { located = 0 }
                                                    END { 
                                                       sub ("GlueSAStateUsedSpace: ", "", result)
                                                       print result
                                                    }')

   echo "Size is: $size"

   if [ $size -eq 0 ]; then
      echo "Error in information provider. Wrong space usage reported."
      return 1
   fi

   return 0
}


if test_bug45711_pre; then
  if test_bug45711; then
    if test_bug45711_post; then
      TEST_PASSED
    else
      TEST_FAILED
    fi
  else
    test_bug45711_post
    TEST_FAILED
  fi
else
  TEST_FAILED
fi

