#!/bin/sh

if [ -f /etc/tumgreyspf/default.conf ] ; then
	GREYLISTEXPIREDAYS=`grep GREYLISTEXPIREDAYS /etc/tumgreyspf/default.conf | cut -d'=' -f2 | awk '{print $1}' | cut -d'.' -f1`
fi

if [ -z "${GREYLISTEXPIREDAYS}" ] ; then
	GREYLISTEXPIREDAYS=10
fi

greylistDir="/var/lib/tumgreyspf/data"

if ! [ -d "${greylistDir}" ] ; then
	# echo "No tumgreyspf data folder"
	exit 0
fi

if [ -z "$(ls ${greylistDir})" ] ; then
	# echo "No data to clean in this run"
	exit 0
fi

#echo -n "Now parsing all Class A in ${greylistDir}:"
# /var/lib/tumgreyspf/data/96/52/161/check_file
for i in ${greylistDir}/* ; do
#	echo -n " "`basename ${i}`
#	if [ "${i}" = "${greylistDir}/lost+found" ] ; then
#		echo "Skipping ${greylistDir}/lost+found"
#	fi
	for j in ${i}/* ; do
		for k in ${j}/* ; do
			find ${k}/ -name check_file -mtime +${GREYLISTEXPIREDAYS} -delete
			rmdir --ignore-fail-on-non-empty ${k}
		done
		rmdir --ignore-fail-on-non-empty ${j}
#		rmdir ${j}
	done
	rmdir  --ignore-fail-on-non-empty ${i}
#	rm ${i}
done
#echo " all done!"
