#!/usr/bin/perl -w

=head1 NAME

octo_replay - Octopussy Replay program

=head1 SYNOPSIS

octo_replay --device <device> --service <service> --begin YYYYMMDDHHMM --end YYYYMMDDHHMM

=head1 DESCRIPTION

octo_replay is the program used by the Octopussy Project 
to replay Logs for some Device / Service
(it gets 'recognized' logs and put it back in Incoming directory)

=cut

use strict;
use warnings;
use Readonly;

use Getopt::Long;
Getopt::Long::Configure('bundling');

use AAT::Utils qw( ARRAY );
use Octopussy;
use Octopussy::Device;
use Octopussy::FS;
use Octopussy::Logs;
use Octopussy::Storage;

Readonly my $PROG_NAME    => 'octo_replay';
Readonly my $PROG_VERSION => Octopussy::Version();

my $help;
my ($opt_device, $opt_service, $opt_begin, $opt_end) =
    (undef, undef, undef, undef);
my $file_pid = undef;
my $cache    = undef;

=head1 FUNCTIONS

=head2 Help()

Prints Help

=cut

sub Help
{
    my $help_str = <<"EOF";

$PROG_NAME (version $PROG_VERSION)

 Usage: $PROG_NAME --device <device> --service <service> 
        --begin YYYYMMDDHHMM --end YYYYMMDDHHMM

EOF

    print $help_str;
    if (!defined $opt_device)
    {
        print ' ' . Octopussy::Device::String_List(undef) . "\n";
    }
    elsif (!defined $opt_service)
    {
        print ' '
            . Octopussy::Device::String_Services(ARRAY($opt_device)) . "\n";
    }
    print "\n";

    exit;
}

#
# MAIN
#
exit if (!Octopussy::Valid_User($PROG_NAME));

my $status = GetOptions(
    'h'         => \$help,
    'help'      => \$help,
    'device=s'  => \$opt_device,
    'service=s' => \$opt_service,
    'begin=s'   => \$opt_begin,
    'end=s'     => \$opt_end,
);

Help()
    if ((!$status)
    || ($help)
    || (!defined $opt_device)
    || (!defined $opt_service)
    || (!defined $opt_begin)
    || (!defined $opt_end));

my $dir_incoming = Octopussy::Storage::Directory_Incoming($opt_device)
    . "$opt_device/Incoming/";
my ($files, $total) =
    Octopussy::Logs::Get_TimePeriod_Files($opt_device, $opt_service, $opt_begin,
    $opt_end);
foreach my $min (sort keys %{$files})
{
    my @logs = ();
    foreach my $f (@{$files->{$min}})
    {
		my $cat = ($f =~ /.+\.gz$/ ? 'zcat' : 'cat');
		if (defined open my $FILE, '-|', "$cat \"$f\"")
        {
            while (<$FILE>) { push @logs, $_; }
            close $FILE;
            unlink $f;
        }
    }

    if ($min =~ /^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})$/)
    {
        my $incoming = $dir_incoming . "$1/$2/$3/msg_${4}h${5}_00.log";
        if (defined open my $INCOMING, '>', $incoming)
        {
            foreach my $l (sort @logs) { print {$INCOMING} $l; }
            close $INCOMING;
            Octopussy::FS::Chown($incoming);
        }
    }
}

=head1 AUTHOR

Sebastien Thebert <octo.devel@gmail.com>

=head1 SEE ALSO

octo_dispatcher, octo_extractor, octo_parser, octo_uparser, octo_reporter, octo_scheduler

=cut
