#!/usr/bin/perl

#
#  script to generate a list of files for the production
#
#  Major     : [Alpha, Beta, Gamma]
#  Minor     : [A, B, C]
#  RunNum    : [1,2,3..1000...]
#  FirstSeed, LastSeed: [1-10000, 120000-13000]
#  StandardConfig: [stdA, stdB....]
# 
# Simu supported options: 
#    --of   = filename Alpha_C_R8678_S456_LGe
#    --odir = output
#    --seed = 456
#    --run  = 8678
#    --usrcfg = Alpha_C.cfg    # major_minor
#    --stdcfg = stdA
#    
#
#  First event handling
#    
#  Run Dependant parameters
#  
#  syntax: --ClassName.VarName=Value
#


sub usage;

use integer;
use Getopt::Long qw(:config pass_through no_ignore_case);

#my $data = "file.dat";
#my $length = 24;
#my $verbose;

#$result = GetOptions ("length|size=i" => \$length,    # numeric
#                      "file=s"   => \$data,      # string
#                      "verbose"  => \$verbose);  # flag

my $name           = '';
my $num_ev_tot     = 1;
my $num_ev_per_run = 0;#200;
my $out_dir        = '';
my $std_config     = '';
my $user_config    = '';
my $user_dir       = "usrcfgs/";
my $run_num        = 0;
my $first_seed     = 0;
my $in_mod         = '';
my $in_1st_ev      = 0;
my $in_file        = '';
my $loc            = '';

GetOptions ("file-name|o=s"   => \$name,
            "dir|d=s"         => \$out_dir,
            "std-cfg|c=s"     => \$std_config,
            "user-cfg|u=s"    => \$user_config,
            "user-dir=s"      => \$user_dir,
            "run|r=i"         => \$run_num,
            "init-seed|s=i"   => \$first_seed,
            "in-1st|f=i"      => \$in_1st_ev,
            "in-file=s"       => \$in_file,
            "in-mod|i=s"      => \$in_mod,
            "nev|N=i"         => \$num_ev_tot,
            "nev-per-run|n=i" => \$num_ev_per_run,
            "location|l=s"    => \$loc
            );

# Command line check ##################################
if ( !$name && $user_config ) {
    $name = $user_config;
}

if (!($name and $first_seed and $run_num)) {
    usage();
    exit 0;
}

my $seed = $first_seed+$run_num-1;

my $n_runs;
if ( $num_ev_per_run ) {
    $n_runs = $num_ev_tot / $num_ev_per_run;
    if ( $num_ev_tot%$num_ev_per_run )  {$n_runs++; }
} else {
    $n_runs = 1;
}

# Header ##############################################

print "#!/bin/sh\n";
print "# Number of runs $n_runs\n";

# Main loop ###########################################
for($i=0; $i<$n_runs; $i++) {

    my $run_name = sprintf("$name\_R%03d_S".$seed,($run_num+$i));
    !$loc or $run_name .= "_L$loc";
    
    my $filename = "$run_name";

    my $commandline = "./bin/i686/Simu -b";
    my $curr_ev = $i*$num_ev_per_run; 

# standard flags
    $commandline .= " --ofile=$filename --runname=$run_name";

# optional flags
    my $num_ev;
    if ( $num_ev_per_run ){
        if ( $num_ev_tot-$curr_ev >= $num_ev_per_run ) {
            $num_ev = $num_ev_per_run;
        } else {
            $num_ev = $num_ev_tot%$num_ev_per_run;
        }
    } else {
        $num_ev = $num_ev_tot;
    }

    $commandline .= " --events=$num_ev"; 
     
    !$seed           or $commandline .= " --seed=$seed";
    !$std_config     or $commandline .= " --stdcfg=$std_config"; 
    !$user_config    or $commandline .= " --usrcfg=$user_dir$user_config.cfg";
    !$out_dir        or $commandline .= " --odir=$out_dir";
    if ( $in_1st_ev ) {

        my $curr_ev = $in_1st_ev+$i*$num_ev_per_run;
        my $opt_ev = "";
        my $opt_file = "";
        if ( $in_mod eq "unish" ) {
            $opt_ev = " --UnisimFileShowerGenerator.FirstEvent";
            $opt_file = " --UnisimFileShowerGenerator.FileName";
        } elsif ( $in_mod eq "uniphp" ) {
            $opt_ev = " --FileUnisimLightToEuso.fFirstEvent";
            $opt_file .= " --FileUnisimLightToEuso.fFileName";
        } elsif ( $in_mod eq "phproot" ) {
            $opt_ev = " --PhPRootFileLightToEuso.fFirstEvent";
            $opt_file .= " --PhPRootFileLightToEuso.fFileName";
        } else {
            print "in-mod=$in_mod is not a valid option. Can be \"unish\" or \"uniphp\" or \"phproot\"\n";
            usage();
            exit 0;
        }

        $commandline .= $opt_ev."=".$curr_ev;
        !$in_file or $commandline .= $opt_file."=".$in_file;
        
    }




#    print $argnum;

    foreach $argnum (0 .. $#ARGV) 
    {
        $commandline .= " $ARGV[$argnum]";
    }

    print "$commandline\n";

    $seed++;
}

#    $filename = sprintf("$name_major\_$name_minor\_%d.root",$run_num+$i);
#for($i=0; $i<3; $i++) {
#    $filename = sprintf("$name_major\_$name_minor\_%d.root",$run_num+$i);
#    print "$filename\n";
#}

#$numArgs = $#ARGV + 1;
#print "thanks, you gave me $numArgs command-line arguments.\n";
#
#foreach $argnum (0 .. $#ARGV) 
#{
#  print "$ARGV[$argnum]\n";
#}

#print "Option -f $opt_f\n";


sub usage {
    print "Usage:\n";
    print "makescript.pl -o [name] -d [output dir] -u [user configuration] -c [standard config] -r [run number] -s [seed] -f [first event] -light=[uniphp | unish | phproot] -in-file=[name]\n";
}
