#! /usr/bin/perl
# This script will cleanup any stale sessions the Portal system has created.

# portal_session_cleanup - Created by James Pattie, (james@pcxperience.com)
# Copyright (c) 2000-2003, Xperience, Inc. (http://www.pcxperience.com/)
# All rights reserved.  This program is free software; you can redistribute it
# and/or modify it under the same terms as Perl itself.
# 12/11/2000

use Portal::Language;
use Portal::Methods;
use Portal::Data::Config;
use Portal::Who;
use Portal::Log;
use strict;

my $langObj = Portal::Language->new(lang => 'en');
if ($langObj->error)
{
  die "Error instantiating Portal::Language->new()!  Error = " . $langObj->errorMessage() . "\n";
}

my $configObj = undef;
eval { $configObj = Portal::Data::Config->new(langObj => $langObj); };
if ($@)
{
  die "Error instantiating Portal::Data::Config->new()!  Error = $@\n";
}
if ($configObj->error)
{
  die $configObj->errorMessage;
}

my $methods = Portal::Methods->new(langObj => $langObj);
if ($methods->error)
{
  myDie(error => $methods->errorMessage, configObj => $configObj);
}

# make connection to the portal database
my $portalDB = $methods->portalDBSetup(type => "portal", configObj => $configObj);
if ($methods->error)
{
  myDie(error => $methods->errorMessage . "\n", configObj => $configObj);
}
if ($portalDB->error)
{
  myDie(error => $portalDB->errorMessage, configObj => $configObj);
}

# instantiate an instance of the Who module.
my $whoObj = Portal::Who->new(portalDB => $portalDB, langObj => $langObj, configObj => $configObj, methods => $methods);
if ($whoObj->error)
{
  myDie(error => $whoObj->errorMessage, configObj => $configObj);
}

# get a list of all whoEntries that have expired.
my @whoEntries = $whoObj->getWhoEntries(useInterval => 1);
if ($whoObj->error)
{
  myDie(error => $whoObj->errorMessage, configObj => $configObj);
}

foreach my $whoEntry (@whoEntries)
{
  my $result = $whoObj->deleteWhoEntry(session => $whoEntry->{session}, removeSession => 1);
  if ($whoObj->error)
  {
    myDie(error => $whoObj->errorMessage, configObj => $configObj);
  }
  if ($result == -1)
  {
    print "Warning: Session = '$whoEntry->{session}' for uname = '$whoEntry->{uname}', Started on '$whoEntry->{startDate}', Last Updated on '$whoEntry->{refreshDate}' disappeared when we went to remove it!\n";
  }
}
my $logObj = Portal::Log->new(dbHandle => $portalDB, langObj => $langObj);
if ($logObj->error)
{
  die $logObj->errorMessage;
}
my $hostname = `hostname -i`;
chomp $hostname;
$hostname =~ s/ //g;

my $logEntry = Portal::Objects::LogEntry->new(action => 20, ipAddress => $hostname, extraInfo => "Cleaned up " . scalar(@whoEntries) . " entries.", userId => 0, langObj => $langObj);
if ($logEntry->error)
{
  die $logEntry->errorMessage;
}
$logObj->newEntry(logEntry => $logEntry);
if ($logObj->error)
{
  die $logObj->errorMessage;
}

exit 0;

# myDie - Takes lang, encoding, error, configObj
sub myDie
{
  my %args = ( lang => 'en', encoding => 'iso-8859-1', error => "", configObj => undef, @_ );
  my $lang = $args{lang};
  my $encoding = $args{encoding};
  my $message = $args{error};
  my $configObj = $args{configObj};
  my $dateStamp = $methods->getCurrentDate(format => "%F %T");

  print("Error Occurred!\n");
  print($message);
  print("Have the Administrator check the error log ($dateStamp).\n");
  my $logObj = Portal::Log->new(dbHandle => $portalDB, langObj => $langObj);
  if ($logObj->error)
  {
    die $logObj->errorMessage;
  }
  my $hostname = `hostname -i`;
  chomp $hostname;
  $hostname =~ s/ //g;

  my $logEntry = Portal::Objects::LogEntry->new(action => 20, ipAddress => $hostname, extraInfo => $message, userId => 0, langObj => $langObj);
  if ($logEntry->error)
  {
    die $logEntry->errorMessage;
  }
  $logObj->newEntry(logEntry => $logEntry);
  if ($logObj->error)
  {
    die $logObj->errorMessage;
  }
  exit 1;
}
