#!/usr/bin/perl -w

##########################################################################################
#
#  Suite.pl
#
#  A Perl script to test the DPM (socket interface)
#
#  Author: Gilbert Grosdidier, LAL (Universite Paris-Sud, Orsay)
#  Creation Date:   3/11/2004
#  Last rev:       18/07/2007 (GG) + 16/11/2007
#
##########################################################################################

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

# $Id: socketSuite,v 1.26 2007/11/17 02:20:58 grodid Exp $

use strict;

##use File::CounterFile;

######################
## The following functions are tested or used, in raw:

# $operation = "dpm-put";
# $operation = "globus-url-copy";
# $operation = "rfcp";
# $operation = "dpm-putdone";
# $operation = "dpm-get";
# $operation = "dpm-getreqsummary";
# $operation = "dpm-getreqstatus";
# $operation = "dpm-extendfilelife";
# $operation = "dpm-getreqstatus";
# $operation = "dpm-extendfilelife";
# $operation = "dpm-getreqstatus";
# $operation = "dpm-relfiles";
# $operation = "dpm-getreqstatus";
# $operation = "dpm-getreqid --format=dpmsurl:30,dpmrtyp,dpmpint,dpmfsiz:10,dpmmode,dpmstatus,dpmfstat:6,dpmturl:35";
# $operation = "OVERALL SUITE";

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


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

my $Counter;
my $value; 
my $valinc;
my $sfile;
my $args;
my $xCount = 0;
my $usage;
my $dateST;
chomp(my $dday = `date '+%j'`);
my $tStart = time();
if ( ! $sfileH ) {
    require File::CounterFile;
    $Counter = File::CounterFile->new("./CounterFile", 1000+$dday);
    $value = $Counter->value();
    $valinc = $Counter->inc();
#   $sfile = "socket_Tfile.fil$valinc";
    $sfile = "fil$valinc";
    $sfileH = $sfile;
    print " Counter Start Value: $sfileH \n";
    $sfile = "socket_Tfile.${sfileH}S0";
#    exit 0;
} else {
    print " Counter Start Value: $sfileH \n";
    $sfile = "socket_Tfile.${sfileH}S0";
}

##20$ENV{DPM_HOST}  = "lxb1902";
##20my $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 $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";
}

$ENV{DPNS_HOST} = $target;
$ENV{DPM_HOST}  = $target;
print " 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}_T${sfileH}";

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";
my $baseDteam = "/dpm/$site_domain/home/$voteam";
my $baseDpm = $baseDteam;
if ( $subTree ) {
  $baseDpm = "$baseDteam/$subTree";
}


my $file = "${baseDpm}/$sfile";
my @Lfile = ();

foreach (1..3) {
    ##$valinc = $Counter->inc();
    my $short = "socket_Tfile.${sfileH}S$_";
    push @Lfile, "${baseDpm}/$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";
    print STDOUT " The Current Host is a RH73 one: $fullname \n";
} elsif ( -e "/boot/vmlinux-2.4.21-20.EL.cernsmp" ) {
    ## This is SLC3
##20if ( $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";
    print STDOUT " The Current Host is a SLC3 one: $fullname \n";
} else {
    print STDOUT " Host type unidentified: $fullname \n";
    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;
	}
    }
}

print STDOUT " \n";
foreach my $kfile ( sort keys %{$struct} ) {
    my $rls = `ls -l $struct->{$kfile}->{input}`;
    print STDOUT " File: $kfile Orig: $struct->{$kfile}->{input} \n$rls";
}
print STDOUT " \n";

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

$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}'`;
    print " VERIF0: $file $struct->{$file}->{input} $struct->{$file}->{size} \n";

my $f1stfproc = "gsiftp";  #####  SPECIAL: must be [rfio|gsiftp]
## REVERTED on 10/05/05  $f1stfproc = "rfio";  ##  TEMP on 13/04/05

my $foperation;
if ( $f1stfproc eq "rfio" ) {
    $foperation = "rfcp";
} else {
    $foperation = "globus-url-copy";
}

system("which rfcp");
#system("echo 1 \$PATH; which rfcp");
#system("echo 2 \$PATH; hash rfcp; which rfcp");
print STDOUT " \n";

