#
# StoreTestResults.pl
#
# Stores results (log, mask, trim, RC files etc plus PS files) from a 
# test sequence into a archive directory under $archive (defined in
# LocalVariables.pl).
#
# Syntax: StoreTestResults.pl <DUTSerialNumber> <SequenceType> <Run> <Scan>
# (You probably need to say perl StoreTestResults.pl... on Windows)
#
# DUTSerialNumber is as you specified in sctdaq config
# SequenceType    is a user string, it might be something like
#                   Hybrid_Initial or Module_Longterm
#                 it is used as a directory name in $archive
# Run/Scan        are the start run/scan of the test sequence
#                 the sequence is assumed all to be in the same run
#                 the first scan must be correct!
#                 Scan defaults to 1
#
# The algorithm for finding/using log files is imperfect: particularly if 
# you have frequently re-analysed a test sequence the script will pick
# up the results of the latest reanalysis. This may not be what you want.
#
# If installing this script afresh on a new system, you also need the perl
# libraries SctDAQWebLib.pl and LocalVariables.pl; You should (at least) look
# at LocalVariables.pl to check it is OK for your local system
#
# It is expected this script will run on Windows, but it should work on Unix
# with changes only being needed in LocalVariables.pl (your mileage may 
# differ: feedback welcome) plus perhaps your #!path-to-perl on the first 
# line of this script - if there is no path-to-perl, run the script via
#
#   perl StoreTestResults.pl <arguments>
#
# Send feedback to Dave.Charlton@cern.ch
#
# Version 0.8 DGC  10 Jun 2003 Look for longterm PS file with and without hostname
# Version 0.7 DGC  11 Feb 2003 Look for ltt PS file with 2 different scan nums
# Version 0.6 DGC  3 Feb 2003  Fix bug that copied too many files if previous
#                              tests done on device that day; copy XML files too
# Version 0.5 DGC  29 Oct 2002 Backup raw data from here as well
# Version 0.4 DGC  9 Sep 2002  Minor changes to handle LDT a bit better
# Version 0.3 DGC  If the SequenceType has already been done, refuse to copy
#                  files and suggest deletion of target directory
# Version 0.2 DGC  Copies LDT and longterm.ps files as well, at least 
#                  for simple case
# Version 0.1 DGC  First version


require "SctDAQWebLib.pl" || die "Cannot load SctDAQWebLib.pl" ;
&InitialiseVariables ;

if ( $#ARGV!=2 && $#ARGV!=3 ) {
    print "syntax: StoreTestResults <s/n> <TestSequence> <run> [<scan>]\n" ;
    print "        (<scan> defaults to 1)\n" ;
    die "bad arguments" ;
}
$DUT=$ARGV[0] ;
$TestSequence=$ARGV[1] ;
$TestRun=$ARGV[2] ;
$StartScan=1 ;
$StartScan=$ARGV[3] if $#ARGV>2 ;

$TestArchiveDir="$archive/$DUT/$TestSequence" ;
print "First checking if target directory $TestArchiveDir exists...\n" ;
if (-d $TestArchiveDir) {
    print "\nFound it! Overwriting this directory is a bad idea, so now stopping...\n\n" ;
    print "If you really want to replace the results of $TestSequence for\n" ;
    print "  $DUT with the new results, please first delete or rename\n" ;
    print "  the target directory.\n" ;
    exit -4 ;
}
print "Searching for most recent log file containing run $TestRun scan $StartScan...\n" ;

@LogFiles=sort {$b cmp $a} &GetLogFiles($DUT,$rawresultdir) ;

$found=0 ;
$#ResultFiles=-1 ;
$MaxScan=-1 ;
foreach $f (@LogFiles) {
    print "Searching $f...\n" ;
    open(LOGF,"$rawresultdir/$f") ;
    $#scans=-1 ;
    $runline="" ;
    while (<LOGF>) {
	if (/^%NewTest/) {
	    push @scans,$runline if $runline ne "" ;
	    $runline="" ;
	}
	$runline=$_ if /^Run number/ ;
	$runline="" if /^%LongTermTest/ ;
    }
    push @scans,$runline if $runline ne "" ;
    close(LOGF) ;
    $RunFoundInFile=0 ;
    foreach $runline (@scans) {
	chomp $runline ;
	$runscan=(split(':',$runline))[1] ;
	($run,$scan)=split('-',$runscan) ;
	next if $run!=$TestRun ;
	$MaxScan=$scan if $scan>$MaxScan ;
	$RunFoundInFile=1 ;
	if ($run==$TestRun && $scan==$StartScan) {
	    print "..found run $run scan $scan!\n" ;
	    $found=1 ;
	    last ;
	}
    }
    if ($RunFoundInFile) {
	unshift @ResultFiles,$f ;
    }
    last if $found ;
}

