#!/usr/bin/perl -w

##########################################################################################
#
#  srmv2Suite[.pl]
#
#  A Perl script to test the srmV2 interface to the DPM
#
#  Author: Gilbert Grosdidier, LAL (Universite Paris-Sud, Orsay)
#  Creation Date:   3/11/2004
#  Last rev:       15/11/2006 (GG) + 16/11/2007
#
##########################################################################################

###
### Copyright (C) 2004-2006 by CERN/IT/GD/CT & CNRS/IN2P3/LAL
### All rights reserved
###

# $Id: srmv2Suite,v 1.37 2007/11/17 02:20:58 grodid Exp $

use strict;

##09use File::CounterFile;

######################
## The following functions are tested or used, in raw:
## (dpm-XXXX or dpns-ZZZZ are DPM or DPNS socket functions,
## while srm2_YYYY are specific to the srmV2 suite)

# $operation = "srm2_testPut";
# $operation = "rfcp";
# $operation = "srm2_testPutDone";
# $operation = "srm2_testGet";
# $operation = "srm2_testGetRequestSummary";
# $operation = "dpm-getreqstatus";
# $operation = "srm2_testExtendFileLifeTime";
# $operation = "dpm-getreqstatus";
# $operation = "srm2_testExtendFileLifeTime";
# $operation = "dpm-getreqstatus";
# $operation = "srm2_testReleaseFiles";
# $operation = "dpm-getreqstatus";
# $operation = "srm2_testLs";
# $operation = "srm2_testMkdir";
# $operation = "srm2_testMv";
# $operation = "srm2_testLs";
# $operation = "srm2_testRm";
# $operation = "srm2_testLs";
# $operation = "srm2_testMv";
# $operation = "srm2_testLs";
# $operation = "srm2_testRmdir";
# $operation = "srm2_testLs";
# $operation = "srm2_testRm";
# $operation = "srm2_testLs";
# $operation = "srm2_testRmdir";
# $operation = "srm2_testRmdir";
# $operation = "srm2_testSetPermission";
# $operation = "dpns-getacl";
# $operation = "srm2_testSetPermission";
# $operation = "dpns-getacl";
# $operation = "srm2_testSetPermission";
# $operation = "dpns-getacl";
# $operation = "srm2_testSetPermission";
# $operation = "dpns-getacl";
# XcuteComm("srm2_testReassignToUser",
# XcuteComm("srm2_testReserveSpace",
# XcuteComm("srm2_testReleaseSpace",
# XcuteComm("srm2_testUpdateSpace", 
# XcuteComm("srm2_testCompactSpace", 
# XcuteComm("srm2_testGetSpaceMetaData", 
# XcuteComm("srm2_testChangeFileStorageType",
# XcuteComm("srm2_testGetSpaceToken", 
# XcuteComm("srm2_testRemoveFiles",
# XcuteComm("srm2_testAbortFiles",
# XcuteComm("srm2_testSuspendRequest",
# XcuteComm("srm2_testResumeRequest",
# XcuteComm("srm2_testCheckPermission",
# $operation = "srm2_testGetRequestID";
# $operation = "dpm-getreqid --format=dpmsurl:80,dpmrtyp,dpmpint,dpmfsiz";
# $operation = "OVERALL SUITE";

## $operation = "srm2_testPut";
## $operation = "rfcp";
## $operation = "srm2_testPutDone";
## $operation = "srm2_testGet";
##         XcuteComm("srm2_testGetRequestSummary",
##         XcuteComm("dpm-getreqstatus",
##         XcuteComm("srm2_testExtendFileLifeTime",
##         XcuteComm("dpm-getreqstatus",
##         XcuteComm("srm2_testReleaseFiles",
##         XcuteComm("dpm-getreqstatus",
##     XcuteComm("srm2_testLs",
##     XcuteComm("srm2_testMkdir",
## 	XcuteComm("srm2_testMv",
##     XcuteComm("srm2_testLs",
##     XcuteComm("srm2_testRm",
##     XcuteComm("srm2_testLs",
## 	XcuteComm("srm2_testMv",
##     XcuteComm("srm2_testLs",
##     XcuteComm("srm2_testRmdir",
##     XcuteComm("srm2_testLs",
##     XcuteComm("srm2_testRm",
##     XcuteComm("srm2_testRmdir",
##     XcuteComm("srm2_testRmdir",
## $operation = "srm2_testSetPermission";
## $operation = "dpns-getacl";
## XcuteComm("srm2_testReassignToUser",
## XcuteComm("srm2_testReserveSpace",
## XcuteComm("srm2_testReleaseSpace",
## XcuteComm("srm2_testUpdateSpace", 
## XcuteComm("srm2_testCompactSpace", 
## XcuteComm("srm2_testGetSpaceMetaData", 
## XcuteComm("srm2_testChangeFileStorageType",
## XcuteComm("srm2_testGetSpaceToken", 
## XcuteComm("srm2_testRemoveFiles",
## XcuteComm("srm2_testAbortFiles",
## XcuteComm("srm2_testSuspendRequest",
## XcuteComm("srm2_testResumeRequest",
## XcuteComm("srm2_testCheckPermission",
## $operation = "srm2_testGetRequestID";
## $operation = "dpm-getreqid --format=dpmsurl:80,dpmrtyp,dpmpint,dpmfsiz,dpmmode,dpmstatus,dpmfstat";
## $operation = "OVERALL SUITE";


## Warning: some of them do not belong to the srmv2 set.
######################

my %ns1__TStatusCode = (
			SRM_SUCCESS => 0, 
			SRM_FAILURE => 1, 
			SRM_AUTHENTICATION_FAILURE => 2, 
			SRM_UNAUATHORIZED_ACCESS => 3, 
			SRM_INVALID_REQUEST => 4, 
			SRM_INVALID_PATH => 5, 
			SRM_FILE_LIFETIME_EXPIRED => 6, 
			SRM_SPACE_LIFETIME_EXPIRED => 7, 
			SRM_EXCEED_ALLOCATION => 8, 
			SRM_NO_USER_SPACE => 9, 
			SRM_NO_FREE_SPACE => 10, 
			SRM_DUPLICATION_ERROR => 11, 
			SRM_NON_EMPTY_DIRECTORY => 12, 
			SRM_TOO_MANY_RESULTS => 13, 
			SRM_INTERNAL_ERROR => 14, 
			SRM_FATAL_INTERNAL_ERROR => 15, 
			SRM_NOT_SUPPORTED => 16, 
			SRM_REQUEST_QUEUED => 17, 
			SRM_REQUEST_INPROGRESS => 18, 
			SRM_REQUEST_SUSPENDED => 19, 
			SRM_ABORTED => 20, 
			SRM_RELEASED => 21, 
			SRM_FILE_PINNED => 22, 
			SRM_FILE_IN_CACHE => 23, 
			SRM_SPACE_AVAILABLE => 24, 
			SRM_LOWER_SPACE_GRANTED => 25, 
			SRM_DONE => 26, 
			SRM_CUSTOM_STATUS => 27
		       );
my %RStatusCode = reverse %ns1__TStatusCode;
##TESTprint " TT: ", $RStatusCode{24}, "\n";

my $target  = shift;
my $tstNAME = shift;
my $sfileH  = shift;
my $subTree = shift;

my $pxGRP;
my $pxDN;

my $Counter;
my $value; 
my $valinc;
my $short;
my $sfile;
my $xCount = 0;
my $usage;
my $dateST;
chomp(my $dday = `date '+%j'`);
my $otStart = time();
if ( ! $sfileH ) {
    require File::CounterFile;
    $Counter = File::CounterFile->new("./CounterFile", 1000+$dday);
    $value = $Counter->value();
    $valinc = $Counter->inc();
    $sfile = "fil$valinc";
    $sfileH = $sfile;
    print " Counter Start Value: $sfile \n";
    $sfile = "${sfileH}A0";
#    exit 0;
} else {
    print " Counter Start Value: $sfileH \n";
    $sfile = "${sfileH}A0";
}
#my $stat26 = "26";
my $stat26 = "0";  ##  For the new status code values

if ( $ENV{STAT26} ) {
    $stat26 = "$ENV{STAT26}";
}

##19$ENV{DPM_HOST} = "lxb1902";
##19my $target = $ENV{DPM_HOST};

## Run this with something like:
## srmv2Suite lxb1902.cern.ch baud

# my $target  = shift;
# my $tstNAME = shift;
##BAD$target =~ m/(\w*?)\.(.*)/;
$target =~ m/([-\w]*?)\.(.*)/;
my $mach = $1;
my $site_domain = $2;
chomp(my $fullname = `hostname -f`);
if ( ! $target || ! $tstNAME || ! $site_domain ) {
    print STDERR " Wrong ARGS ! \n";
    print STDERR " Usage: $0 Target_FullNodeName UserName [InitFileName]\n";
    exit 1;
} else {
    print STDOUT " The target Host is:  $target \n";
    print STDOUT " The target Domain is: $site_domain \n";
    print STDOUT " The current Host is: $fullname \n";
}
my $fmach = "$mach.$site_domain";