##PREVmy $u_token = "gilbert_dec13";

##PREVmy @res = `dpm-put --dpmproto gsiftp --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 $command;
my $findex;
my $operation = "dpm-put";
$command = "$operation --dpmproto gsiftp --dpmutoken=$u_token --dpmreqsize 100 $file";
if ( $s_token ) {
    $command = "$operation --dpmproto $f1stfproc --dpmstoken=$s_token --dpmutoken=$u_token --dpmreqsize $struct->{$file}->{size} $file";
} else {
    $command = "$operation --dpmproto $f1stfproc --dpmutoken=$u_token --dpmreqsize 100 $file";
}
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
print " CMD: $command\n";
my @res = `$command`;
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 = $_; 
	$tstatus = $m[2]; 
	$TURL = $m[5]; 
	##PREV$TURL =~ m[(.*?)://(.*)];
	if ( $f1stfproc eq "rfio" ) {  #####  SPECIAL
	    $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

	}
	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;
    }
    if ( /request state/ ) { $status = ( "Done" eq $m[2] ) ? "[OK]" : "[FAILED]" ; }
}
if ( $status ne "[OK]" || $tstatus ne "Ready," ) { 
    print " Put FAILED: $lTURL \n";
    exit 1; 
}
else { print " Operation: $operation = $status \n"; }

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

undef $status;
@res = ();
##PREV@res = `dpm-put --dpmutoken=$u_token --dpmproto rfio --dpmreqsize 100 @Lfile`;
$fflag = 2;
if ( $s_token ) {
    $command = "$operation --dpmutoken=$u_token --dpmstoken=$s_token --dpmproto rfio --dpmreqsize 10 @Lfile";
    $command = "$operation --dpmutoken=$u_token --dpmstoken=$s_token --dpmproto rfio --dpmtosurls $struct->{$Lfile[0]}->{size},$struct->{$Lfile[1]}->{size},$struct->{$Lfile[2]}->{size} @Lfile";
    $command = "$operation --dpmutoken=$u_token --dpmstoken=$s_token --dpmproto rfio --dpmfsizes $struct->{$Lfile[0]}->{size},$struct->{$Lfile[1]}->{size},$struct->{$Lfile[2]}->{size} @Lfile";
} else {
    $command = "$operation --dpmutoken=$u_token --dpmproto rfio --dpmreqsize 100 @Lfile";
}
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
print " CMD: $command\n";
@res = `$command`;
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 = $_; 
	$tstatus = $m[2]; 
	$TURL = $m[5]; 
	##PREV$TURL =~ m[(.*?)://(.*)];
	##PREV$TURL =~ m[(.*?)://.*?/(.*)];
	$TURL =~ m[(.*?)://(.*)];
	$sTURL = $2;
	$sTURL =~ s;//;:/; ;

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

	my $lfile;
	if ( $fflag == 1 ) {
	    $lfile = $file;
	} else {
	    $lfile = $Lfile[$findex];
	}
	$struct->{$lfile}->{TURL} = $TURL;
	$struct->{$lfile}->{sTURL} = $sTURL;
#p	print " VERIF: $TURL $2 $sTURL $lfile $struct->{$lfile}->{sTURL}\n";
##	$struct->{$lfile}->{TURL} = $TURL;
	$struct->{$lfile}->{r_token} = $r_token;
    }
    if ( /request state/ ) { $status = ( "Done" eq $m[2] ) ? "[OK]" : "[FAILED]" ; }
}
if ( $status ne "[OK]" || $tstatus ne "Ready," ) { 
    print " Put FAILED: $lTURL \n";
    exit 1; 
}
else { print " Operation: $operation = $status \n"; }