if (!$found) {
    die "Sorry: cannot find run/scan $TestRun/$StartScan in result files for device $DUT" ; 
}

# We have the desired result files. 

print "Will archive results from following " , $#ResultFiles+1, " result files...\n" ;
foreach $f (@ResultFiles) {
    print " $f\n" ;
}
print " except that archiving will stop as soon as a higher run number is found\n" ;

# Make the results directory

$ArchiveDUTTop="$archive/$DUT" ;
print "Searching for directory $ArchiveDUTTop...\n" ;
if (-d $ArchiveDUTTop) {
    print "...exists\n" ;
}
else {
    print "...doesn't exist, so creating it\n" ;
    mkdir($ArchiveDUTTop,0777) ;
}

$TestArchiveDir="$archive/$DUT/$TestSequence" ;
print "Searching for directory $TestArchiveDir...\n" ;
if (-d $TestArchiveDir) {
    print "...exists\n" ;
}
else {
    print "...doesn't exist, so creating it\n" ;
    mkdir($TestArchiveDir,0777) ;
}

# In copying the results file, only the relevant run is kept
# BUT all data from that run should be kept
# Following runs should also not be included 
# (?this might be a bad assumption for the LTT?)

$AnyURLs=0 ;

$TotalTests=0 ;

$host="" ;
foreach $ResultFile (@ResultFiles) {

# Complexity: we want to copy from the first scan, but also to allow a 
# prior hard-reset test to make it...!
# Current kludge: Copy from first scan >= wanted in right run, or from first hard reset
#                 test in that run. There is a better way...!

    $ResultFileRun=$ResultFile ;
    $ResultFileRun=~s/\./_$TestRun\./ ;
    $ResultFileOut="$TestArchiveDir/$ResultFileRun" ;
    $#NumNewTests=-1 ;

    $LineNum=0 ;
    $FirstLineWanted=-1 ;
    $LastLineWanted=-1 ;
    $CurrentTestStartLine=0 ;
    $AnyHigherRun=0 ;
    open(LOGF,"$rawresultdir/$ResultFile") ;
    $hostline=0 ;
    while (<LOGF>) {
	chop ;
	$host=(split("\"",$_))[1] if $hostline ;
	$hostline=0 ;
	$hostline=1 if /^#HOST/ ;
	$CurrentTestStartLine=$LineNum-1 if /^%NewTest/ ;
	push @NumNewTests,$CurrentTestStartLine if /^%NewTest/ ;
	$r=(split('-',(split(':'))[1]))[0] if /^Run number/;
	$s=(split('-',(split(':'))[1]))[1] if /^Run number/;
	$FirstLineWanted=$CurrentTestStartLine if $r==$TestRun && ($s>=$StartScan || $s eq "") && $FirstLineWanted<0 ;
	$LastLineWanted=$CurrentTestStartLine-1 if $r>$TestRun && $LastLineWanted<0 ;
	$AnyHigherRun=1 if $r>$TestRun && $FirstLineWanted>=0 ;
	$LineNum++ ;
    }
    close(LOGF) ;

#  and only keep parts from the requested run

    open(LOGF,"$rawresultdir/$ResultFile") ;
    open(LOGF_O,">$ResultFileOut") ;
    $LineNum=0 ;
    while (<LOGF>) {
	if ($LineNum>=$FirstLineWanted && 
	    ($LineNum<=$LastLineWanted || $LastLineWanted<0)) {
	    print LOGF_O  ;
	}    
	else {
	    $LineNum++ ;
	    next ;
	}
	last if $LastLineWanted>0 && $LineNum>$LastLineWanted  ;
	$LineNum++ ;
	chop ;
	if (/^URL/ || /^FILENAME/) {
	    $SourceFile=substr($_,index($_,':')+2) ;
	    if (/^URL/) {
		$SourceFile=&StripWebKey($SourceFile) ;
		$SourceFile="$rawpsdir/$SourceFile" ;
		$AnyURLs=1 ;
	    }
	    if (/^FILENAME/) {
		$SourceFile=~s/\\/\//g ;
	    } 
	    $DestinationFile=substr($SourceFile,rindex($SourceFile,'/')+1) ;
	    $DestinationFile="$TestArchiveDir/$DestinationFile" ;
	    &CopyFile($SourceFile,$DestinationFile) ;
# Special action for .trim files - also copy .mask files!
	    $Extension=substr($SourceFile,rindex($SourceFile,'.')+1) ;
	    if ($Extension eq "trim") {
		$SourceFile=~s/\.trim/\.mask/ ;
		$DestinationFile=~s/\.trim/\.mask/ ;
		&CopyFile($SourceFile,$DestinationFile) ;
	    }
	}
    }
    close (LOGF_O) ;
    close(LOGF) ;

    $TotalTests+=$#NumNewTests+1 ;

    if ($AnyHigherRun) {
	print "Found a higher run number in $ResultFile, stopping archival here.\n" ;
    }
    last if $AnyHigherRun ;
}

