#!/usera/local/bin/perl -w

use strict;

# script to launch the java sctdaq SCT database uploader application
# Dave R 23/04/2003
# v2 21/05/2003 DR Add -nogui' option
# v3 06/06/2003 DR Allow for _runno appended to result files
# v4 18/11/2003 DR allow for partial regular expressions (.*) in serial number

# This script is a simple wrapper to a java application.
# To run the java application, you must include the following in your
# CLASSPATH environment variable
# - jSCTDAQ.jar (downloadable from http://www.hep.phy.cam.ac.uk/~silicon/jSCTDAQ.html
# - classes111.zip (downloadable from Geneva database website)
# - SCTTEST_3_nn_P.jar where nn=17 or higher (downloadable from Geneva database website)
#
# Up to 4 arguments (or no arguments) can be supplied with this script
# - serial number or result filename 
# eg 'perl uploadSCTDAQ.pl 20220330200002' or 'perl uploadSCTDAQ.pl 20220330200002_20030812.txt'
# - a directory containing the result files IF not in the standard RESULTS_DIURECTORY
#   DO NOT enter a directory name if your result files are in the normal RESULTS_DIRECTORY
# - the word 'hybrid' to indicate that the result file contains hybrid data (not module)
# - '-noupload' to prevent the actual upload to the database
#    If you use this option, it is your responsibility to upload the created database files
#    using the Geneva 'java UploadTestData' utility
#    eg 'cd your_upload_directory' (change to the directory containing the upload files)
#    then  'java UploadTestData *.sdb username password' (do the upload)
# - '-nogui' to prevent the appearance of gui and prevent upload to database
#    (ie just creates all upload files without further prompting)
# - '-c "Test Comment"' to upload the comment "Test Comment" with every test


#Linux - uncomment the next two lines if you are running on Linux, and make
# sure the definitions are as defined in sctdaq/macros/parameters.h
#my $RESULTS_DIRECTORY = "../sctvar/results/";
#my $UPLOAD_DIRECTORY = "../sctvar/upload/";

#Windows - make sure these are as defined in sctdaq/macros/parameters.h
# If not using Windows, commment out the next two lines and uncomment the 
# above lines appropriate for Linux
my $RESULTS_DIRECTORY = "D:\\sctvar\\results\\";
my $UPLOAD_DIRECTORY = "D:\\sctvar\\upload\\";

########################################### Do not edit anything below this line

my $nargs = (@ARGV);
if($nargs>6) {
  print "Up to 6 arguments are permitted:\n";
  print " - the result file name or serial number\n";
  print " - a directory containing the result file(s) as an alternative to RESULTS_DIRECTORY\n";
  print " - the word 'hybrid' to flag that the result file(s) contain hybrid data, not modules\n";
  print " - the '-noupload' flag OR the '-nogui' flag\n";
  print "You can use any or none of the above.\n";
  die;
  }

my $noupload="";
my $archive="null";
my $resultfile="null";
my $hybrid="";
my $commentArgument=0;
my $cmt="";


foreach (@ARGV) {
  if($commentArgument>0) {
     $cmt ="-c \"$_\"";
     $commentArgument=0;
     }
  if(-d && -e ) {$archive=$_;}
  if(/\-noupload/) {$noupload=$_;}
  if(/\-nogui/) {$noupload=$_;}
  if(/([\d\.\*]{14})/) {$resultfile=$1;}
  if(/(\d{14}_\d{8}[_0-9]?\.txt)/) {$resultfile=$1;}
  if(/hybrid/) {$hybrid="hybrid";}
  if(/^-c$/) {$commentArgument=1;}
  }
print "java DBUpload/uploadSCTDAQ $RESULTS_DIRECTORY $UPLOAD_DIRECTORY $archive $resultfile $hybrid $noupload $cmt\n";
system("java DBUpload/uploadSCTDAQ $RESULTS_DIRECTORY $UPLOAD_DIRECTORY $archive $resultfile $hybrid $noupload $cmt");

