#!/bin/sh

# Script to purge expired Interchange session and tmp files
#
# Before anything else we assure that we run only as the 'interch' user
# because we allow 'find' to follow symlinks (e.g. to handle session
# directories put on a RAM disk or NFS mount), which could be a
# security risk if run as another user.

icuser=interch
myuid=`id -u`
if [ "$myuid" = 0 ]; then
	exec su -c "$0" - $icuser
elif [ "$myuid" -ne "`id -u $icuser`" ]; then
	echo "Aborting Interchange session/tmp file purge" >&2
	echo "Must run as root or user '$icuser', not user '`id -u -n`'" >&2
	exit 1
fi

for i in /var/lib/interchange/*/{session,tmp}
do
	[ -d $i ] || continue
	find $i -type f -follow -mtime +1 | xargs -r rm -f
	find $i -type d -follow -empty -depth -mindepth 1 | xargs -r rmdir --ignore-fail-on-non-empty
done

exit 0
