#!/bin/sh

CONFIG=/etc/zarafa/server.cfg
test -f ${CONFIG} || exit 0

LOGPATH=$(grep -w client_update_log_path ${CONFIG} | grep -v \# | cut -d= -f2 | sed -e 's/[[:blank:]\r]//g')
if test -z "${LOGPATH}"; then
	LOGPATH=/var/db/zarafa/client
fi
if ! test -r "${LOGPATH}"; then
	exit 0
fi
LOGPATH=$(readlink -f ${LOGPATH})
if test -z "${LOGPATH}"; then
	exit 0
fi
PATHDEPTH=$(echo -en ${LOGPATH} | sed -e 's_[^/]__g' | wc -c)
if test -n ${LOGPATH} -a ${PATHDEPTH} -gt 2 -a -d ${LOGPATH}; then
	# remove all files older than 14 days
	find ${LOGPATH} -mindepth 2 -maxdepth 2 -type f -ctime +14 -exec rm {} \;
	# remove all empty directories
	find ${LOGPATH} -maxdepth 1 -type d -exec rmdir {} 2>/dev/null \;
fi
