##############################################################################
# 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=../../LFC-config

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

test_bug49345_pre () {

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

   return 0
}

test_bug49345_post () {

   echo "Remove test directory $LFCDIR/bug49345"
   lfc-rm -r ${LFCDIR}/bug49345

   if [ $? -ne 0 ]; then
      echo "Cannot remove directory $LFCDIR/bug49345 on LFC server: $LFC_HOST"
      return 1
   fi

   if [ "$POST_REMOVE" == "yes" ]; then
     lfc-rm -r ${LFCDIR}
   fi

   return 0
}

test_bug49345 () {

   echo "Checking LFCDIR existence..."
   lfc-ls ${LFCDIR} &> /dev/null

   if [ $? -ne 0 ]; then
      echo "LFCDIR not found on LFC server: $LFC_HOST"
      lfc-mkdir -p ${LFCDIR}
      if [ $? -ne 0 ]; then
        echo "Could not create $LFCDIR!"
        return 1
      fi
      POST_REMOVE="yes"
   fi

   echo "Create directory $LFCDIR/bug49345 for rights manipulations"
   lfc-mkdir ${LFCDIR}/bug49345

   if [ $? -ne 0 ]; then
      echo "Cannot create $LFCDIR/bug49345 on LFC server: $LFC_HOST"
      return 1
   fi

   echo "Do lfc-chmod using invalid rights field."
   lfc-chmod 33333333333 ${LFCDIR}/bug49345

   if [ $? -eq 0 ]; then
      echo "lfc-chmod with invalid right succeeded!"
      return 1
   fi
 
   return 0
}


if (test_bug49345_pre); then
  if (test_bug49345); then
    if (test_bug49345_post); then
       TEST_PASSED
    else
       TEST_FAILED
    fi
  else
    TEST_FAILED
  fi
else
  TEST_FAILED
fi

