#! /bin/sh
#
# This script cleans up obsolete session directories.
# It should usually be called by cron.
# Syntax: session-cleaner <session-dir> <threshold>
# where session-dir is the base directory of the session directories,
# and threshold is the latency in minutes.

if [ -z "$2" ]; then
 cat <<1
This is WIMS session file cleaner.

Usage: $0 <session-dir> <threshold>
	session-dir: base directory for sessions
	threshold: time to allow for a session to live. In minutes.

1
exit
fi

if [ ! -d "$1" ]; then
 cat <<1 &>2
$0: Error: session directory $1 does not exist.
1
exit
fi

rm -f $1/lastclean
killtime=`date -d $2' minutes ago' '+%s'`
for s in $1/*
do
 if [ $s != $1/'*' ] && [ -d "$s" ]; then 
  stime=`stat -t $s 2>/dev/null | awk '{print $12}'`
  if [ ! -z "$stime" ]; then
   if [ $stime -lt $killtime ] && [ -d "$s" ]; then
    mv $s $1/kill 2>/dev/null
    rm -Rf $1/kill 2>/dev/null
   fi
  fi
 fi
done

touch $1/lastclean

