#!/usr/local/bin/perl

# $Id: maintenance,v 1.2 2004/01/31 06:20:40 mig Exp $
######################################
# Comas - Conference Management System
######################################
# Copyright 2003 CONSOL
# Congreso Nacional de Software Libre (http://www.consol.org.mx/)
#   Gunnar Wolf <gwolf@gwolf.cx>
#   Manuel Rabade <mig@mig-29.net>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
######################################

######################################
# Script: maintenance
# various maintenance tasks to comas
######################################
# Depends on:
#
# Config::Simple - simple configuration file class
# Comas::DB - Handles the database connectivity for a Comas module
# Apache::Session::Postgres - An implementation of Apache::Session

use lib qw(../);

use strict;
use warnings;

use Config::Simple;
use Comas::DB;
use Apache::Session::Postgres;

=head1 NAME

maintenance - various maintenance tasks to comas

=head1 SYNOPSIS

./maintenance /path/to/comas/config/file instance

=head1 DESCRIPTION

This script should be run periodicaly to perform some maintenance tasks on comas
databases.

Currently it only delete the expired sessions of the comas database.

=head1 REQUIRES

Apache::Session
Config::Simple
Comas::DB

=head1 EXPORTS

Nothing

=head1 SEE ALSO

Nothing.

=head1 AUTHOR

Gunnar Wolf, gwolf@gwolf.cx
Manuel Rabade, mig@mig-29.net

Comas has been developed for CONSOL, Congreso Nacional de Software Libre,
http://www.consol.org.mx/

=head1 COPYRIGHT

Copyright 2003 Gunnar Wolf and Manuel Rabade

This library is free software, you can redistribute it and/or modify it
under the terms of the GPL version 2 or later.
    
=cut

my ($cfg, $valid_cfg, %db_cfg, %db_adm_cfg, $db, $config_file, $instance, $sth,
    @ids);

unless ($#ARGV == 1) {
    die 'Bad numer of parameters, check the POD';
}

($config_file, $instance) = @ARGV;

# We load the configuration.
$cfg = Config::Simple->new($config_file);

unless (defined $cfg) {
    die 'Could not open configuration file ', $config_file;
}

# The valid configuration keys.
$valid_cfg = { dbname => 1, host => 1, port => 1, dbuser => 1,
               dbpasswd => 1, adm_dbuser => 1, adm_dbpasswd => 1 };

# Here we prase the configuration file
foreach my $cfg_key (keys %{$cfg->vars}) {
    my $temp_key;
    
    # We separate the keys of the file (they are in the notation block.key).
    my ($inst, $key) = split(/\./, $cfg_key);
    
    # If the key dont belong to our instance we drop it.
    next if ($inst ne $instance);
    
    unless ($valid_cfg->{$key}) {
        die 'Invlid configuration key: ', $key, ', at section: ', $inst;
    }
    
    if ($key eq 'dbuser' || $key eq 'dbpasswd') {
        $db_cfg{"-$key"} = $cfg->param("$instance.$key");
    } elsif ($key eq 'adm_dbuser' || $key eq 'adm_dbpasswd') {
        ($temp_key = $key) =~ s/^adm_//;
        $db_adm_cfg{"-$temp_key"} = $cfg->param("$instance.$key");
    } else {
        $db_cfg{"-$key"} = $cfg->param("$instance.$key");
        $db_adm_cfg{"-$key"} = $cfg->param("$instance.$key");
    }
}

# Comas::DB Objetcts/
$db = Comas::DB->new(%db_cfg);

if (! defined $db) {
    die 'Could not connect to the database, check your configuration file';
}

unless ($sth = $db->prepare('SELECT id FROM sessions') and
        $sth->execute) {
    die 'Could not get session ids';
}

@ids = map {$_->[0]} @{$sth->fetchall_arrayref};

foreach my $id (@ids) {
    my %session;
    tie %session, 'Apache::Session::Postgres', $id,
    {Handle => $db->{-dbh}, Commit => 0};
    if (! defined $session{last_visit}) {
        tied(%session)->delete;
    } elsif (defined $session{last_visit} and 
             ($session{last_visit} + 600) < time) {
        tied(%session)->delete;
    }
}

# $Log: maintenance,v $
# Revision 1.2  2004/01/31 06:20:40  mig
# - Mas documentacion
#

1;
