#!/bin/sh -f
#
# Usage: srm-can-access (get|put) <path> [<user>]

path=$1
path=`echo $1| sed "s/^\/*/\//"` 
stat=`\
rfstat cchpssd0.in2p3.fr:${path} | awk \
  'BEGIN { RS = "" ; FS = "\n" } \
   {  \
      protection=$3;\
      sub(/.*: /,"",protection);\
      directory=(substr(protection,1,1)!="-");\
      user_read=(substr(protection,2,1)!="-");\
      user_write=(substr(protection,3,1)!="-");\
      user_exec=(substr(protection,4,1)!="-");\
      group_read=(substr(protection,5,1)!="-");\
      group_write=(substr(protection,6,1)!="-");\
      group_exec=(substr(protection,7,1)!="-");\
      other_read=(substr(protection,8,1)!="-");\
      other_write=(substr(protection,9,1)!="-");\
      other_exec=(substr(protection,10,1)!="-");\
      file_user=$5 ;\
      sub(/.*\(/,"",file_user);\
      sub(/\).*/,"",file_user);\
      file_group=$6;\
      sub(/.*\(/,"",file_group);\
      sub(/\).*/,"",file_group);\
      file_size=$7;\
      sub(/.*: /,"",file_size);\
      print directory,user_read,user_write,user_exec,\
            group_read,group_write,group_exec,other_read,\
            other_write,other_exec,file_user,file_group,\
            file_size \
   }' \
`
RC=$?
if [ "$RC" != "0" ]
then
  echo file does not exist >&2
  exit 1
fi
isdirectory=`echo $stat | awk '{ print $1 }'`
user_can_read=`echo $stat | awk '{ print $2 }'`
user_can_write=`echo $stat | awk '{ print $3 }'`
user_can_execute=`echo $stat | awk '{ print $4 }'`
group_can_read=`echo $stat | awk '{ print $5 }'`
group_can_write=`echo $stat | awk '{ print $6 }'`
group_can_execute=`echo $stat | awk '{ print $7 }'`
other_can_read=`echo $stat | awk '{ print $8 }'`
other_can_write=`echo $stat | awk '{ print $9 }'`
other_can_execute=`echo $stat | awk '{ print $10 }'`
file_user=`echo $stat | awk '{ print $11 }'`
file_group=`echo $stat | awk '{ print $12 }'`
file_size=`echo $stat | awk '{ print $13 }'`
if [ "$isdirectory" = "1" ]
then
  # file is directory
  echo -1
  exit 0
fi
echo $file_size