print "Tests ran on host $host\n" ;

print "Now looking for any XML config files...\n" ;
@xmlfiles=&GetXMLFiles($DUT,$rawresultdir) ;
foreach $x (@xmlfiles) {
    ($dum,$run,$scanxml)=split('\_',$x) ;
    $scan=(split('\.',$scanxml))[0] ;
    next if $run!=$TestRun ;
    next if $scan<$StartScan ;
    $SourceFile="$rawresultdir/$x" ;
    $DestinationFile="$TestArchiveDir/$x" ;
    &CopyFile($SourceFile,$DestinationFile) ;
}

print "The number of tests wanted was found to be: $TotalTests\n" ;
if ($TotalTests>$NumTestsInCharacterisation +5 ) {
    print "This seems to be a Longterm test, so looking for LDT file...\n" ;
    $FirstDate=$ResultFiles[0] ;
    $FirstDate=~s/$DUT\_// ;
    $FirstDate=~s/\.txt// ;
    $LDTFile="$DUT\_LDT\_$FirstDate.txt" ;
    print " specifically for file $LDTFile\n" ;
    if (-f "$rawresultdir/$LDTFile") {
	print "  found and copying it...\n" ;
	&CopyFile("$rawresultdir/$LDTFile","$TestArchiveDir/$LDTFile") ;
    }
    $LDTFile="LONGTERM\_$DUT\_$FirstDate.txt" ;
    print " and also for file $LDTFile\n" ;
    if (-f "$rawresultdir/$LDTFile") {
	print "  found and copying it...\n" ;
	&CopyFile("$rawresultdir/$LDTFile","$TestArchiveDir/$LDTFile") ;
    }
    $startlessone=$StartScan-1 ;
    $HistoryPSFile="longterm_$host\_$TestRun\_$startlessone.ps" ;
    print "History PS file should be $HistoryPSFile\n" ;
    if (-f "$rawpsdir/$HistoryPSFile") {
	print "  found and copying it...\n" ;
	&CopyFile("$rawpsdir/$HistoryPSFile","$TestArchiveDir/$HistoryPSFile") ;
    }
    else {
	$startlessnone=$StartScan ;
	$HistoryPSFile="longterm_$host\_$TestRun\_$startlessnone.ps" ;
	print "  not there - so try $HistoryPSFile\n" ;
	if (-f "$rawpsdir/$HistoryPSFile") {
	    print "  found and copying it...\n" ;
	    &CopyFile("$rawpsdir/$HistoryPSFile","$TestArchiveDir/$HistoryPSFile") ;
	}
	else {
	    $HistoryPSFile="longterm_$TestRun\_$startlessone.ps" ;
	    print "  not there - so try $HistoryPSFile\n" ;
	    if (-f "$rawpsdir/$HistoryPSFile") {
		print "  found and copying it...\n" ;
		&CopyFile("$rawpsdir/$HistoryPSFile","$TestArchiveDir/$HistoryPSFile") ;
	    }
	    else {
		$startlessnone=$StartScan ;
		$HistoryPSFile="longterm_$TestRun\_$startlessnone.ps" ;
		print "  not there - so try $HistoryPSFile\n" ;
		if (-f "$rawpsdir/$HistoryPSFile") {
		    print "  found and copying it...\n" ;
		    &CopyFile("$rawpsdir/$HistoryPSFile","$TestArchiveDir/$HistoryPSFile") ;
		}
	    }
	}
    }
}
 
print "Test log files and standard result files have been saved in\n" ;
print " $TestArchiveDir\n" ;
if ($AnyURLs) {
    print "Postscript files have also been copied\n" ;
}
else {
    print "No PS files have been copied: you should copy them by hand\n" ;
}

print "Now backing up the raw data files...\n" ;
system "perl BackupRawData.pl $TestRun" ;

print "\n$0 execution complete\n" ;
exit 0 ;

