#!/bin/sh

FILESPOOL="/var/lib/open-xchange/filespool"
USERGROUP="tomcat:tomcat"
DIRRIGHTS="0770"
FILERIGHTS="0660"

# Method for Solaris because Solaris doesn't have the seq command.
# Bug #1524

xseq () {
   i=$1;
   while [ $i -le $2 ]; do
      #printf "%d\n" $i
      echo "$i"
      i=`expr $i + 1`
   done
}

echo "Setting right user and group. This can take a long time depending on the"
echo "used filesystem. Please be patient."

if [ ! -e "$FILESPOOL" ]
then
   echo "Directory $FILESPOOL is missing. Creating it!"
   MISSINGDIR="true"
   mkdir -m $DIRRIGHTS "$FILESPOOL"
fi

for TOP in `xseq 0 255`
do
   echo -n -e "Finished `expr $TOP \* 100 / 255`% ...\r"

   DIR1=`printf "$FILESPOOL/%02x\n" $TOP`

   if [ -e "$DIR1" ]
   then
      chmod $DIRRIGHTS $DIR1

      for SUB in `xseq 0 255`
      do
         DIRPATH=`printf "$FILESPOOL/%02x/%02x\n" $TOP $SUB`

         if [ -e "$DIRPATH" ]
         then
            #echo "Correcting directory $DIRPATH ..."

            chmod $DIRRIGHTS $DIRPATH

            for FILE in `ls -1 $DIRPATH/[0-9a-f][0-9a-f] 2>/dev/null`
            do
               chmod $FILERIGHTS $FILE
            done
         fi
      done
   fi
done

echo "Setting user and group ..."
chown -R $USERGROUP $FILESPOOL
