#!/usr/bin/perl
#
#
#  USAGE cd moduleDB; ./web_upd.pl -<options> -<dir>
#
#                  =================================
#  FULL ANALYSIS    ./web_upd.pl -A M0001/T-10a    
#                  =================================
#
#------------------------------------------------------
#                    OPTIONS                          #
# -----------------------------------------------------
# -p phCalibration
# -s SCurve
# -c chipSummaryPage
# -m moduleSummaryPage
# -t testSummaryPlot

# -C " ... " add your comment
# -S " ... " add the final grade (as marked on sheer)

# -w converts ps -> gif, produces html-files & transfers 
#  to kamor
#
# -A full analysis
# -----------------------------------------------------
  

use Getopt::Std;
getopts('C:G:ASpscmfw');

if ($opt_A) {  $opt_p=1; $opt_s=1; $opt_c=1; $opt_m=1; $opt_w=1; }    


$numArgs = $#ARGV + 1;
print "WEB UPDATE> $numArgs module test(s) will be processed.\n";

$kamor = "l\_tester\@kamor.ethz.ch";
$dbDir = "/home/l_tester/ptr/moduleDB";

foreach $argnum (0 .. $#ARGV) {

    $dirTmp   = $ARGV[$argnum];    
    $dirMacros = "../$dirTmp";
    $dirDB = $dirTmp;
    
    @mtmp = split(/\//,$dirTmp);
    $testNr=$mtmp[$#mtmp];  
    @ntmp = split(/\-/,$mtmp[$#mtmp-1]);
    $moduleNr=$ntmp[0];
    $name= $moduleNr."".$testNr;
    
    $dirKamor = $moduleNr."/".$testNr;
  
    # Comments  
    if ($opt_C) {
	
	if (-e "$dirTmp/comment.txt") { system("rm -f $dirTmp/comment.txt"); }

	open(COMM, ">$dirTmp/comment.txt") || die "==> Cannot open $dirTmp/comment.txt\n";
	print COMM "$opt_C \n";
	
	print "\nWEB UPDATE> Wrote comment \"$opt_C\" to file:\n";
	print "WEB UPDATE> $dirTmp/comment.txt ...\n\n";
	exit 0;
    }

    #Grades
    if ($opt_G) {

	system("./macros/gradeModule.pl -m $dirTmp -G $opt_G");
	exit 0;	
    }
    

    if(`ls -1 $dirDB/FullTest.root`) {
	
	chdir("macros");  
	
	if ( $opt_p || $opt_s || $opt_c ) {
	    
	    open(ROOT, "|root -l") || die "Cannot open ROOT\n";  
	    
	    print ROOT ".L PHCalibration.C\n";
	    if ($opt_p)  { print ROOT "FitAllCurves(\"$dirMacros\", true)\n"; }
	    
	    print ROOT ".L SCurve.C\n";
	    if ($opt_s)  { print ROOT "FitSCurves(\"$dirMacros\")\n"; }
	    
	    print ROOT ".L chipSummaryPage.C\n";
	    if ($opt_c)  { print ROOT "chipSummaries(\"$dirMacros\")\n"; }
	    
	    print ROOT ".q\n";
	    close(ROOT);
	    
	}
	
	
	
	if ($opt_m) {
	    
	    open(ROOT, "|root -l") || die "Cannot open ROOT\n";  
	    print ROOT ".L tempProfile.C\n"; 
	    print ROOT "readProfile(\"$dirMacros\")\n";
	    print ROOT ".q\n";
	    close(ROOT);
	    
	    system("./phpSummaryText.pl -m $dirTmp");    #produces text file with summary for php & mod.
	    
	    open(ROOT, "|root -l") || die "Cannot open ROOT\n";  
	    print ROOT ".L moduleSummaryPage.C\n";
	    print ROOT "moduleSummary(\"$dirMacros\")\n";
	    print ROOT ".q\n";
	    close(ROOT);
	}
	
	chdir("..");
    }
    
    else {
	
	if(-d $dirDB)  {  print "\nWEB UPDATE> ----> COULD NOT FIND FullTest.root IN DIRECTORY $dirDB ....\n\n";  next; }
	else {   print "\nWEB UPDATE> ----> $dirDB: NO SUCH DIRECTORY .... \n\n"; next;  }
	
    }
    
}

foreach $argnum (0 .. $#ARGV) {

    $dirTmp   = $ARGV[$argnum];    
    $dirMacros = "../$dirTmp";
    $dirDB = $dirTmp;
    
    @mtmp = split(/\//,$dirTmp);
    $testNr=$mtmp[$#mtmp];  
    @ntmp = split(/\-/,$mtmp[$#mtmp-1]);
    $moduleNr=$ntmp[0];
    $name= $moduleNr."".$testNr;
    
    $dirKamor = $moduleNr."/".$testNr;
    
    if(`ls -1 $dirDB/FullTest.root`) {
	
	chdir("macros");  
	
	if ($opt_m) {
	    
	    system("./overallGrade.pl -m $dirTmp");    #produces overall grade file
	    

	    if ( !(-e "$dirMacros/../finalGrade.txt") ) {
		
		print "WEB UPDATE> no final grade found, will scan db ... \n";
		system("./scanDBgrades.pl -m $dirTmp");
	    }

	    if ( (-e "$dirMacros/../finalGrade.txt") && (-e "$dirMacros/summaryTest.txt"))  {
		system("/bin/cat $dirMacros/../finalGrade.txt >> $dirMacros/summaryTest.txt");
	    }
	    
	    if ( (-e "$dirMacros/comment.txt") && (-e "$dirMacros/summaryTest.txt"))  {
		system("/bin/cat $dirMacros/comment.txt >> $dirMacros/summaryTest.txt");
	    }

	}  

	if ($opt_w) { 

	    system("./htmlSummaryPage.pl -m $dirMacros");    #produces html-page for one module 
	    
	    @psFiles = `ls -1 $dirMacros/mod*.ps`;
	    foreach $n (0 .. $#psFiles) {                     #convert ps to gif
		
		$ps  =  $psFiles[$n];
		chop($ps);
		
		$gif =  $psFiles[$n];
		$gif =~ s/chipSummary\_//;
		$gif =~ s/moduleSummary\_//;
		$gif =~ s/ps/gif/;
		chop($gif);
		
		print "WEB UPDATE> Converting $ps to $gif ....\n"; 
		system("convert -rotate 90 $ps $gif \n");
	    }
	    
	    ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks)
		= stat("$dirMacros/summaryTest.txt");
	    
	    if ( $size!=0 ) {
		
		@path = split(/\//, $dirKamor);
		
		$fullPath = "/export/data1/moduleTests/moduleDB" ;   #create target directory for html-files on kamor
		
		foreach $p (0 .. $#path) {  
		    
		    $fullPath = "$fullPath/$path[$p]";
		    print "WEB UPDATE> Creating directory $fullPath on kamor ....\n";
		    system("ssh $kamor mkdir $fullPath"); 
		    
		}
		
		system("scp $dirMacros/*.html $kamor:/export/data1/moduleTests/moduleDB/$dirKamor");   #datatransfer
		system("scp $dirMacros/../*.gif $kamor:/export/data1/moduleTests/moduleDB/$dirKamor/../");
		system("scp $dirMacros/*.gif $kamor:/export/data1/moduleTests/moduleDB/$dirKamor");
		system("scp $dirMacros/summaryTest.txt $kamor:/export/data1/moduleTests/moduleDB/$dirKamor");
	    }
	    
	    
	}
	
	chdir("..");
    }
    
    else {
	
	if(-d $dirDB)  {  print "\nWEB UPDATE> ----> COULD NOT FIND FullTest.root IN DIRECTORY $dirDB ....\n\n";  next; }
	else {   print "\nWEB UPDATE> ----> $dirDB: NO SUCH DIRECTORY .... \n\n"; next;  }
	
    }
    
}


if ($opt_S) {

    chdir("$dbDir/summAna");
    system("./runAna -c -d ../SUMMARY");

    chdir("$dbDir");
	    
    @psFiles2 = `ls -1 SUMMARY/mod*.ps`;
    foreach $n (0 .. $#psFiles2) {                     #convert ps to gif
	
	$ps  =  $psFiles2[$n];
	chop($ps);
	
	$pdf =  $psFiles2[$n];
	$pdf =~ s/ps/pdf/;
	chop($pdf);
	
	print "WEB UPDATE> Converting $ps to $pdf ....\n"; 
	system("convert $ps $pdf \n");
	
    }
     

    print "WEB UPDATE> Creating directory SUMMARY on kamor ....\n";
    system("ssh $kamor mkdir /export/data1/moduleTests/moduleDB/SUMMARY"); 
    system("scp SUMMARY/*.pdf $kamor:/export/data1/moduleTests/moduleDB/SUMMARY");
	
}
 
