#!/usr/local/bin/perl
#
# Copyright (C) 2003 Alexandre Dulaunoy <adulau@foo.be>
#
# 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.
#
#
# $Id: mqsmanagerd,v 1.2 2003/07/25 10:05:44 adulau Exp adulau $

use Config::General;
use POSIX;
use Data::Dumper;
use RPC::XML::Client;

my $version = "0.0.4";

$| = 1;

if ( -e "../var/mqsmanagerd.pid" ) {
    killMyself(
        "Another mqsmanagerd running or ./var/mqsmanagerd.pid still there...");
}

my %mqsmanagerconfig = ReadingConfiguration("../cfg/mqsmanager.cfg");
my %mqsconfig        = ReadingConfiguration("../cfg/mqs.cfg");
logMessage("Minimalist Queue Services Manager $version - Starting");
goingDaemon();

while (1) {
    logMessage(
"MQS Manager (every $mqsmanagerconfig{general}{cleanuptiming} sec.) started..."
    );
    my $client = new RPC::XML::Client $mqsmanagerconfig{general}{mqsurl};
    foreach my $val ( keys %{ $mqsconfig{queue} } ) {
        if ( $mqsconfig{queue}{$val}{getfromqueue} eq "remove" ) {
            logMessage(
"DeleteMessage from queue $val on $mqsmanagerconfig{general}{mqsurl}"
            );
            my $req = RPC::XML::request->new( 'mqs.DeleteMessage', $mqsmanagerconfig{general}{authstring}, $val );
            my $res = $client->send_request($req);
            my $value = $res->value;
            logMessage(
"$value deleted from queue $val on $mqsmanagerconfig{general}{mqsurl}"
            );
        }
    }
    sleep $mqsmanagerconfig{general}{cleanuptiming};
}

sub logMessage {

    my $message = shift;
    my $time    = POSIX::strftime( "%Y-%m-%d %H:%M:%S%z", localtime );
    my $logpath = $mqsconfig{general}{path} . "/var/mqsmanagerd.log";
    open( LOG, ">>$logpath" );
    print LOG "$time" . " [" . $$ . "] " . "$message\n";
    close(LOG);
    return;
}

sub goingDaemon {
    my $pid;
    $pid = fork;
    exit if $pid;
    die "goingDaemon : Couldn't fork : $!" unless defined($pid);
    POSIX::setsid() or die "goingDaemon : Can't start a new session: $!";
    $SIG{HUP}  = \&restartDaemon;
    $SIG{INT}  = \&stopDaemon;
    $SIG{TERM} = \&stopDaemon;
    logMessage("Going daemon with PID $$");
    open( PIDFILE, ">../var/mqsmanagerd.pid" );
    print PIDFILE $$;
    close(PIDFILE);
    return $$;
}

sub restartDaemon {

    logMessage("Restarting Daemon...");
    logMessage("Reading configuration...");
    %mqsconfig        = ReadingConfiguration("../cfg/mqs.cfg");
    %mqsmanagerconfig = ReadingConfiguration("../cfg/mqsmanager.cfg");
}

sub stopDaemon {

    logMessage("Stopping Daemon...");
    unlink "../var/mqsmanagerd.pid";
    die;
}

sub killMyself {
    my $message = shift;
    logMessage($message);
    die "$message";
}

sub ReadingConfiguration {
    my $configfile = shift;
    my $conf       = new Config::General($configfile);
    my %config     = $conf->getall;
    return %config;
}

