#!/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

echo
echo "Working directory is: $DPNS_HOME"
echo

echo -n "Creating directory $TEST_DIR using simple call"
dpns-mkdir $TEST_DIR
if [ $? -eq 0 ]; then
   echo "...ok"
   echo -n "Checking its existence with dpns-ls"
   if dpns-ls -l | grep -q $TEST_DIR; then
      echo "...ok"
   else
      TEST_FAILED "The directory was not created, but dpns-mkdir exited with 0"
   fi
   dpns-rm -r $TEST_DIR
else
   TEST_FAILED "The directory could not be created"
fi

echo
echo -n "Creating subdirectories hierarchy $TEST_DIR/$TEST_SUBDIR1/$TEST_SUBDIR2 using '-p' call"
dpns-mkdir -p $TEST_DIR/$TEST_SUBDIR1/$TEST_SUBDIR2
if [ $? -eq 0 ]; then
   echo "...ok"
   echo -n "Checking its existence with dpns-ls"
   if dpns-ls -l $TEST_DIR/$TEST_SUBDIR1 | grep -q $TEST_SUBDIR2; then
      echo "...ok"
   else
      TEST_FAILED "The subdirectory was not created, but dpns-mkdir exited with 0"
   fi
   dpns-rm -r $TEST_DIR
else
   TEST_FAILED "The subdirectory could not be created"
fi

echo
echo -n "Create a directory using -m 700"
dpns-mkdir -m 700 -p $TEST_DIR
if [ $? -eq 0 ]; then
   echo "...ok"
   echo -n "Checking its existence and access permissions with dpns-ls"
   if dpns-ls -l | grep $TEST_DIR | grep -q "^drwx\-\-\-\-\-\-"; then
      echo "...ok"
   else
      TEST_FAILED "The directory hasn't been created, or the permissions are wrong"
   fi
   dpns-rm -r $TEST_DIR
else
   TEST_FAILED "The directory could not be created"
fi

echo
echo -n "Create a directory using -m 707 -p"
dpns-mkdir -m 707 -p $TEST_DIR/$TEST_SUBDIR1/$TEST_SUBDIR2
if [ $? -eq 0 ]; then
   echo "...ok"
   echo -n "Checking its existence and access permissions with dpns-ls"
   if dpns-ls -l $TEST_DIR/$TEST_SUBDIR1 | grep $TEST_SUBDIR2 | grep -q "^drwx\-\-\-rwx"; then
      echo "...ok"
   else
      TEST_FAILED "The directory has not been created, or the permissions are wrong"
   fi
   dpns-rm -r $TEST_DIR
else
   TEST_FAILED "The directory could not be created"
fi

echo
echo -n "Creating a directory using absolute path"
dpns-mkdir -p "$DPNS_HOME/$TEST_DIR/$TEST_SUBDIR1"
if [ $? -eq 0 ]; then
   echo "...ok"
   echo -n "Checking its existence with dpns-ls"
   if dpns-ls -l $TEST_DIR | grep -q $TEST_SUBDIR1; then
      echo "...ok"
   else
      TEST_FAILED "The directory has not been created, but dpns-mkdir exited with 0"
   fi
   dpns-rm -r $TEST_DIR
else
   TEST_FAILED "The directory could not be created"
fi

echo
echo "OVERALL RESULT:"
TEST_PASSED

