#!/usr/bin/env bash
#------------------------------------------------------------------------------
# example: cdfopr/scripts/submit_stnmaker_prod zewk0d 1 10 [caf] [queue]
#
#  edit the script to put in 
#  - right location of the tarball 
#  - queue
#  - logic defining output directory 
#  - user email address for CAF
#------------------------------------------------------------------------------
 dataset=$1
    job1=$2
    job2=$3
    mode="test";
    caf="local"; 
    if [ .$4 != "." ] ; then 
      mode=`echo $4 | awk -F : '{print $1}'` 
       x=`echo $4 | awk -F : '{print $2}'`; 
       if [ .$x != "." ] ; then caf=$x
       else                     caf=$mode  
       fi
    fi

if [ $caf == "cafcondor" ] ; then queue=medium ; else queue=medium ; fi
if [ .$5  != "."         ] ; then queue=$5 ; fi

echo $mode $caf
  prefix="";     if [ .$6 != "." ] ; then prefix=echo ; fi

tcl_file=
    host=`hostname -f`
     tar=stnmaker_prod_5.3.3_dev_242_16.tgz
    user=`klist | grep Default | awk '{print $3}' | awk -F @ '{print $1}'`

if [ ".$CDF_TARBALL_DIR" != "." ] ; then 
  tarball_dir=$CDF_TARBALL_DIR
else
  case $host in 
    "ncdf131.fnal.gov"  ) 
       tarball_dir=/cdf/data01/tarballs ;;
    "fcdflnx3.fnal.gov" ) 
       tarball_dir=/cdf/opr/cdfopr/tarballs ;;
    * )
       tarball_dir= ;;
  esac
fi

if [[ .$CAF_CURRENT == "." || .$CAF_CURRENT == ".caf" ]] ; then 
  if [ $user == "cdfopr" ] ; then
    queue=cdfopr
  else
    queue=medium
  fi
elif [ .$CAF_CURRENT == ".cafcondor" ] ; then 
  queue=medium ; 
else 
  queue=medium ; 
fi

tarball=$tarball_dir/$tar

       book=cdfpewk
output_node=cdfopr@fcdfdata053.fnal.gov
 output_dir=/cdf/scratch/cdfopr/datasets/$book/$dataset

input_dataset=$dataset

case $dataset in
    "bewk0d" )                                     # incl high-Pt electrons
        job=stnmaker.prod
        output_node=cdfdata@fcdfdata091
        output_dir=/cdf/scratch/cdfdata/ewk/stntuple/dev_242/$dataset/tmp
        queue=long
	;;
    "bewk2d" )                                     # incl high-Pt muons
        job=stnmaker.prod
        queue=long
	;;
    "gcrs0d" )                                     # zero-bias (0d-0e) ntupling
        job=stnmaker.prod
	;;
    "met25-ps100" )
        job=stnmaker.prod
	input_dataset="emet0d"
	tcl_file=cdfopr/tcl/stnmaker/prereq-met25-ps100.tcl
        queue=short
	;;
    "tmet0d" )                                     # W->tau nu strip
        job=stnmaker.prod
        input_dataset=etau0d
        tcl_file=cdfopr/tcl/stnmaker/prereq_taumet.tcl
        queue=short
	;;
    "wewk0d" )                                     # W->enu (calib)
        job=stnmaker.prod
	;;
    "wewk2d" )                                     # W->munu (calib)
        job=stnmaker.prod
	;;
    "wewk8t" )                                     # 5.3.3 W->taunu MC
        job=stnmaker.prod:mc
	;;
    "wewk9t" )                                     # 5.3.3 W->taunu MC v2
        job=stnmaker.prod:mc
	;;
    "wewkee" )
        job=stnmaker.prod:mc
	;;
    "mewkpp" )
        job=stnmaker.prod:mc
	;;
    "mewktp" )
        job=stnmaker.prod:mc
	;;
    "zewk0d" )                                     # Z->ee
        job=stnmaker.prod
	;;
    "zewk2d" )                                     # Z->mumu
        job=stnmaker.prod
	;;
    "zewk3m" )                                     # Z->mumu 5.3.3 MC
        job=stnmaker.prod:mc
	;;
    "zewkae" )                                     # Z->ee 5.3.3 MC
        job=stnmaker.prod:mc
	;;
    * )
esac

parameters="-e bin/$BFARCH/stnmaker_prod.exe -j stnmaker.prod "
parameters=${parameters}" -D $input_dataset -o ${output_node}:$output_dir"

output_tarball=${output_node}:$output_dir/stnmaker_prod_$dataset.$.tgz

if [ .$tcl_file != "." ] ; then
  parameters=${parameters}" -i $tcl_file "
fi

if   [ $mode == "test"     ] ; then 
  if [ $caf == "local" ] ; then
#-----------------------------------------------------------------------
#  local test job
#-----------------------------------------------------------------------
    $prefix ./cdfopr/scripts/run.sh -v -J $job1 $parameters -n 10
  else 
#-----------------------------------------------------------------------
#  test remote job
#-----------------------------------------------------------------------
    setup cdfsoft2 development

    if [ $caf == "caf"       ] ; then queue=test ; fi
    if [ $caf == "cafcondor" ] ; then queue=test ; fi

    $prefix CafSubmit --tarFile=$tarball             \
                      --outLocation=$output_tarball  \
                      --dhaccess=DFC                 \
                      --procType=$queue              \
                      --email=${user}@fnal.gov       \
                      --farm=$caf                    \
                      --start=1 --end=2              \
    "./cdfopr/scripts/run.sh -v -J $ $parameters -n 10 -x CAF=$caf" $
  fi
else
#-----------------------------------------------------------------------
#  remote job
#-----------------------------------------------------------------------
    setup cdfsoft2 development

    $prefix CafSubmit --tarFile=$tarball             \
                      --outLocation=$output_tarball  \
                      --dhaccess=DFC                 \
                      --procType=$queue              \
                      --email=${user}@fnal.gov       \
                      --farm=$caf                    \
                      --start=$job1 --end=$job2      \
    "./cdfopr/scripts/run.sh -v -J $ $parameters -x CAF=$caf" $

fi