#!/usr/bin/bash
#
# This script expects pvfs2-xattr and pvfs2-stat to be on your search path.
#

function usage {
echo
    echo "pvfs2-getmattr [-c] [-m] [-h] -f file"
    echo "  -c : Retrieve the number of mirror copies"
    echo "  -m : Retrieve the mirroring mode"
    echo "  -h : Display this message"
    echo
    echo "Retrieve copies and mode when none specified.  File " \
         "is required."
    exit
}

if [ "$#" -lt 2 ] || [ "$#" -gt 4 ]
then
  usage
fi

XATTR_PATH=`which pvfs2-xattr 2> /dev/null`
if [ $? -eq "1" ]
then
  echo "pvfs2-xattr command not found."
  exit
fi

MODE=0
COPY=0

while [[ $# -gt 0 ]]
do
  case "$1" in
    -c) COPY=1
        shift;;
    -m) MODE=1
        shift;;
    -f) shift
        pvfs2-stat "$1" > /dev/null 2>&1
        if [ "$?" != 0 ]
        then
          usage
        fi
        TARGET="$1"
        shift;;
     *) usage;;
  esac
done

if [ "$MODE" -eq 0 ] && [ "$COPY" -eq 0 ]
then
  MODE=1
  COPY=1
fi

if [ "$COPY" -eq "1" ]
then
     pvfs2-xattr -k user.pvfs2.mirror.copies -t "$TARGET"
fi

if [ "$MODE" -eq "1" ]
then
     pvfs2-xattr -k user.pvfs2.mirror.mode -t "$TARGET"
fi