foreach my $kfile ( sort keys %{$struct} ) {
    ##BADforeach my $kfile ( @Lfile ) {

    if ( $kfile eq $file ) {

	## For the first file of the stack
	undef $status;
	@res = ();
	##PREV$command = "$operation file://".$ENV{PWD}."/$struct->{$file}->{input} $struct->{$file}->{TURL}";
	if ( $f1stfproc eq "rfio" ) {
	    $operation = "rfcp";
	    $command = "$operation $struct->{$file}->{input} $struct->{$file}->{sTURL}";
	} else {
	    $operation = "globus-url-copy";
	    $command = "$operation file://$struct->{$file}->{input} $struct->{$file}->{sTURL}";
	}
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
	print " CMD: $command\n";
	@res = `$command 2>&1`;
	foreach ( @res ) {
	    print "C: $_";
	    my @m = split;
	}
	$status = $? ? "[FAILED]" : "[OK]" ;
	if ( $status ne "[OK]" ) {
	    exit 1;
	} else {
	    print " Operation: $operation = $status \n";
	}

    } else {

	## For the rest of the stack
	undef $status;
	$operation = "rfcp";
	@res = ();
	#@res = `rfcp /etc/ldap.conf $sTURL`;
	#@res = `rfcp /etc/termcap $sTURL`;
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
	print " CMD: rfcp $struct->{$kfile}->{input} $struct->{$kfile}->{sTURL}\n";
	@res = `rfcp $struct->{$kfile}->{input} $struct->{$kfile}->{sTURL}`;
	foreach ( @res ) {
	    if ( /bytes/ ) {
		$status = "[OK]";
	    }
	}
	if ( $status ne "[OK]" ) {
	    exit 1;
	} else {
	    print " Operation: $operation = $status \n";
	}

    }

    $operation = "dpm-putdone";
    undef $status;
    my $SURL;
    @res = ();
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
    print " CMD: dpm-putdone --dpmrtoken=$struct->{$kfile}->{r_token} $kfile \n";
    @res = `dpm-putdone --dpmrtoken=$struct->{$kfile}->{r_token} $kfile`;
    foreach ( @res ) {
	print "D: $_";
	my @m = split;
	if ( /SURL =/ ) {
	    $SURL = $m[5];
	}
	if ( /request state/ ) {
	    $status = ( "Done" eq $m[2] ) ? "[OK]" : "[FAILED]" ;
	}
    }
    if ( $status ne "[OK]" ) {
	exit 1;
    } else {
	print " Operation: $operation = $status \n";
    }

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

    my $fTURL;
    $TURL = $struct->{$kfile}->{TURL};
    undef $status;
    $operation = "dpm-get";
    @res = ();
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
    print " CMD: dpm-get --dpmutoken=$u_token --dpmproto:rfio $kfile \n";
    @res = `dpm-get --dpmutoken=$u_token --dpmproto:rfio $kfile`;
    foreach ( @res ) {
	print "GF: $_";
	my @m = split;
	if ( /r_token:/ ) {
	    $f_token = $m[3];
	}
	if ( /TURL =/ ) {
	    $fTURL = $m[5];
	}
	if ( /request state/ ) {
	    $status = ( "Done" eq $m[2] ) ? "[OK]" : "[FAILED]" ;
	}
    }
    if ( $status ne "[OK]" ) {
	exit 1;
    } else {
	print " Operation: $operation = $status \n";
    }

    my @TURL_  = ($TURL  =~ m[\w+?://(.*)]);
    my @fTURL_ = ($fTURL =~ m[\w+?://(.*)]);
    if ( "@TURL_" ne "@fTURL_" ) { 
	print " Mismatch: >@TURL_< >@fTURL_< \n";
	print " Mismatch: $TURL $fTURL \n";
	####exit 1;
    } else {
	print " TURL[rfio]: $TURL $fTURL\n";
    }

    my $gTURL;
    undef $status;
    @res = ();
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
    print " CMD: dpm-get --dpmutoken=$u_token --dpmproto:gsiftp $kfile \n";
    @res = `dpm-get --dpmutoken=$u_token --dpmproto:gsiftp $kfile`;
    foreach ( @res ) {
	print "GG: $_";
	my @m = split;
	if ( /r_token:/ ) {
	    $g_token = $m[3];
	}
	if ( /TURL =/ ) {
	    $gTURL = $m[5];
	}
	if ( /request state/ ) {
	    $status = ( "Done" eq $m[2] ) ? "[OK]" : "[FAILED]" ;
	}
    }
    if ( $status ne "[OK]" ) {
	exit 1;
    } else {
	print " Operation: $operation = $status \n";
    }

    @TURL_  = ($TURL  =~ m[\w+?://(.*)]);
    @fTURL_ = ($gTURL =~ m[\w+?://(.*)]);
    if ( "@TURL_" ne "@fTURL_" ) { 
	print " Mismatch: >@TURL_< >@fTURL_< \n";
	print " Mismatch: $TURL $gTURL \n";
	#####exit 1;
    } else {
	print " TURL[gsiftp]: $TURL $gTURL \n";
    }

    $struct->{$kfile}->{f_token} = $f_token;
    $struct->{$kfile}->{g_token} = $g_token;
}

foreach my $kfile ( sort keys %{$struct} ) {
undef $status;
$operation = "dpm-getreqsummary";
@res = ();
##PREV@res = `dpm-getreqsummary $struct->{$kfile}->{r_token} $struct->{$kfile}->{f_token} $struct->{$kfile}->{g_token}`;
$command = "$operation  $struct->{$kfile}->{r_token} $struct->{$kfile}->{f_token} $struct->{$kfile}->{g_token}";
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
print " CMD: $command\n";
@res = `$command`;
foreach ( @res ) {
    if ( ! /state/ ) { next; }
    my @m = split;
    print "S: $_";
    if ( /request state/ ) { $status = ( "Done" eq $m[2] ) ? "[OK]" : "[FAILED]" ; }
}
if ( $status ne "[OK]" ) { exit 1; }
else { print " Operation: $operation = $status \n"; }
}

foreach my $kfile ( sort keys %{$struct} ) {
undef $status;
$operation = "dpm-getreqstatus";
@res = ();
##PREV@res = `dpm-getreqstatus $struct->{$kfile}->{r_token} $struct->{$kfile}->{f_token} $struct->{$kfile}->{g_token}`;
$command = "$operation  $struct->{$kfile}->{r_token} $struct->{$kfile}->{f_token} $struct->{$kfile}->{g_token}";
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
print " CMD: $command\n";
@res = `$command`;
foreach ( @res ) {
    if ( ! /state/ ) { next; }
    my @m = split;
#    print "S: $_";
    if ( /request, state/ ) {
	my $l = $_;
	$l =~ s/req.*TURL/TURL/;
#	$l =~ s/TURL.*fsz/fsz/;
	print "S: $l";
    } else {
	print "S: $_";
    }
    if ( /request state/ ) { $status = ( "Done" eq $m[2] ) ? "[OK]" : "[FAILED]" ; }
}
if ( $status ne "[OK]" ) { exit 1; }
else { print " Operation: $operation = $status \n"; }
}

$operation = "dpm-extendfilelife";
foreach my $kfile ( sort keys %{$struct} ) {
    undef $status;
    @res = ();
##PREV    @res = `dpm-extendfilelife --dpmrtoken $struct->{$kfile}->{f_token} --dpmlifet=$struct->{$kfile}->{pin} $kfile`;
    $command = "$operation --dpmrtoken $struct->{$kfile}->{f_token} --dpmlifet=$struct->{$kfile}->{pin} $kfile";
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
    print " CMD: $command\n";
    @res = `$command`;
    foreach ( @res ) {
	if ( ! /stat/ ) { next; }
	my @m = split;
	#print "E: $_";
	if ( /new/ ) { print "E: $_"; }
	if ( /request state/ ) { $status = ( "Done" eq $m[2] ) ? "[OK]" : "[FAILED]" ; }
    }
    if ( $status ne "[OK]" ) { exit 1; }
    else { print " Operation: $operation = $status \n"; }
    undef $status;
    @res = ();
    my $lt = 2*$struct->{$kfile}->{pin};
##PREV    @res = `dpm-extendfilelife --dpmrtoken $struct->{$kfile}->{g_token} --dpmlifet=$lt $kfile`;
    $command = "$operation --dpmrtoken $struct->{$kfile}->{g_token} --dpmlifet=$lt $kfile";
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
    print " CMD: $command\n";
    @res = `$command`;
    foreach ( @res ) {
	if ( ! /stat/ ) { next; }
	my @m = split;
	if ( /new/ ) { print "E: $_"; }
	if ( /request state/ ) { $status = ( "Done" eq $m[2] ) ? "[OK]" : "[FAILED]" ; }
    }
    if ( $status ne "[OK]" ) { exit 1; }
    else { print " Operation: $operation = $status \n"; }
}

foreach my $kfile ( sort keys %{$struct} ) {
undef $status;
$operation = "dpm-getreqstatus";
@res = ();
##PREV@res = `dpm-getreqstatus $struct->{$kfile}->{r_token} $struct->{$kfile}->{f_token} $struct->{$kfile}->{g_token}`;
$command = "$operation $struct->{$kfile}->{r_token} $struct->{$kfile}->{f_token} $struct->{$kfile}->{g_token}";
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
print " CMD: $command\n";
@res = `$command`;
foreach ( @res ) {
    if ( ! /state/ ) { next; }
    my @m = split;
#    print "S: $_";
    if ( /request, state/ ) {
	my $l = $_;
	$l =~ s/req.*TURL/TURL/;
#	$l =~ s/TURL.*fsz/fsz/;
	print "S: $l";
    } else {
	print "S: $_";
    }
    if ( /request state/ ) { $status = ( "Done" eq $m[2] ) ? "[OK]" : "[FAILED]" ; }
}
if ( $status ne "[OK]" ) { exit 1; }
else { print " Operation: $operation = $status \n"; }
}

$operation = "dpm-extendfilelife";
foreach my $kfile ( sort keys %{$struct} ) {
    undef $status;
    @res = ();
    my $lt = 3*$struct->{$kfile}->{pin};
##PREV    @res = `dpm-extendfilelife --dpmrtoken $struct->{$kfile}->{f_token} --dpmlifet=$lt $kfile`;
    $command = "$operation --dpmrtoken $struct->{$kfile}->{f_token} --dpmlifet=$lt $kfile";
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
    print " CMD: $command\n";
    @res = `$command`;
    foreach ( @res ) {
	if ( ! /stat/ ) { next; }
	my @m = split;
	#print "E: $_";
	if ( /new/ ) { print "E: $_"; }
	if ( /request state/ ) { $status = ( "Done" eq $m[2] ) ? "[OK]" : "[FAILED]" ; }
    }
    if ( $status ne "[OK]" ) { exit 1; }
    else { print " Operation: $operation = $status \n"; }
    undef $status;
    @res = ();
    $lt = 5*$struct->{$kfile}->{pin};
##PREV    @res = `dpm-extendfilelife --dpmrtoken $struct->{$kfile}->{g_token} --dpmlifet=$lt $kfile`;
    $command = "$operation --dpmrtoken $struct->{$kfile}->{g_token} --dpmlifet=$lt $kfile";
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
    print " CMD: $command\n";
    @res = `$command`;
    foreach ( @res ) {
	if ( ! /stat/ ) { next; }
	my @m = split;
	#print "E: $_";
	if ( /new/ ) { print "E: $_"; }
	if ( /request state/ ) { $status = ( "Done" eq $m[2] ) ? "[OK]" : "[FAILED]" ; }
    }
    if ( $status ne "[OK]" ) { exit 1; }
    else { print " Operation: $operation = $status \n"; }
}

foreach my $kfile ( sort keys %{$struct} ) {
undef $status;
$operation = "dpm-getreqstatus";
@res = ();
##PREV@res = `dpm-getreqstatus $struct->{$kfile}->{r_token} $struct->{$kfile}->{f_token} $struct->{$kfile}->{g_token}`;
$command = "$operation $struct->{$kfile}->{r_token} $struct->{$kfile}->{f_token} $struct->{$kfile}->{g_token}";
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
print " CMD: $command\n";
@res = `$command`;
foreach ( @res ) {
    if ( ! /state/ ) { next; }
    my @m = split;
#    print "S: $_";
    if ( /request, state/ ) {
	my $l = $_;
	$l =~ s/req.*TURL/TURL/;
#	$l =~ s/TURL.*fsz/fsz/;
	print "S: $l";
    } else {
	print "S: $_";
    }
    if ( /request state/ ) { $status = ( "Done" eq $m[2] ) ? "[OK]" : "[FAILED]" ; }
}
if ( $status ne "[OK]" ) { exit 1; }
else { print " Operation: $operation = $status \n"; }
}

$operation = "dpm-relfiles";
foreach my $kfile ( sort keys %{$struct} ) {
    undef $status;
    @res = ();
##PREV    print " CMD: dpm-relfiles --dpmrtoken $struct->{$kfile}->{f_token} $kfile \n";
##PREV    @res = `dpm-relfiles --dpmrtoken $struct->{$kfile}->{f_token} $kfile`;
    $command = "$operation --dpmrtoken $struct->{$kfile}->{f_token} $kfile";
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
    print " CMD: $command\n";
    @res = `$command`;
    foreach ( @res ) {
	print "R: $_";
	my @m = split;
	if ( /request state/ ) { $status = ( "Done" eq $m[2] ) ? "[OK]" : "[FAILED]" ; }
    }
    if ( $status ne "[OK]" ) { exit 1; }
    else { print " Operation: $operation = $status \n"; }
}

foreach my $kfile ( sort keys %{$struct} ) {
undef $status;
$operation = "dpm-getreqstatus";
@res = ();
##PREV@res = `dpm-getreqstatus $struct->{$kfile}->{r_token} $struct->{$kfile}->{f_token} $struct->{$kfile}->{g_token}`;
$command = "$operation $struct->{$kfile}->{r_token} $struct->{$kfile}->{f_token} $struct->{$kfile}->{g_token}";
  $dateST = `date '+%y%m%d-%H%M%S'`;
  print "++++ $$ $dateST\n";
print " CMD: $command\n";
@res = `$command`;
foreach ( @res ) {
    if ( ! /state/ ) { next; }
    my @m = split;
#    print "S: $_";
    if ( /request, state/ ) {
	my $l = $_;
	$l =~ s/req.*TURL/TURL/;
#	$l =~ s/TURL.*fsz/fsz/;
	print "S: $l";
    } else {
	print "S: $_";
    }
    if ( /request state/ ) { $status = ( "Done" eq $m[2] ) ? "[OK]" : "[FAILED]" ; }
}
if ( $status ne "[OK]" ) { exit 1; }
else { print " Operation: $operation = $status \n"; }
}

$operation = "dpm-getreqid";
undef $status;
@res = ();
$command = "$operation --format=dpmsurl:46,dpmrtyp,dpmpint,dpmfsiz:10,dpmmode,dpmstatus,dpmfstat:16,dpmturl:55 $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]" ; }
}
if ( $status ne "[OK]" ) { exit 1; }
else { print " Operation: $operation = $status \n"; }

$args = "";
foreach my $kfile ( sort keys %{$struct} ) {
    if ( $kfile eq $file ) { next; }
    if ( $kfile eq $Lfile[-1] ) { next; }
    $args .= " $kfile";
}
XcuteComm("dpm-rm", 
	  "$args",
	  "RM1", "Done", 1);

# Try to add dpmmode
$operation = "dpm-getreqid";
undef $status;
@res = ();
$command = "$operation --format=dpmsurl:46,dpmrtyp,dpmpint,dpmfsiz:10,dpmmode,dpmstatus,dpmfstat:16,dpmturl:55 $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 "GID3: $_";
    my @m = split;
    if ( /request state/ ) { $status = ( "Done" eq $m[2] ) ? "[OK]" : "[FAILED]" ; }
}
if ( $status ne "[OK]" ) { exit 1; }
else { print " Operation: $operation = $status \n"; }


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

exit 0;

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

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

    undef $status;
    @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";
    @res = `$command 2>&1`;
    foreach ( @res ) {
	$_ =~ s/req.*TURL/TURL/;
	if ( ! /^\s+$|^\.\.\.$/ ) { print "$label: $_"; }
	my @m = split;
	if ( /request state/ ) {
	    $status = ( "$xpectStat" eq "$m[2]" ) ? "[OK]" : "[FAILED]" ;
	}
    }
    if ( $status ne "[OK]" && $fatalXit) {
	exit 1;
    } else {
	print " Operation: $operation = $status \n";
	print "<_________________________________________________________________________>\n\n";
    }

    return 0;

}
