#!/bin/bash

# Run via cron for terminate obsoleted suspended sessions

# Under GPL

CRITICAL_TIME=7200

# Read the config file
. $(PATH=$(cd $(dirname $0) && pwd):$PATH which nxloadconfig) --


SourceIfNotEmpty()
{
	local f="$1"
	shift
	[ -s "$f" ] && . "$f" "$@"
}

# Obsoletes, for compatibility
SourceIfNotEmpty /etc/sysconfig/rx-etersoft

[ -n "$SESSION_TTL" ] || exit

nxdir="/var/lib/rx-etersoft/db/running"
nxserver="/usr/bin/nxserver"

if [ -d "$nxdir" -a "$SESSION_TTL" -gt 0 ] ; then
  for f in `ls $nxdir` ; do
    sessiontype=`cat $nxdir/$f | grep status | cut -d= -f2`
    user=`cat $nxdir/$f | grep userName | cut -d= -f2`
    sessiontime=`cat $nxdir/$f | grep creationTime | cut -d= -f2`
    sessionid=`cat $nxdir/$f | grep sessionId | cut -d= -f2`
    criticaltime=$(expr `date +%s` - $SESSION_TTL)
    sessioncriticaltime=$(expr $CRITICAL_TIME + $SESSION_TTL)
    criticaltime2=$(expr `date +%s` - $sessioncriticaltime)

    if [ -z "$sessiontime" ] ; then
        echo "Drop empty $f session file"
        rm -fv $nxdir/$f
        continue
    fi

    # terminate obsoleted sessions in usual way
    if [ $sessiontime -lt $criticaltime ] ; then
        if [ "$sessiontype" = "Suspended" ] ; then
            $nxserver --terminate $sessionid
        fi
    fi

    # force terminate more than obsoleted sessions with force
    if [ $sessiontime -lt $criticaltime2 ] ; then
        if [ "$sessiontype" = "Suspended" ] ; then
            $nxserver --force-terminate $sessionid
        fi
    fi

  done
fi
