#!/bin/sh

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

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 $(seq 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 $(seq 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