# my $RHhost = shift;
# my $SLhost = shift;
# my $tstNAME = shift;
# my $target;
# if ( ( $RHhost eq "-" ) && ( $SLhost eq "-" ) ||
#      ( $RHhost ne "-" ) && ( $SLhost ne "-" ) || ( ! $tstNAME ) ) {
#     print STDERR " Wrong ARGS ! \n";
#     exit 1;
# } elsif ( $SLhost eq "-" ) {
#     $target = $RHhost;
#     print STDOUT " The target Host is a RH73 one: $target \n";
# } else {
#     $target = $SLhost;
#     print STDOUT " The target Host is a SLC3 one: $target \n";
# }

$ENV{DPNS_HOST} = $fmach;
$ENV{DPM_HOST}  = $fmach;
print STDOUT " Target Servers: DPNS: $ENV{DPNS_HOST} DPM, srmV2: $ENV{DPM_HOST} \n";

chomp(my $day = `date '+%b%d'`);
# my $u_token = "gilbert_${day}_v$valinc";
##my $u_token = "gilbert_${day}_A${sfileH}";
my $u_token = "gilbert_${day}_U${sfileH}";
my $s_token = "gilbert_${day}_S${sfileH}";
my $d_token = "gilbert_${day}_D${sfileH}";
my $mkdir_t = "dir_${d_token}";
##19my $endPoint = "https://lxb1901.cern.ch:8444";
my $endPoint = "https://${target}:8444";

my $struct;

my $voteam;
if ( $ENV{VOTEAM} ) {
  $voteam = $ENV{VOTEAM};
} else {
  $voteam = 'dteam';
}

##my $baseDpm = "/dpm/dteam/${tstNAME}";
# my $baseDpm = "/dpm/cern.ch/home/dteam";
##1101my $baseDpm = "/dpm/$site_domain/home/$voteam";

my $baseDteam = "/dpm/$site_domain/home/$voteam";
my $baseDpm = $baseDteam;
if ( $subTree ) {
  $baseDpm = "$baseDteam/$subTree";
}

##19my $baseSrm = "srm://$target.cern.ch:8444/$baseDpm";
my $baseSrm = "srm://${target}:8444/$baseDpm";

my $file = "${baseDpm}/$sfile";
# this for dpm at lxb1901 (GG machine ... - not GSIFTP enabled yet
#SURL=srm://lxb1901.cern.ch:8443//dpm/dteam/grodid/srm_test_suite_file.$$.$RANDOM

## The 'S'calar or 'S'hort stuff
#$file = "srm://$target.cern.ch:8444//dpm/dteam/grodid/srm_test_suite_file.$sfile";
$short = "srmv2_Tfile.$sfile";
$file = "${baseSrm}/$short";
$struct->{$file}->{short} = "$short";

## The Abort'F'iles stuff
my $Fshort = "srmv2_Tfile.${sfile}F";
my $Ffile = "${baseSrm}/$Fshort";
$struct->{$Ffile}->{short} = "$Fshort";
$struct->{$Ffile}->{input} = "/etc/ldap.conf";
$struct->{$Ffile}->{fflag} = 1;
$struct->{$Ffile}->{pin} = 11100000;

## The 'A'rray stuff
# $valinc = $Counter->inc();
# $short = "srmv2_Tfile.fil$valinc";
$short = "srmv2_Tfile.${sfileH}A1";
my @Lfile = ();
##push @Lfile, "/dpm/dteam/grodid/fil$valinc";
#push @Lfile, "srm://$target.cern.ch:8444//dpm/dteam/grodid/srm_test_suite_file.fil$valinc";
push @Lfile, "${baseSrm}/$short";
$struct->{$Lfile[0]}->{short} = "$short";

my $tempDir;
if ( $ENV{TEMPDIR} ) { $tempDir = $ENV{TEMPDIR}; }
else { $tempDir = "/tmp"; }

# $valinc = $Counter->inc();
# $short = "srmv2_Tfile.fil$valinc";
$short = "srmv2_Tfile.${sfileH}A2";
push @Lfile, "${baseSrm}/$short";
$struct->{$Lfile[1]}->{short} = "$short";

# $valinc = $Counter->inc();
# $short = "srmv2_Tfile.fil$valinc";
$short = "srmv2_Tfile.${sfileH}A3";
push @Lfile, "${baseSrm}/$short";
$struct->{$Lfile[2]}->{short} = "$short";

$struct->{$file}->{input} = "/etc/ldap.conf";
$struct->{$Lfile[0]}->{input} = "/etc/termcap";

## Which kind of host are we running on ?

if ( -e "/boot/vmlinux-2.4.20-30.7.legacysmp" ) {
    ## This is RH73
    $struct->{$Lfile[1]}->{input} = "/boot/vmlinux-2.4.20-30.7.legacysmp";
    $struct->{$Lfile[2]}->{input} = "/boot/initrd-2.4.20-30.7.legacysmp.img";
    print STDOUT " The Current Host is a RH73 one: $fullname \n";
} elsif ( -e "/boot/vmlinux-2.4.21-20.EL.cernsmp" ) {
    ## This is SLC3
##19if ( $ENV{HOST} eq "lxb1902" ) {
##21chomp(my $fullname = `hostname -f`);
##21if ( "$fullname" eq "$SLhost" ) {
    $struct->{$Lfile[1]}->{input} = "/boot/vmlinux-2.4.21-20.EL.cernsmp";
    $struct->{$Lfile[2]}->{input} = "/boot/initrd-2.4.21-20.EL.cernsmp.img";
##19    $endPoint = "https://lxb1902.cern.ch:8444";
##21    $endPoint = "https://${target}:8444";
    print STDOUT " The Current Host is a SLC3 one: $fullname \n";
} else {
    print STDOUT " Host type unidentified: $fullname \n";
    ##exit 1;
    my @bList = `ls -dl /boot/* | sort -rn -k 5,5`;
    foreach ( @bList ) {
	my @m = split /\s+/;
	if ( ! $struct->{$Lfile[1]}->{input} ) {
	    $struct->{$Lfile[1]}->{input} = $m[8];
	} elsif ( $m[4]>100000 && $m[4]<900000 ) {
	    $struct->{$Lfile[2]}->{input} = $m[8];
	    last;
	}
    }
}
foreach my $kfile ( sort keys %{$struct} ) {
    my $rls = `ls -l $struct->{$kfile}->{input}`;
    print STDOUT " File: $kfile Orig: $struct->{$kfile}->{input} \n$rls";
}

## Path setup ... one must start it from test/srmv2 for the time being ...
    $ENV{PATH} = "../../rfio:$ENV{PATH}";
    $ENV{PATH} = "$ENV{PATH}:../../socket/dpmcli";
    $ENV{PATH} = "$ENV{PATH}:../../ns";
    $ENV{PATH} = "$ENV{PATH}:../../dpm";
    $ENV{PATH} = "$ENV{PATH}:../../test/srmv1";
    $ENV{PATH} = "$ENV{PATH}:../../test/srmv2";

$struct->{$file}->{fflag} = 1;
$struct->{$Lfile[0]}->{fflag} = 2;
$struct->{$Lfile[1]}->{fflag} = 2;
$struct->{$Lfile[2]}->{fflag} = 2;

$struct->{$file}->{pin} = 111000;
$struct->{$Lfile[0]}->{pin} = 222000;
$struct->{$Lfile[1]}->{pin} = 333000;
$struct->{$Lfile[2]}->{pin} = 444000;

## Sizes in kbytes (1024)
my $dss;
($struct->{$file}->{size}, $dss) = split /\s+/, `ls -s $struct->{$file}->{input} | awk '{print \$1}'`;
($struct->{$Ffile}->{size}, $dss) = split /\s+/, `ls -s $struct->{$Ffile}->{input} | awk '{print \$1}'`;
($struct->{$Lfile[0]}->{size}, $dss) = split /\s+/, `ls -s $struct->{$Lfile[0]}->{input} | awk '{print \$1}'`;
($struct->{$Lfile[1]}->{size}, $dss) = split /\s+/, `ls -s $struct->{$Lfile[1]}->{input} | awk '{print \$1}'`;
($struct->{$Lfile[2]}->{size}, $dss) = split /\s+/, `ls -s $struct->{$Lfile[2]}->{input} | awk '{print \$1}'`;
#p    print " VERIF0: $file $struct->{$file}->{input} $struct->{$file}->{size} \n";

##PREVmy @res = `dpmcli/dpm-put --dpmproto rfio --dpmutoken=$u_token --dpmreqsize 100 $file`;
my $fflag = 1;
#test my @res = `~baud/w0/DPNS_MYSQL/ns/nsls -l /dpm/dteam/grodid`;
my $r_token;
my $f_token;
my $g_token;
my $TURL;
my $lTURL;
my $status;
my $tstatus;
my $sTURL;
my $findex;
my $command;
my @res;

my $tStart;
my $tEnd;
my $tElapse;

