#
# 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: SpoolManager.pm 3 2007-10-18 16:20:19Z guillaume $
#

use strict ;
use Test ;

require 'test/A2P/Defaults.pm' ;

BEGIN {
    plan
        tests  => 30 ,
        onfail => sub { exit(1) }
}

$| = 1 ;

our $tie_file ;
our $test_string ;

our ( $SERVICE_TMP, $SHMDIR, $ADVANCED_DEBUGGING, $LOCKID,
    $LOGFILENAME, $DEBUG_IN_FILE, $AFPSPOOL );
our $Progname    = 'TEST-SpoolManager' ;

# Be sure
unlink $AFPSPOOL unless -d $AFPSPOOL ;
mkdir $AFPSPOOL unless -d $AFPSPOOL ;
mkdir $AFPSPOOL.'/.a2p' unless -d $AFPSPOOL.'/.a2p' ;

&info("Loading A2P::SpoolManager library");
ok require A2P::SpoolManager ;

sub update_prefix ; *update_prefix = \&A2P::SpoolManager::update_prefix ;

&info("Update prefix");
# Default for $SPOOL_PREFIX is "TRANS ARCHI" ;
ok join('-',&update_prefix("TRANS ARCHI")), "TRANS-ARCHI" ; #2

sub prefix ; *prefix = \&A2P::SpoolManager::prefix ;

&info("Check file match a prefix");
ok &prefix("TRANS.TEST"), "TRANS" ;
ok &prefix("TrANs.TEST"), "TrANs" ;
ok &prefix("ARCHI.A"), "ARCHI" ;
ok &prefix("archi."), "" ;
ok &prefix("TEST"), "" ;
ok &prefix(""), "" ;
ok &prefix("none"), "" ;
ok &prefix("none.TEST.none"), "" ; #10


&info("Check get locking");


sub openLockingPrefix ;  *openLockingPrefix = \&A2P::SpoolManager::openLockingPrefix ;
sub lockprefix ;         *lockprefix = \&A2P::SpoolManager::lockprefix ;
sub unlockprefix ;       *unlockprefix = \&A2P::SpoolManager::unlockprefix ;
sub closeLockingPrefix ; *closeLockingPrefix = \&A2P::SpoolManager::closeLockingPrefix ;

&info("Check locking on prefix");
my $hashref = { LockingPrefix => {} };
my $lockfile = $AFPSPOOL . '/.a2p/TRANS.LCK' ;
unlink $lockfile ;
ok ! -e $lockfile ;
ok &openLockingPrefix($hashref), 1 ;
ok -e $lockfile ;
ok exists($hashref->{LockingPrefix}->{TRANS}) ; #14
ok exists($hashref->{LockingPrefix}->{ARCHI}) ;
my $TRANS = $hashref->{LockingPrefix}->{TRANS} ;
my $ARCHI = $hashref->{LockingPrefix}->{ARCHI} ;
ok (defined($TRANS) and defined($ARCHI));
ok ref($TRANS), 'GLOB' ;
ok ref($ARCHI), 'GLOB' ;
ok &lockprefix($hashref,"TRANS") ; #19
my $lck = qx(/usr/sbin/lsof -l $lockfile  2>&1 | tee /tmp/test-locking.log 2>&1) ;
&info("Check lsof result:", $lck, "toward: perl $$ $< [0-9]+wW REG [0-9,]+ 0");
ok $lck && $lck =~ /perl\s+$$\s+$<\s+\d+wW\s+REG\s+[0-9,]+\s+0\s+/ ;
ok &unlockprefix($hashref,"TRANS") , 1 ;
ok exists($hashref->{LockingPrefix}->{TRANS}) ; #22
ok exists($hashref->{LockingPrefix}->{ARCHI}) ;
ok &closeLockingPrefix($hashref), 1 ;
ok ! exists($hashref->{LockingPrefix}->{TRANS}) ;
ok ! exists($hashref->{LockingPrefix}->{ARCHI}) ; #26

&info("Checklocking when lock file has been deleted");
&openLockingPrefix($hashref) ;
unlink $lockfile ;
ok &lockprefix($hashref,"TRANS") ; #27
$lck = qx(/usr/sbin/lsof -l $lockfile  2>&1 | tee /tmp/test-locking.log 2>&1) ;
&info("Check lsof result:", $lck, "toward: perl $$ $< [0-9]+wW REG [0-9,]+ 0");
ok $lck && $lck =~ /perl\s+$$\s+$<\s+\d+wW\s+REG\s+[0-9,]+\s+0\s+/ ;
ok &unlockprefix($hashref,"TRANS") , 1 ; #29
ok &closeLockingPrefix($hashref), 1 ;

1 ;
