#
# Copyright (c) 2004-2007 - Consultas, PKG.fr
#
# This file is part of A2P.
#
# A2P 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.
#
# A2P 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 A2P; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# $Id: Archiver.pm 3 2007-10-18 16:20:19Z guillaume $
#

package A2P::Archiver ;

# Derived class from Thread.pm
use base qw(A2P::Thread);

use strict ;
use integer ;
use Socket ':crlf' ;
use A2P::Globals ;
use A2P::Syslog ;
use A2P::archivage ;
use A2P::Com qw( IsCom comJOB );
use A2P::JobStatus 'a2pjobstate' ;

BEGIN {
    our $VERSION = sprintf "%s", q$Rev: 397 $ =~ /(\d[0-9.]+)\s+/ ;
}
our $VERSION ;

################################################################################
##          Archivage thread object                                           ##
################################################################################
sub Do {
    my $self = shift ;
    my $ref  = shift ;

    my $Ret = 0 ;

    my @Job = &IsCom( comJOB , $$ref );

    if ( @Job == 2 ) {
        my ( $Job , $archreq ) = @Job ;

        &Debug("Received Arch request for $Job");

        $archreq =~ s/\.arch$// ; # Squeezes arch extension

        # Keep internal stats
        &UPSTAT('ARCH_REQ');

        # Archivage main sub-routine connector
        if ( $ARCH_ENABLED < 100 ) {
            $Ret = &ArchivageMain( $archreq );

        } else {
            # Archivage must be simulated
            $Ret = $ARCH_ENABLED == 100 ? 0 :
                ( rand($ARCH_ENABLED) > 99 ? 1 : 0 ) ;
        }

        # Keep status and log error
        my $status_step = 'o' ;
        my $status = { JID => $Job , STATUS => 'ARCHIVED' } ;

        if ($Ret) {
            $self->ThisError("Archivage of '$archreq.pdf' returned with $Ret" .
                ( ($!) ? ": $! $? $@" : "" ));
            $status->{ABTERM} = 'Not archived';
            $status->{STATUS} = 'KO' ;
            $status_step = 'A' ;
        }

        my ( $JobId ) = $Job =~ /^(.*)-\d+$/ ;
        &a2pjobstate( $JobId || $Job , 10, $status_step, $status )
            or &Info("Can't set job status '$status_step'");

        # Keep internal stats
        $Ret ? &UPSTAT('BAD_ARCH_REQ') : &UPSTAT('GOOD_ARCH_REQ');

        $self->Return( $Job , $Ret ? $Ret : DONE );
        $self->AnswerDone( $Job );

    } else {
        # Keep internal stats
        &UPSTAT('BAD_ARCH_FORMAT');

        $self->ThisError("Can't understand request '$$ref'");

        $Ret = 9 ;

        if (@Job) {
            # Return exec status even for bad format to not break job processing
            $self->Return( $Job[0] , $Ret );
            $self->AnswerDone( $Job[0] );
        }
    }

    return $Ret ? 0 : 1 ;
}

sub ThreadInit {
    undef $SOCK ;
}

&Debug("Module " . __PACKAGE__ . " v$VERSION loaded");

1;