## Put operation for the Sfile (Scalar)
my $operation = "srm2_testPut";
my $overWrite = 0;
my $fsize;
$xCount++;
$fsize = $struct->{$file}->{size}*1024;
$command = "$operation $u_token 1 $overWrite $file 1000 2 $fsize";
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
$tStart = time();
@res = `$command 2>&1`;
$tEnd = time();
$tElapse = $tEnd - $tStart;
foreach ( @res ) {
    print "L1: $_";
    my @m = split;
    if ( /r_token / ) { $r_token = $m[3]; }
#    if ( /TURL =/ ) { $lTURL = $_; $tstatus = $m[2]; $TURL = $m[5]; }
    if ( /state\[(\d+?)\]/ ) { 
	$findex = $1;
	$lTURL = $_; 
	chop($m[2]);
	$tstatus = $m[2]; 
	##NEW$tstatus = $RStatusCode{$m[2]}; 
	$TURL = $m[5]; 
	##PREV$TURL =~ m[(.*?)://(.*)];
	$TURL =~ m[(.*?)://.*?/(.*)];
	$sTURL = $2;
	my $lfile;
	if ( $fflag == 1 ) {
	    $lfile = $file;
	} else {
	    $lfile = $Lfile[$findex];
	}
	$struct->{$lfile}->{TURL} = $TURL;
	$struct->{$lfile}->{sTURL} = $sTURL;
##	$struct->{$lfile}->{TURL} = $TURL;
	$struct->{$lfile}->{r_token} = $r_token;
    }
    ##PREVif ( /request state/ ) { $status = ( "Done" eq $m[2] ) ? "[OK]" : "[FAILED]" ; }
#   if ( /request state/ ) { $status = ( "26" eq $m[2] ) ? "[OK]" : "[FAILED]" ; }
#   if ( /request state/ ) { $status = ( "0" eq $m[2] ) ? "[OK]" : "[FAILED]" ; }
    if ( /request state/ ) { $status = ( "$stat26" eq $m[2] ) ? "[OK]" : "[FAILED]" ; }
}
if ( $status ne "[OK]" || $tstatus ne "22" ) { 
    print " Put FAILED: $lTURL >$status< >$tstatus<\n";
    ##20 exit 1; 
}
##20 else { print " Operation: $operation = $status Duration: $tElapse \n"; }
&printAndCExit();

##$TURL =~ m[(.*?)://(.*)];
##my $sTURL = $2;

## Put operation for the Lfile (Array)
undef $status;
##PREV@res = `dpmcli/dpm-put --dpmutoken=$u_token --dpmproto rfio --dpmreqsize 100 @Lfile`;
##@res = `srm2_testput @Lfile`;
$xCount++;
my $lifet = 1000;
my $nbfiles = scalar(@Lfile);
$command = "$operation $u_token $nbfiles $overWrite";
foreach my $kfile ( @Lfile ) {
    $lifet += 1000;
    my $fsize = $struct->{$kfile}->{size}*1024;
    if ( $lifet == 4000 ) {
        $command .= " $kfile $lifet 2 $fsize";
    } else {
	$command .= " $kfile $lifet 0 $fsize";
    }
#p    print " VERIF: $kfile $struct->{$kfile}->{size} \n";
}
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
$tStart = time();
@res = `$command 2>&1`;
$tEnd = time();
$tElapse = $tEnd - $tStart;
$fflag = 2;
foreach ( @res ) {
    print "L3: $_";
    my @m = split;
    if ( /r_token / ) { $r_token = $m[3]; }
#    if ( /TURL =/ ) { $lTURL = $_; $tstatus = $m[2]; $TURL = $m[5]; }
    if ( /state\[(\d+?)\]/ ) { 
	$findex = $1;
	$lTURL = $_; 
	chop($m[2]);
	$tstatus = $m[2]; 
	##NEW$tstatus = $RStatusCode{$m[2]}; 
	$TURL = $m[5]; 
	##PREV$TURL =~ m[(.*?)://(.*)];
	$TURL =~ m[(.*?)://.*?/(.*)];
	$sTURL = $2;
	my $lfile;
	if ( $fflag == 1 ) {
	    $lfile = $file;
	} else {
	    $lfile = $Lfile[$findex];
	}
	$struct->{$lfile}->{TURL} = $TURL;
	$struct->{$lfile}->{sTURL} = $sTURL;
##	$struct->{$lfile}->{TURL} = $TURL;
	$struct->{$lfile}->{r_token} = $r_token;
    }
    ##PREVif ( /request state/ ) { $status = ( "Done" eq $m[2] ) ? "[OK]" : "[FAILED]" ; }
#   if ( /request state/ ) { $status = ( "26" eq $m[2] ) ? "[OK]" : "[FAILED]" ; }
#   if ( /request state/ ) { $status = ( "0" eq $m[2] ) ? "[OK]" : "[FAILED]" ; }
    if ( /request state/ ) { $status = ( "$stat26" eq $m[2] ) ? "[OK]" : "[FAILED]" ; }
}
##PREVif ( $status ne "[OK]" || $tstatus ne "Ready," ) { 
if ( $status ne "[OK]" || $tstatus ne "22" ) { 
    print " Put FAILED: $lTURL >$status< >$tstatus<\n";
    ##20 exit 1; 
}
##20 else { print " Operation: $operation = $status Duration: $tElapse \n"; }
&printAndCExit();

## Put operation for the $Ffile
$fsize = $struct->{$Ffile}->{size}*1024;
XcuteComm("srm2_testPut",
	  "$u_token 1 $overWrite $Ffile 1000 2 $fsize",
	  "FFI", "$stat26");
#	  "FFI", "26");
#	  "FFI", "0");
$struct->{$Ffile}->{TURL} = XtractVal("TURL =");
$struct->{$Ffile}->{r_token} = XtractVal("soap_call_ns1__srmPrepareToPut returned r_token");

##exit 0;  ##  TESTS V/P
## End of all put operations (the vector of files is in "keys %{$struct}")

system("which rfcp");

## Overall (transfer + putdone) operations for all files altogether
foreach my $kfile ( sort keys %{$struct} ) {

## Transfer step (uses rfcp) ...
undef $status;
$xCount++;
$operation = "rfcp";
@res = ();
#@res = `rfcp /etc/ldap.conf $sTURL`;
#@res = `rfcp /etc/termcap $sTURL`;
##PREV$command = "$operation $struct->{$kfile}->{input} $struct->{$kfile}->{sTURL}";
my $lTURL = XtractGoodTurl($struct->{$kfile}->{TURL});
$command = "$operation $struct->{$kfile}->{input} $lTURL";
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
##PREV@res = `rfcp $struct->{$kfile}->{input} $struct->{$kfile}->{sTURL} 2>&1`;
$tStart = time();
@res = `$command 2>&1`;
$tEnd = time();
$tElapse = $tEnd - $tStart;
my $exitFlag = 0;
foreach ( @res ) {
    print " RFCP_TRACE $_";
    if ( $ENV{RFIO_TRACE} ) {
#p	print " RFIO_TRACE $_";
    }
    if ( /bytes/ ) { $status = "[OK]"; }
    if ( /Command exited/ || /Timed out/ ) { 
	$status = "[FAILED]";
	$exitFlag = 1; 
    }
}
if ( $exitFlag && $ENV{RFIO_TRACE} ) {
    foreach ( @res ) {
	print " RFIO_FAIL $_";
    }
}

&printAndCExit();
###20 if ( $status ne "[OK]" ) { exit 1; }
###20 else { 
###20     if ( $ENV{RFIO_TRACE} ) {
###20 	
###20     }
###20     print " Operation: $operation = $status Duration: $tElapse \n"; 
###20 }

## Putdone step ...
$operation = "srm2_testPutDone";
undef $status;
my $SURL;
@res = ();
$xCount++;
$command = "$operation $struct->{$kfile}->{r_token} $kfile";
##PREVprint " CMD: dpmcli/dpm-putdone --dpmrtoken=$struct->{$kfile}->{r_token} $kfile \n";
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
##PREV@res = `dpmcli/dpm-putdone --dpmrtoken=$struct->{$kfile}->{r_token} $kfile`;
@res = `$command 2>&1`;
foreach ( @res ) {
    print "D: $_";
    my @m = split;
    if ( /SURL =/ ) { $SURL = $m[5]; }
    if ( /request state/ ) { $status = ( "0" eq $m[2] ) ? "[OK]" : "[FAILED]" ; }
}
##20 if ( $status ne "[OK]" ) { exit 1; }
##20 else { print " Operation: $operation = $status Duration: $tElapse \n"; }
&printAndCExit();

if ( $SURL ne $kfile ) { 
    print " Mismatch: $SURL $kfile \n";
    exit 1;
}

my $fTURL;
$TURL = $struct->{$kfile}->{TURL};
undef $status;
$operation = "srm2_testGet";
@res = ();
$xCount++;
$command = "$operation rfio $u_token $kfile";
##PREVprint " CMD: dpmcli/dpm-get --dpmutoken=$u_token --dpmproto:rfio $kfile \n";
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
##PREV@res = `dpmcli/dpm-get --dpmutoken=$u_token --dpmproto:rfio $kfile`;
$tStart = time();
@res = `$command 2>&1`;
$tEnd = time();
$tElapse = $tEnd - $tStart;
foreach ( @res ) {
    print "GF: $_";
    my @m = split;
    if ( /r_token / ) { $f_token = $m[3]; }
    if ( /TURL =/ ) { $fTURL = $m[5]; }
#   if ( /request state/ ) { $status = ( "26" eq $m[2] ) ? "[OK]" : "[FAILED]" ; }
#   if ( /request state/ ) { $status = ( "0" eq $m[2] ) ? "[OK]" : "[FAILED]" ; }
    if ( /request state/ ) { $status = ( "$stat26" eq $m[2] ) ? "[OK]" : "[FAILED]" ; }
}
##20 if ( $status ne "[OK]" ) { exit 1; }
##20 else { print " Operation: $operation = $status Duration: $tElapse \n"; }
&printAndCExit();

if ( $TURL ne $fTURL ) { 
    print " Mismatch: $TURL $fTURL \n";
    exit 1;
} else {
    print " TURL[rfio]: $fTURL \n";
}

my $gTURL;
undef $status;
@res = ();
$xCount++;
$command = "$operation gsiftp $u_token $kfile";
##PREVprint " CMD: dpmcli/dpm-get --dpmutoken=$u_token --dpmproto:gsiftp $kfile \n";
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
##PREV@res = `dpmcli/dpm-get --dpmutoken=$u_token --dpmproto:gsiftp $kfile`;
$tStart = time();
@res = `$command 2>&1`;
$tEnd = time();
$tElapse = $tEnd - $tStart;
foreach ( @res ) {
    print "GG: $_";
    my @m = split;
    if ( /r_token / ) { $g_token = $m[3]; }
    if ( /TURL =/ ) { $gTURL = $m[5]; }
#   if ( /request state/ ) { $status = ( "26" eq $m[2] ) ? "[OK]" : "[FAILED]" ; }
#   if ( /request state/ ) { $status = ( "0" eq $m[2] ) ? "[OK]" : "[FAILED]" ; }
    if ( /request state/ ) { $status = ( "$stat26" eq $m[2] ) ? "[OK]" : "[FAILED]" ; }
}
##20 if ( $status ne "[OK]" ) { exit 1; }
##20 else { print " Operation: $operation = $status Duration: $tElapse \n"; }
&printAndCExit();

print " TURL[gsiftp]: $gTURL \n";
$struct->{$kfile}->{f_token} = $f_token;
$struct->{$kfile}->{g_token} = $g_token;
$struct->{$kfile}->{fTURL} = $fTURL;
$struct->{$kfile}->{gTURL} = $gTURL;
}
    print "<_________________________________________________________________________>\n\n";
## End Of Overall (transfer + putdone) operations for all files altogether

##PREV$operation = "dpm-getreqsummary";
$operation = "srm2_testGetRequestSummary";
foreach my $kfile ( sort keys %{$struct} ) {
	XcuteComm("srm2_testGetRequestSummary",
		  "$endPoint $struct->{$kfile}->{r_token} $struct->{$kfile}->{f_token} $struct->{$kfile}->{g_token}",
		  "SU", 0, 1);
}

if ( 0 ) {
    if ( $ENV{HOST} eq "lxb1902" ) {
	#   $ENV{PATH} = "$ENV{PATH}:/afs/cern.ch/user/g/grodid/private/PROC/CLI-MYSQL-SLC3/dpmcli";
	$ENV{PATH} = "$ENV{PATH}:../../gopts/dpmcli";
	$ENV{PATH} = "$ENV{PATH}:../../ns";
	$ENV{PATH} = "$ENV{PATH}:../../dpm";
	$ENV{PATH} = "$ENV{PATH}:../../test/srmv2";
    } else {
	#   $ENV{PATH} = "$ENV{PATH}:/afs/cern.ch/user/g/grodid/private/PROC/procps-3.2.3/dpmcli";
	$ENV{PATH} = "$ENV{PATH}:../../gopts/dpmcli";
	$ENV{PATH} = "$ENV{PATH}:../../ns";
	$ENV{PATH} = "$ENV{PATH}:../../dpm";
	$ENV{PATH} = "$ENV{PATH}:../../test/srmv2";
    }
}

## Does not exist here ... (srmV2)
foreach my $kfile ( sort keys %{$struct} ) {
	XcuteComm("dpm-getreqstatus",
		  "$struct->{$kfile}->{r_token} $struct->{$kfile}->{f_token} $struct->{$kfile}->{g_token}",
		  "ST", "Done", 1);
}

##exit 0;  ##  TESTS  pintime

##PREV$operation = "dpm-extendfilelife";
$operation = "srm2_testExtendFileLifeTime";
foreach my $kfile ( sort keys %{$struct} ) {
	XcuteComm("srm2_testExtendFileLifeTime",
		  "$struct->{$kfile}->{f_token} $kfile $struct->{$kfile}->{pin}",
		  "EF1", 0, 1);
	my $lt = 2*$struct->{$kfile}->{pin};
	XcuteComm("srm2_testExtendFileLifeTime",
		  "$struct->{$kfile}->{g_token} $kfile $lt",
		  "EG2", 0, 1);
}

foreach my $kfile ( sort keys %{$struct} ) {
	XcuteComm("dpm-getreqstatus",
		  "$struct->{$kfile}->{r_token} $struct->{$kfile}->{f_token} $struct->{$kfile}->{g_token}",
		  "ST2", "Done", 1);
}

$operation = "srm2_testExtendFileLifeTime";
foreach my $kfile ( sort keys %{$struct} ) {
	my $lt = 3*$struct->{$kfile}->{pin};
	XcuteComm("srm2_testExtendFileLifeTime",
		  "$struct->{$kfile}->{f_token} $kfile $lt",
		  "EF3", 0, 1);
	$lt = 5*$struct->{$kfile}->{pin};
	XcuteComm("srm2_testExtendFileLifeTime",
		  "$struct->{$kfile}->{g_token} $kfile $lt",
		  "EG5", 0, 1);
}

foreach my $kfile ( sort keys %{$struct} ) {
	XcuteComm("dpm-getreqstatus",
		  "$struct->{$kfile}->{r_token} $struct->{$kfile}->{f_token} $struct->{$kfile}->{g_token}",
		  "ST3", "Done", 1);
}


##PREV$operation = "dpm-relfiles";
$operation = "srm2_testReleaseFiles";
foreach my $kfile ( sort keys %{$struct} ) {
	XcuteComm("srm2_testReleaseFiles",
		  "$struct->{$kfile}->{f_token} $kfile",
		  "RL", 0, 1);
}

$operation = "dpm-getreqstatus";
foreach my $kfile ( sort keys %{$struct} ) {
	XcuteComm("dpm-getreqstatus",
		  "$struct->{$kfile}->{r_token} $struct->{$kfile}->{f_token} $struct->{$kfile}->{g_token}",
		  "ST4", "Done", 1);
}

    XcuteComm("srm2_testLs",
	      "-l $file @Lfile",
	      "LS", 0, 1);

####exit 69;  ##  For special tests

## CopyCompare step ...

## for rfio and gsiftp gets
foreach my $getType ( "fTURL", "gTURL" ) {
    ## for all 4 files ...
    foreach my $kfile ( sort keys %{$struct} ) {
	my $local;
	my $flocal;
	my $lTURL = XtractGoodTurl($struct->{$kfile}->{$getType});
	if ( $getType eq "fTURL" ) {  ##  rfio case
	    $operation = "rfcp";
	    $local = "$tempDir/$struct->{$kfile}->{short}"."F";
	    $struct->{$kfile}->{fLOCAL} = $local;
	    $flocal = $local;
	} else {  ##  gsiftp case
##	    if (0) {  ##  TEMP GG 13/04/05
	    if (1) {  ##  TEMP GG 13/04/05  ##  REVERTED on 10/05/05 for lxb1904
	    $operation = "globus-url-copy -vb";
	    $local = "$tempDir/$struct->{$kfile}->{short}"."G";
	    $struct->{$kfile}->{gLOCAL} = $local;
	    $flocal = "file://".$local;
	} else {
	    $operation = "rfcp";
	    $local = "$tempDir/$struct->{$kfile}->{short}"."F";
	    $struct->{$kfile}->{fLOCAL} = $local;
	    $flocal = $local;
	}
	}
	XcuteComm("$operation",
		  "$lTURL $flocal",
		  "DWNL_$getType", 0);  ##  download substep
	XcuteComm("diff -s",
		  "$struct->{$kfile}->{input} $local",
		  "DIFF_$getType", 0);  ##  diff substep, non-fatal
	unlink $local;
    }
}
## End of CopyCompare step ...

####exit 0;  ##  TEMPORARY ( to avoid killing any file ... )

foreach my $kfile ( sort keys %{$struct} ) {
##  $struct->{$kfile}->{copyX} = "${kfile}X";
    $struct->{$kfile}->{copyY} = "${baseSrm}/${mkdir_t}/$struct->{$kfile}->{short}"."Y";
    $struct->{$kfile}->{copyX} = "${kfile}X";
    $struct->{$kfile}->{copyZ} = "${kfile}Z";
}

    XcuteComm("srm2_testMkdir",
	      "${baseSrm}/$mkdir_t",
	      "MK", 0, 1);

$operation = "srm2_testMv";
foreach my $kfile ( sort keys %{$struct} ) {
	XcuteComm("srm2_testMv",
		  "$kfile $struct->{$kfile}->{copyX}",
		  "MV", 0, 1);
}

my $args = "$file @Lfile";
foreach my $kfile ( sort keys %{$struct} ) {
    $args .= " $struct->{$kfile}->{copyX}";
}
    XcuteComm("srm2_testLs",
	      "-l $args",
	      "LS2", 0, 1);

    XcuteComm("srm2_testLs",
	      "-l ${baseSrm}/$mkdir_t",
	      "LSD", 0, 1);

###BADexit 0;  ##  TEMP for checking gsiftp 24/05/05

$args = "";
    foreach my $kfile ( @Lfile[0,1] ) {
		$args .= " $struct->{$kfile}->{copyX}";
    }
    XcuteComm("srm2_testRm",
	      "$args",
	      "RM", 0, 1);

$args = "";
    foreach my $kfile ( sort keys %{$struct} ) {
	$args .= " $struct->{$kfile}->{copyX}";
    }
    XcuteComm("srm2_testLs",
	      "-l $args",
	      "LS3", 0, 1);

$operation = "srm2_testMv";
foreach my $kfile ( $Lfile[2] ) {
	XcuteComm("srm2_testMv",
		  "$struct->{$kfile}->{copyX} $struct->{$kfile}->{copyY}",
		  "MVk", 0, 1);
}

## Extracting the user/group codes from the remote DPM node PWD/GRP files
my %vogid = ();
my %vogauid = ();
my %vonid = ();
my %voaid = ();
my %votid = ();
my %duser = ();
## Useless but ...
my $sPWD;
my $sGRP;

## To extract $pxGRP and $pxDN values from the proxy (to be used in ACL comparison later) ...
&getPxAttributes();
##exit 0;

##my $tempDir;
##if ( $ENV{TEMPDIR} ) { $tempDir = $ENV{TEMPDIR}; }
##else { $tempDir = "/tmp"; }

if (0) {
$sPWD = "$tempDir/${mach}_PWD_$sfileH";
$sGRP = "$tempDir/${mach}_GRP_$sfileH";

system("globus-url-copy gsiftp://${mach}.cern.ch/${mach}:/etc/passwd file://$sPWD");
system("globus-url-copy gsiftp://${mach}.cern.ch/${mach}:/etc/group  file://$sGRP");
if ( ! ( -s "$sGRP" && -s "$sPWD" ) ) {
    system("rfcp ${mach}:/etc/passwd $sPWD");
    system("rfcp ${mach}:/etc/group  $sGRP");
}
}

####if ( -s "$sGRP" && -s "$sPWD" ) {

if (0) {
foreach my $grp ( "atlas", "cms", "dteam" ) {
    my ($auid, $ux, $uid, $gid, $rest) = split /:/, `grep ${grp}006 $sPWD`;
    if ( ! $gid ) { next; }
    $vogid{$grp} = $gid;
    my ($ruid, $rpl, $rgid, $spus) = split /:/, `grep ${gid} $sGRP`;
    $vogauid{$grp} = $ruid;
    my ($mode, $ix, $irt, $aid, $nest) = split /\s+/, `dpns-ls -ld /dpm/${site_domain}/home/${grp}`;
    $voaid{$aid} = $gid;
    $vonid{$gid} = $aid;
    $votid{$grp} = $aid;
    $votid{$aid} = $grp;
    print " GRP: $grp, GID: $gid, AID: $aid RUID: $ruid\n";
}
}

foreach my $grp ( "atlas", "cms", "dteam", "$pxGRP" ) {
    my @m = split /\s+/, `dpm-getgrpbynam $grp`;
    my $ruid = $m[-1];
    $vogauid{$grp} = $ruid;
    $vogauid{$grp} = $grp;
    my ($mode, $ix, $irt, $aid, $nest) = split /\s+/, `dpns-ls -ld /dpm/${site_domain}/home/${grp}`;
    $votid{$grp} = $aid;
    $votid{$aid} = $grp;
    print " GRP: $grp, AID: $aid RUID: $ruid\n";
}

if (0) {
foreach my $nid ( 1, 4, 5, 6 ) {
    my ($auid, $ux, $uid, $gid, $rest) = split /:/, `grep dteam00$nid $sPWD`;
    $duser{$auid} = $uid;
    $duser{$uid} = $auid;
    print " DTEAM: AUID: $auid, UID: $uid\n";
}
}

foreach my $nid ( 1, 2, 3 ) {
    ##my ($auid, $ux, $uid, $gid, $rest) = split /:/, `grep dteam00$nid $sPWD`;
    my $auid = "dteamtest0$nid";
    ##my $aout = `dpm-getusrbynam $auid`;
    my @m = split /\s+/, `dpm-getusrbynam $auid`;
    my $uid = $m[-1];
    $duser{$auid} = $uid;
    $duser{$uid} = $auid;
    print " DTEAM: AUID: $auid, UID: $uid\n";
}

####}

##BELOWmy ($cmod, $ccix, $cuid, $cgid, $cest) = split /\s+/, `dpns-ls -l $baseDpm/$struct->{$rfile}->{short}X`;
##unlink "$sPWD";
##unlink "$sGRP";
if (0) {
my $atlas = $vogid{"atlas"};
my $cms   = $vogid{"cms"};
my $dteam = $vogid{"dteam"};
}

##Temp: back to old situation (until virtual ID fixes it - 10/11/05)
if (0) {
my $atlas = "atlas";
my $cms   = "cms";
my $dteam = "dteam";
}
if (0) {
my $atlas = "zp";
my $cms   = "zh";
my $dteam = "cg";
}
## Seems to be the good one for now 10/11/05, at least for lxgate22 & lxb1727
## Solves only the issue of making srm2_testSetPermission to work smoothly, not the comparison headache
my $atlas = $vogauid{"atlas"};
my $cms   = $vogauid{"cms"};
##my $dteam = $vogauid{"dteam"};
my $dteam = $vogauid{"$pxGRP"};

my $cfile = $file;
my $fileM = "$struct->{$Lfile[2]}->{copyY}";
## Collision test .........................................................................
## First setPermission to 444
$operation = "srm2_testSetPermission";
#OO$command = "$operation 2 4 4 $struct->{$cfile}->{copyX} 1 - dteam 4";
$command = "$operation 2 4 4 $struct->{$cfile}->{copyX} 1 - $dteam 4";
    $xCount++;
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
@res = `$command 2>&1`;
foreach ( @res ) {print "TSP444: $_"; }

## Then issue a testMv, which MUST fail by now ... (14/07/05) with "Internal error"
## Seems to fail indeed, by now ... (12/09/05) with "File exists", which is far better ! TBC
	XcuteComm("srm2_testMv",
		  "$fileM $struct->{$cfile}->{copyX}",
		  "MWg", 11, 0);
## Then move back to setPermission to 664
$operation = "srm2_testSetPermission";
#OO$command = "$operation 2 6 4 $struct->{$cfile}->{copyX} 1 - dteam 6";
$command = "$operation 2 6 4 $struct->{$cfile}->{copyX} 1 - $dteam 6";
    $xCount++;
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
@res = `$command 2>&1`;
foreach ( @res ) {print "TSP664: $_"; }

$args = "$file";
    foreach my $kfile ( $file, @Lfile ) {
	$args .= " $struct->{$kfile}->{copyX}";
	$args .= " $struct->{$kfile}->{copyY}";
    }
    XcuteComm("srm2_testLs",
	      "-l $args",
	      "LSk", 0, 1);

    XcuteComm("srm2_testRmdir",
	      "${baseSrm}/$mkdir_t",
	      "RMd", 12, 1);

$args = "$file";
    foreach my $kfile ( $file, @Lfile ) {
		$args .= " $struct->{$kfile}->{copyX}";
		$args .= " $struct->{$kfile}->{copyY}";
    }
    XcuteComm("srm2_testLs",
	      "-l $args",
	      "LSd", 0, 1);

$args = "";
    foreach my $kfile ( $Lfile[2] ) {
		$args .= " $struct->{$kfile}->{copyY}";
    }
    XcuteComm("srm2_testRm",
	      "$args",
	      "RMy", 0, 1);

$args = "$file";
foreach my $kfile ( $file, @Lfile ) {
	$args .= " $struct->{$kfile}->{copyX}";
	$args .= " $struct->{$kfile}->{copyY}";
    }
    XcuteComm("srm2_testLs",
	      "-l $args",
	      "LSr", 0, 1);

    XcuteComm("srm2_testRmdir",
	      "${baseSrm}/$mkdir_t",
	      "RMD", 0, 1);

    XcuteComm("srm2_testRmdir",
	      "${baseSrm}/$mkdir_t",
	      "RMA", 5, 1);

#my $rfile = $Lfile[1];
my $rfile = $file;
my ($cmod, $ccix, $cuid, $cgid, $cest) = split /\s+/, `dpns-ls -l $baseDpm/$struct->{$rfile}->{short}X`;

my @aclRes = ();
$operation = "dpns-getacl";
undef $status;
@res = ();
$command = "$operation $baseDpm/$struct->{$rfile}->{short}X";
    $xCount++;
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
@res = `$command 2>&1`;
foreach ( @res ) {
    if ( $_ =~ /^(\# owner: )(.*)/ ) {
	my $rest = $2;
	$rest =~ s/ /_/g;
	print "ACL0: # owner: $rest\n";
    } else {
	print "ACL0: $_";
    }
    #push @aclRes, "ACL1: $_";
    #my @m = split;
}
## To get more info ...
    print "<_________________________________________________________________________>\n\n";
system("dpns-ls -l $baseDpm/$struct->{$rfile}->{short}X");

    XcuteComm("srm2_testLs",
	      "-l $struct->{$rfile}->{copyX}",
	      "LSvACL0", 0, 1);

# For owner and other permissions, first
$operation = "srm2_testSetPermission";
undef $status;
@res = ();
$command = "$operation 2 7 4 $struct->{$rfile}->{copyX} - -";
    $xCount++;
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
@res = `$command 2>&1`;
foreach ( @res ) {print "TSPOP: $_"; }

# For group permissions, now
#ADD, REMOVE, CHANGE= 0, 1, 2
$command = "$operation 0 - - $struct->{$rfile}->{copyX} 2 - $atlas 3 $cms 6";
##p print " CMD: $command\n";
    $xCount++;
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
@res = `$command 2>&1`;
foreach ( @res ) {print "TSPGP36: $_"; }

$command = "$operation 2 - - $struct->{$rfile}->{copyX} 1 - $dteam 5";
    $xCount++;
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
@res = `$command 2>&1`;
foreach ( @res ) {print "TSPGP5: $_"; }

#my @aclRes = ();
$operation = "dpns-getacl";
undef $status;
@res = ();
$command = "$operation $baseDpm/$struct->{$rfile}->{short}X";
    $xCount++;
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
@res = `$command 2>&1`;
foreach ( @res ) {
    if ( $_ =~ /^(\# owner: )(.*)/ ) {
	my $rest = $2;
	$rest =~ s/ /_/g;
	$_ = "# owner: $rest\n";
    } 
    print "ACL1: $_";
    push @aclRes, "ACL1: $_";
    #my @m = split;
}

    XcuteComm("srm2_testLs",
	      "-l $struct->{$rfile}->{copyX}",
	      "LSvACL1", 0, 1);

$operation = "srm2_testSetPermission";
#also test a REMOVE op, and a GROUP NONE
$command = "$operation 1 - - $struct->{$rfile}->{copyX} 1 - $atlas 3"; 
    $xCount++;
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
@res = `$command 2>&1`;
foreach ( @res ) {print "TSPGPA: $_"; }
$command = "$operation 2 - - $struct->{$rfile}->{copyX} 1 - $cms 0";
    $xCount++;
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
@res = `$command 2>&1`;
foreach ( @res ) {print "TSPGPC: $_"; }

$operation = "dpns-getacl";
undef $status;
@res = ();
$command = "$operation $baseDpm/$struct->{$rfile}->{short}X";
    $xCount++;
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
@res = `$command 2>&1`;
foreach ( @res ) {
    if ( $_ =~ /^(\# owner: )(.*)/ ) {
	my $rest = $2;
	$rest =~ s/ /_/g;
	$_ = "# owner: $rest\n";
    } 
    print "ACL2: $_";
    push @aclRes, "ACL2: $_";
    my @m = split;
}

    XcuteComm("srm2_testLs",
	      "-l $struct->{$rfile}->{copyX}",
	      "LSvACL2", 0, 1);

#For user permissions, later
$operation = "srm2_testSetPermission";
$command = "$operation 0 - - $struct->{$rfile}->{copyX} - 1 dteam006 6";
$command = "$operation 0 - - $struct->{$rfile}->{copyX} - 1 dteamtest03 6";
    $xCount++;
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
@res = `$command 2>&1`;
foreach ( @res ) {print "TSPUP6: $_"; }
$command = "$operation 0 - - $struct->{$rfile}->{copyX} - 2 dteam004 4 dteam005 5";
$command = "$operation 0 - - $struct->{$rfile}->{copyX} - 2 dteamtest01 4 dteamtest02 5";
    $xCount++;
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
@res = `$command 2>&1`;
foreach ( @res ) {print "TSPUP45: $_"; }

$operation = "dpns-getacl";
undef $status;
@res = ();
$command = "$operation $baseDpm/$struct->{$rfile}->{short}X";
    $xCount++;
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
@res = `$command 2>&1`;
foreach ( @res ) {
    if ( $_ =~ /^(\# owner: )(.*)/ ) {
	my $rest = $2;
	$rest =~ s/ /_/g;
	$_ = "# owner: $rest\n";
    } 
    print "ACL3: $_";
    push @aclRes, "ACL3: $_";
    my @m = split;
}

    XcuteComm("srm2_testLs",
	      "-l $struct->{$rfile}->{copyX}",
	      "LSvACL3", 0, 1);

$operation = "srm2_testSetPermission";
$command = "$operation 1 - - $struct->{$rfile}->{copyX} - 1 dteam004 4";
$command = "$operation 1 - - $struct->{$rfile}->{copyX} - 1 dteamtest01 4";
    $xCount++;
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
@res = `$command 2>&1`;
foreach ( @res ) {print "TSPUP4: $_"; }
$command = "$operation 2 - - $struct->{$rfile}->{copyX} - 1 dteam006 0";
$command = "$operation 2 - - $struct->{$rfile}->{copyX} - 1 dteamtest03 0";
    $xCount++;
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
@res = `$command 2>&1`;
foreach ( @res ) {print "TSPUP0: $_"; }

$operation = "dpns-getacl";
undef $status;
@res = ();
$command = "$operation $baseDpm/$struct->{$rfile}->{short}X";
    $xCount++;
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
@res = `$command 2>&1`;
foreach ( @res ) {
    if ( $_ =~ /^(\# owner: )(.*)/ ) {
	my $rest = $2;
	$rest =~ s/ /_/g;
	$_ = "# owner: $rest\n";
    } 
    print "ACL4: $_";
    push @aclRes, "ACL4: $_";
    my @m = split;
}

    XcuteComm("srm2_testLs",
	      "-l $struct->{$rfile}->{copyX}",
	      "LSvACL4", 0, 1);

## ../../ns/dpns-getacl -a /dpm/dteam/grodid/srm_test_suite_file.fil606X

$operation = "compareACL";
    $xCount++;
    print "<_________________________________________________________________________>\n\n";
$status = compareACL("$baseDpm/$struct->{$rfile}->{short}X");
##20 if ( $status ne "[OK]" ) { exit 1; }
##20 else { print " Operation: $operation = $status \n"; }
&printAndCExit();

    print "<_________________________________________________________________________>\n\n";

my $fileX = "$struct->{$rfile}->{copyX}";

XcuteComm("srm2_testReassignToUser",
	  "adummyname 1000 $fileX",
	  "RTU", 16);

XcuteComm("srm2_testReserveSpace",
	  "$endPoint $s_token StSysInfo1 4000 2000 20000 0",
	  "RSP1", 0);
my $sp_token = XtractVal("soap_call_ns1__srmReserveSpace returned s_token:");

if (0) {
XcuteComm("srm2_testReleaseSpace",
	  "$endPoint $sp_token StSysInfo1 1",
	  "RLS", 16);
}

XcuteComm("srm2_testUpdateSpace", 
	  "$endPoint $sp_token StSysInfo1 2000 1000 10000",
	  "USP", 0);

XcuteComm("srm2_testCompactSpace", 
	  "$endPoint $sp_token StSysInfo1",
	  "CSP", 16);

##	  "$endPoint $s_token StSysInfo1 3000 1500 15000 0",
XcuteComm("srm2_testReserveSpace",
	  "$endPoint $s_token StSysInfo1 30000000 15000000 15000 0",
	  "RSP2", 0);
my $sp_token1 = XtractVal("soap_call_ns1__srmReserveSpace returned s_token:");

XcuteComm("srm2_testGetSpaceMetaData", 
	  "$endPoint $sp_token $sp_token1",
	  "SMD", 0);

XcuteComm("srm2_testChangeFileStorageType",
	  "0 $fileX",
	  "FST", 16);

XcuteComm("srm2_testGetSpaceToken", 
	  "$endPoint $s_token",
	  "GST", 0);

XcuteComm("srm2_testReleaseSpace",
	  "$endPoint $sp_token StSysInfo1 1",
	  "RLS", 0);

XcuteComm("srm2_testRemoveFiles",
	  "$struct->{$rfile}->{r_token} $fileX",
	  "RFL", 16);

XcuteComm("srm2_testAbortFiles",
	  "$struct->{$Ffile}->{r_token} $Ffile",
	  "AFL", 0);

XcuteComm("srm2_testSuspendRequest",
	  "$struct->{$rfile}->{r_token} $fileX",
	  "SRQ", 16);

XcuteComm("srm2_testResumeRequest",
	  "$struct->{$rfile}->{r_token} $fileX",
	  "RRQ", 16);

XcuteComm("srm2_testCheckPermission",
	  "$fileX",
	  "CHP", 0);

XcuteComm("srm2_testGetRequestID",
		"$endPoint $u_token",
		"GID", 0, 1);

# Try to add dpmmode
$operation = "dpm-getreqid";
undef $status;
@res = ();
$command = "$operation --format=dpmsurl:80,dpmrtyp,dpmpint,dpmfsiz,dpmmode,dpmstatus,dpmfstat $u_token";
    $xCount++;
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
@res = `$command 2>&1`;
foreach ( @res ) {
##    if ( /Stat problem/||/send2nsd: NS009/||/Array Control/ ) { next; }
    print "GID2: $_";
    my @m = split;
    if ( /request state/ ) { $status = ( "Done" eq $m[2] ) ? "[OK]" : "[FAILED]" ; }
}
##20 if ( $status ne "[OK]" ) { exit 1; }
##20 else { print " Operation: $operation = $status \n"; }
&printAndCExit();

$operation = "dpm-getreqid";
undef $status;
@res = ();
$command = "$operation --format=dpmturl:90,dpmrtok,dpmcred $u_token";
    $xCount++;
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
@res = `$command 2>&1`;
foreach ( @res ) {
##    if ( /Stat problem/||/send2nsd: NS009/||/Array Control/ ) { next; }
    if ( /r_token/ ) { next; }
    print "GID3: $_";
    my @m = split;
    if ( /request state/ ) { $status = ( "Done" eq $m[2] ) ? "[OK]" : "[FAILED]" ; }
}
##20 if ( $status ne "[OK]" ) { exit 1; }
##20 else { print " Operation: $operation = $status \n"; }
&printAndCExit();

$operation = "srm2_testMv";
foreach my $kfile ( $file ) {
    XcuteComm("srm2_testMv",
	      "$struct->{$kfile}->{copyX} $kfile",
	      "MVB", 0, 1);
}

$operation = "dpm-getreqid";
undef $status;
@res = ();
$command = "$operation --format=dpmsurl:80,dpmrtyp,dpmpint,dpmfsiz,dpmmode,dpmstatus,dpmfstat $u_token";
$xCount++;
$usage = `$operation 2>&1`;
print "<_________________________________________________________________________>\n\n";
$dateST = `date '+%y%m%d-%H%M%S'`;
print "++++ $$ $dateST\n";
print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
@res = `$command 2>&1`;
foreach ( @res ) {
    print "GID4: $_";
    my @m = split;
    if ( /request state/ ) { $status = ( "Done" eq $m[2] ) ? "[OK]" : "[FAILED]" ; }
}
##20 if ( $status ne "[OK]" ) { exit 1; }
##20 else { print " Operation: $operation = $status \n"; }
&printAndCExit();

TestPutOW(0);
##TestPutOW($overWrite++);
TestPutOW(1);

## The overwrite flag can take values (0|1|2) or (Never|Always|WhenFileIsDifferent)
## Last value is useless - implemented as being equal to Always
TestPutOW(2);

## So that this last test (with 2) is discarded.
if (0) {
my $fileB = "file.Bigger";
system (" cat $struct->{$file}->{input} $struct->{$file}->{input} > $fileB");
$struct->{$file}->{input} = "$fileB";
($struct->{$file}->{size}, $dss) = split /\s+/, `ls -s $struct->{$file}->{input} | awk '{print \$1}'`;
TestPutOW(2);
}

if (1) {
## Actually Transfer the file
$lTURL = XtractGoodTurl($struct->{$file}->{TURL});
undef $status;
$xCount++;
$operation = "rfcp";
@res = ();
$command = "$operation $struct->{$file}->{input} $lTURL";
@res = `$command 2>&1`;
foreach ( @res ) {
    print " RFCP_TROW $_";
}

$operation = "srm2_testPutDone";
undef $status;
my $SURL;
@res = ();
$xCount++;
$command = "$operation $struct->{$file}->{r_token} $file";
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
@res = `$command 2>&1`;
foreach ( @res ) {
    print "PDOW: $_";
    my @m = split;
    if ( /SURL =/ ) { $SURL = $m[5]; }
    if ( /request state/ ) { $status = ( "0" eq $m[2] ) ? "[OK]" : "[FAILED]" ; }
}
##20 if ( $status ne "[OK]" ) { exit 1; }
##20 else { print " Operation: $operation = $status Duration: $tElapse \n"; }
&printAndCExit();

if ( $SURL ne $file ) { 
    print " Mismatch: $SURL $file \n";
    exit 1;
}
}

    print "<_________________________________________________________________________>\n\n";
my $tAlmost = time() - $otStart;
$operation = "OVERALL SUITE";
print " Operation: $operation = $status Duration: $tAlmost sec.\n";
    print "<_________________________________________________________________________>\n\n";

exit 0;

####################################################################################################
sub printAndCExit {
##20 if ( $status ne "[OK]" ) { exit 1; }
##20 else { print " Operation: $operation = $status Duration: $tElapse \n"; }
    if ( $status ne "[OK]" ) {
	foreach ( @res ) {
	    print " FAILURE: $_";
	}
	print " Operation: $operation = $status Duration: $tElapse \n";
#p	print "<_________________________________________________________________________>\n\n";
	exit 1;
    } else {
	print " Operation: $operation = $status Duration: $tElapse \n";
#p	print "<_________________________________________________________________________>\n\n";
	return;
    }
}

####################################################################################################
sub XtractVal {
    my $string = shift;
##    $flat =~ m/$string\s+(.+?)\s+/;
    my $val = "";
    foreach (@res) {
	if ( /$string\s+(\S+?)\s+/ ) {
	    $val = $1;
	} elsif ( /($string\S*?)\s+/ ) {
	    $val = $1;
	}
    }
    print " XtractVal: >$string< >$val< \n";
    return $val;
}

####################################################################################################
sub getPxAttributes {

    ## extract the 'role' or 'VO' (DPM view)
    chomp(my $fqan0 = `voms-proxy-info -fqan | head -1`);
##    if ( $fqan0 =~ 'WARNING' ) {
##	chomp($fqan0 = `voms-proxy-info -vo | head -1`);
    if ( $fqan0 eq '' ) {
	$pxGRP = $voteam;
	print " Got pxGRP: $pxGRP\n";
    } else {
	my @m = split /\//, $fqan0;
	$pxGRP = $m[1];
	foreach my $in ( 2 .. scalar(@m) ) {
	    if ( $m[$in] =~ 'NULL' ) { last; }
	    $pxGRP .= "/" . "$m[$in]";
	}
	print " Got pxGRP: $pxGRP\n";
    }

    ## extract the 'DN'
    chomp(my $ident = `voms-proxy-info -identity | tail -1`);
    $pxDN = $ident;
    $pxDN =~ s/ /_/g;
    print " Got pxDN: $pxDN\n";

    return 0;
}

####################################################################################################
sub XcuteComm {

    $operation = shift;
    my $cargs = shift;
    my $label = shift;
    my $xpectStat = shift;
    my $fatalXit  = shift;
    $xCount++;

##old    undef $status;
    $status = "[UNDEF]";
    @res = ();
    $usage = `$operation 2>&1`;
    $command = "$operation $cargs";
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
    my $tStart = time();
    @res = `$command 2>&1`;
    my $exitFlag = $?;
    my $tEnd = time();
    my $tElapse = $tEnd - $tStart;
##    my $exitFlag = 0;
    foreach ( @res ) {
	$_ =~ s/req.*TURL/TURL/;
	if ( ! /^\s+$|^\.\.\.$/ ) { print "$label: $_"; }
	my @m = split;
	if ( /request state/ ) {
	    $status = ( "$xpectStat" eq "$m[2]" ) ? "[OK]" : "[FAILED]" ;
	} elsif ( /Timed out/ ) {
	    $status = "[FAILED]" ;
	}
    }

    if ( $status eq "[UNDEF]" ) {
	##if ( $exitFlag ) { $status = "[FAILED]" ; }
	$status = ( $exitFlag ) ? "[FAILED]" : "[OK]" ;
    }

    if ( $status ne "[OK]" && $fatalXit) {
	foreach ( @res ) {
	    print " FAILURE: $_";
	}
	print " Operation: $operation = $status Duration: $tElapse \n";
	print "<_________________________________________________________________________>\n\n";
	exit 1;
    } else {
	print " Operation: $operation = $status Duration: $tElapse \n";
	print "<_________________________________________________________________________>\n\n";
    }

##system('ls -l core*');

    return 0;

}

####################################################################################################
sub compareACL {
    my $file = shift;
    my @comp;

    if ( 0 ) {
    @comp = <<"EOACL";
ACL1: # file: $file
ACL1: # owner: $tstNAME
ACL1: # group: cg
ACL1: user::rwx
ACL1: group::r-x                #effective:r-x
ACL1: group:zp:-wx              #effective:-wx
ACL1: group:zh:rw-              #effective:rw-
ACL1: mask::rwx
ACL1: other::r--
ACL2: # file: $file
ACL2: # owner: $tstNAME
ACL2: # group: cg
ACL2: user::rwx
ACL2: group::r-x                #effective:r-x
ACL2: group:zh:---              #effective:---
ACL2: mask::rwx
ACL2: other::r--
ACL3: # file: $file
ACL3: # owner: $tstNAME
ACL3: # group: cg
ACL3: user::rwx
ACL3: user:dteam004:r--         #effective:r--
ACL3: user:dteam005:r-x         #effective:r-x
ACL3: user:dteam006:rw-         #effective:rw-
ACL3: group::r-x                #effective:r-x
ACL3: group:zh:---              #effective:---
ACL3: mask::rwx
ACL3: other::r--
ACL4: # file: $file
ACL4: # owner: $tstNAME
ACL4: # group: cg
ACL4: user::rwx
ACL4: user:dteam005:r-x         #effective:r-x
ACL4: user:dteam006:---         #effective:---
ACL4: group::r-x                #effective:r-x
ACL4: group:zh:---              #effective:---
ACL4: mask::rwx
ACL4: other::r--
EOACL
} else {

    ## Should use here local definition for grp names
    ## That is to say %votid
#my $atlas = $votid{"atlas"};
#my $cms   = $votid{"cms"};
#my $dteam = $votid{"dteam"};
my $atlas = "atlas";
my $cms   = "cms";
#my $dteam = "dteam";
my $dteam = "$pxGRP";
my %duserl = ();
$duserl{dteamtest01} = "dteamtest01";
$duserl{dteamtest02} = "dteamtest02";
$duserl{dteamtest03} = "dteamtest03";

##    my $tstNAME = "$cuid";
##    my $tstGRP  = "$cgid";
    my $tstNAME = "$pxDN";
    my $tstGRP  = "$pxGRP";
    @comp = <<"EOACL";
ACL1: # file: $file
ACL1: # owner: $tstNAME
ACL1: # group: $tstGRP
ACL1: user::rwx
ACL1: group::r-x                #effective:r-x
ACL1: group:$atlas:-wx            #effective:-wx
ACL1: group:$cms:rw-              #effective:rw-
ACL1: mask::rwx
ACL1: other::r--
ACL2: # file: $file
ACL2: # owner: $tstNAME
ACL2: # group: $tstGRP
ACL2: user::rwx
ACL2: group::r-x                #effective:r-x
ACL2: group:$cms:---              #effective:---
ACL2: mask::rwx
ACL2: other::r--
ACL3: # file: $file
ACL3: # owner: $tstNAME
ACL3: # group: $tstGRP
ACL3: user::rwx
ACL3: user:$duserl{dteamtest01}:r--         #effective:r--
ACL3: user:$duserl{dteamtest02}:r-x         #effective:r-x
ACL3: user:$duserl{dteamtest03}:rw-         #effective:rw-
ACL3: group::r-x                #effective:r-x
ACL3: group:$cms:---              #effective:---
ACL3: mask::rwx
ACL3: other::r--
ACL4: # file: $file
ACL4: # owner: $tstNAME
ACL4: # group: $tstGRP
ACL4: user::rwx
ACL4: user:$duserl{dteamtest02}:r-x         #effective:r-x
ACL4: user:$duserl{dteamtest03}:---         #effective:---
ACL4: group::r-x                #effective:r-x
ACL4: group:$cms:---              #effective:---
ACL4: mask::rwx
ACL4: other::r--
EOACL
}

# # ACL1: # file: $file
# # ACL1: # owner: $tstNAME
# # ACL1: # group: $tstGRP
# # ACL1: user::rwx
# # ACL1: group::r--                #effective:r--
# # ACL1: group:atlas:-wx            #effective:-wx
# # ACL1: group:cms:rw-              #effective:rw-
# # ACL1: mask::rwx
# # ACL1: other::r--
# # ACL2: # file: $file
# # ACL2: # owner: $tstNAME
# # ACL2: # group: $tstGRP
# # ACL2: user::rwx
# # ACL2: group::r--                #effective:r--
# # ACL2: group:cms:---              #effective:---
# # ACL2: mask::rwx
# # ACL2: other::r--
# # ACL3: # file: $file
# # ACL3: # owner: $tstNAME
# # ACL3: # group: $tstGRP
# # ACL3: user::rwx
# # ACL3: user:dteam004:r--         #effective:r--
# # ACL3: user:dteam005:r-x         #effective:r-x
# # ACL3: user:dteam006:rw-         #effective:rw-
# # ACL3: group::r--                #effective:r--
# # ACL3: group:cms:---              #effective:---
# # ACL3: mask::rwx
# # ACL3: other::r--
# # ACL4: # file: $file
# # ACL4: # owner: $tstNAME
# # ACL4: # group: $tstGRP
# # ACL4: user::rwx
# # ACL4: user:dteam005:r-x         #effective:r-x
# # ACL4: user:dteam006:---         #effective:---
# # ACL4: group::r--                #effective:r--
# # ACL4: group:cms:---              #effective:---
# # ACL4: mask::rwx
# # ACL4: other::r--

my @aclOrig = (join "",@aclRes);
my ($a, $b);
    if ( 0 ) {
foreach my $c ( 1..10 ) {
    $a = shift @aclOrig;
    $b = shift @comp;
    print "A: >$a<\n";
    print "B: >$b<\n";
}
}

    $aclOrig[0] =~ s;\s+; ;g;
    $comp[0] =~ s;\s+; ;g;
#    print " lengths: ", scalar(@aclRes), " ", scalar(@comp), " \n";

if ( "@aclOrig" ne "@comp" ) { 
    my @aL = split / /, $aclOrig[0];
    my @cL = split / /, $comp[0];
    my $n = 0;
    foreach my $a ( @aL ) {
	my $c = shift @cL;
	$n++;
	if ( $a ne $c ) {
	    print " DIFF: $a $c $n\n";
	}
    }
}

if ( "@aclOrig" ne "@comp" ) { 
    print " ACLRES: @aclOrig \n";
    print "   COMP: @comp \n";
    print " compareACL [FAILED]\n"; 
##############################    return "[FAILED]"; 
}

    return "[OK]";
}

####################################################################################################
sub XtractGoodTurl {
    my $TURL = shift;
    my $sTURL;
    	if ( $TURL =~ /rfio/ ) {  
	    $TURL =~ m[(.*?)://.*?/(.*)];
	    $TURL =~ m[(.*?)://(.*)];
	    $sTURL = $2;
	    $sTURL =~ s;//;:/; ;

	    if ( $ENV{NEW_RFIO_SYNTAX} ) {
		$sTURL = $TURL;  ###  For the NEW RFIOD 30/11/05
	    }

	} else {
	    $TURL =~ m[(.*?://.*?)(/.*?:)(/.*)];
	    $sTURL = $1 . $3;

	    $sTURL = $TURL;  ###  For the NEW FTPD 23/05/05

	}
    print " Xtract\n INPUT: $TURL\n OUTPUT: $sTURL\n";
    return $sTURL;
}

####################################################################################################
sub TestPutOW {
my $overWrite = shift;

my $status;
my $lTURL;
$operation = "srm2_testPut";
$xCount++;
my $lifet = 123456;
my $nbfiles = 1;
$command = "$operation $u_token $nbfiles $overWrite";
#foreach my $kfile ( $Lfile[0] ) {
foreach my $kfile ( $file ) {
    my $fsize = $struct->{$kfile}->{size}*1024;
    if ( $overWrite == 2 ) { $fsize *= 100; }
	$command .= " $kfile $lifet 0 $fsize";
}
    $usage = `$operation 2>&1`;
    print "<_________________________________________________________________________>\n\n";
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
    print " STEP: $xCount OPERATION: $operation USAGE: $usage CMD: $command\n";
$tStart = time();
@res = `$command 2>&1`;
$tEnd = time();
$tElapse = $tEnd - $tStart;
foreach ( @res ) {
    print "LOW$overWrite: $_";
    my @m = split;
    if ( /r_token / ) { $r_token = $m[3]; }
    if ( /state\[(\d+?)\]/ ) { 
	$lTURL = $_; 
	chop($m[2]);
	$tstatus = $m[2]; 
	$TURL = $m[5];
	$struct->{$file}->{TURL} = $TURL;
	$struct->{$file}->{r_token} = $r_token;
    }
    if ( $overWrite ) {
#	if ( /request state/ ) { $status = ( "26" eq $m[2] ) ? "[OK]" : "[FAILED]" ; }
#       if ( /request state/ ) { $status = ( "0" eq $m[2] ) ? "[OK]" : "[FAILED]" ; }
	if ( /request state/ ) { $status = ( "$stat26" eq $m[2] ) ? "[OK]" : "[FAILED]" ; }
    } else {
	## This operation must fail when overWrite = 0 ...
	if ( /request state/ ) { $status = ( "11" eq $m[2] ) ? "[OK]" : "[FAILED]" ; }
    }
}
if ( $status ne "[OK]" || $tstatus ne "22" ) { 
    print " Put FAILED: $lTURL >$status< >$tstatus<\n";
    ##exit 1; 
}
else { print " Operation: $operation = $status Duration: $tElapse \n"; }

    return;
}

####################################################################################################
