#!/usr/bin/env bash
#-----------------------------------------------------------------------
# example:
#
#    submit_concatenator -j stntuple -b cdfpewk -d tewk0e -s 1:29  \
#    -o ewk@fcdfdata030:/cdf/scratch/ewk/datasets/cdfpewk/tmet0d          \
#    -r /cdf/opr2/cdfopr/mcProduction/tewk0e -m caf
#
# cdfopr/scripts/submit_concatenator -v -s 1:52 -b cdfpewk -d sewk05 \
#                -t /cdf/opr/cdfopr/tarballs/root_5.3.3_physics.tgz  \
#                -p 2004_09_12.20_28_04 -j stntuple -m caf
#
#  use FCP to transfer the files - SCP / RCP from time to time fail to 
#  authenticate
#
#  defaults: 
#  --------
# -b [ book ]       : cdfpewk
# -m [ mode ]       : caf [local]
# -j [job_type]     : prod [stntuple]
# -o [ output_dir ] : fcdfsgi2:/cdf/data36/ewk/datasets/cdfpewk/$DATASET )
# -r (remote_tcl_dir: fcdflnx3:/cdf/opr2/cdfopr/datasets/cdfpewk/$DATASET
#------------------------------------------------------------------------------
               BOOK=cdfpewk
            DATASET=0
concat_file_pattern=2004
       job_sections=1
                job=prod
               mode=caf
     remote_tcl_dir=""
        tarball_dir=
         output_dir=
            nevents=0
            tarball="undefined"
            release=`cat .base_release`
     export VERBOSE=""
#-----------------------------------------------------------------------            
export OPTIND=1	
while getopts :b:d:j:m:n:o:p:r:s:t:vw:x: OPT; do
    case $OPT in
        b)                                          # input dataset name
	    export  BOOK=$OPTARG
            if [ .$DEBUG_SCRIPT != "." ] ; then 
              echo   BOOK=$OPTARG
            fi
	    ;;
        d)                                          # input dataset name
	    export  DATASET=$OPTARG
            if [ .$DEBUG_SCRIPT != "." ] ; then echo DATASET=$OPTARG ; fi
	    ;;
        j)                                          # job: prod/stn
	    export  job=$OPTARG
            if [ .$DEBUG_SCRIPT != "." ] ; then echo job=$OPTARG ; fi
	    ;;
        m)                                          # mode: caf/not caf
            mode=$OPTARG
            ;;
        n)                                          # mode: caf/not caf
            nevents=$OPTARG
            ;;
        o)                                          # output dir
            output_dir=$OPTARG
            ;;
        p)                                          # concat_file_pattern
            concat_file_pattern=$OPTARG
            ;;
        r)                                          # remote tcl dir
            remote_tcl_dir=$OPTARG
            ;;
        s)                                          # sections
	    job_sections=$OPTARG
	    ;;
        t)                                          # non-default tarball
	    tarball=$OPTARG
	    ;;
        v)                                          # debug
	    export DEBUG_SCRIPT=1
	    export VERBOSE=1
	    ;;
        *)
           echo OTHER: $OPT $OPTARG
           usage
           ;;
    esac
done

if [ .$output_dir == "." ] ; then 
   ruser=cdfopr
   rhost=fcdfsgi2.fnal.gov
   rdir=/cdf/data36/ewk/datasets/$BOOK/$DATASET
elif [ $output_dir == "fcdfsgi2" ] ; then
   ruser=cdfopr
   rhost=fcdfsgi2.fnal.gov
   rdir=/cdf/data36a/ewk/datasets/$BOOK/$DATASET
else
  . cdfopr/scripts/parse_output_dir $output_dir
  export ruser=$OP1
  export rhost=$OP2
  export  rdir=$OP3
fi

output_node=${ruser}@${rhost}
 output_dir=$rdir

if [ .$remote_tcl_dir == "." ] ; then
  remote_tcl_dir=/cdf/opr2/cdfopr/datasets/$BOOK/$DATASET
fi

if [ .$tarball_dir == "." ] ; then
  if   [ `hostname -f` == "fcdflnx3.fnal.gov" ] ; then
    tarball_dir=/cdf/opr/cdfopr/tarballs
  elif [ `hostname -f` == "ncdf131.fnal.gov"  ] ; then
    tarball_dir=/cdf/data01/tarballs
  fi
fi

. cdfopr/scripts/parse_job_sections $job_sections
sec1=$OP1
sec2=$OP2
#-----------------------------------------------------------------------
#  input tcl files - on FCDFLNX3
#  concat file pattern: tcl file name = $concat_file_pattern.001 etc
#  remote tcl dir so far assumed to be on FCDFLNX3
#-----------------------------------------------------------------------
if [ .$CAF_CURRENT == ".cafcondor" ] ; then queue=short ; else queue=cdfopr ; fi

if [ $job == "prod" ] ; then
#-----------------------------------------------------------------------
# concatenate output of production
#-----------------------------------------------------------------------
  if [ $tarball == "undefined" ] ; then 
      tarball=$tarball_dir/stnmaker_${release}.tgz ; 
  fi
  exefile=bin/$BFARCH/stnmaker.exe

  parameters="-e $exefile -j stnmaker.concat"
  parameters=${parameters}" -d $DATASET "
  parameters=${parameters}" -x PATTERN=$concat_file_pattern"
  parameters=${parameters}" -x REMOTE_TCL_DIR=$remote_tcl_dir"

  if [ $nevents != 0 ] ; then parameters=${parameters}" -n $nevents" ; fi

  echo       mode=$mode
  echo parameters=$parameters

elif [ $job == "stntuple" ] ; then
#-----------------------------------------------------------------------
# concatenate stntuples
#-----------------------------------------------------------------------
  if [ $tarball == "undefined" ] ; then 
    tarball=$tarball_dir/root_${release}.tgz ; 
  fi
  script=cdfopr/scripts/process_concatenation_request
  remote_dir=fcdflnx3.fnal.gov:/cdf/opr2/cdfopr/datasets/$BOOK/$DATASET
  parameters="-v -b $BOOK -d $DATASET -s $script -j root.concat -I $remote_dir/$concat_file_pattern"
fi
#-----------------------------------------------------------------------
# finally submit the job
#-----------------------------------------------------------------------
if [ $mode == "local" ] ; then
  ./cdfopr/scripts/run.sh -v -J $sec1 $parameters
elif [ $mode == "caf" ] ; then
  parameters="-v -J $ "${parameters}" -o ${output_node}:$output_dir"
  if [ .$VERBOSE != "." ] ; then 
    echo Cafcom $tarball icaf:concat_$DATASET.$.tgz cdfopr@fnal.gov \
                $queue $sec1 $sec2 "./cdfopr/scripts/run.sh $parameters"
  fi

  Cafcom $tarball icaf:concat_$DATASET.$.tgz cdfopr@fnal.gov \
         $queue $sec1 $sec2 "./cdfopr/scripts/run.sh $parameters"
fi