#!/bin/csh -f
#-----------------------------------------------------------------------
# send email to murat@fnal.gov before modifying this script
# This script runs stnmaker.exe on stream A raw data files which have gone 
# through EXPRESS production.
#-----------------------------------------------------------------------
set val_dir=/cdf/scratch/cdfopr/val
set logfile=${val_dir}/results/stnmaker_cronjob.log
set express_list=${HOME}/LIST_OF_PROCESSED_RUNS
set output_dir=
set temp_file1=/tmp/cdfopr.1.$$
set temp_file2=/tmp/cdfopr.2.$$
set temp_file3=/tmp/cdfopr.3.$$
set rc=0

#------------------------------------------------------------------------------
# Find first run in run list which has not been processed yet by stnmaker:
#  exit if there is nothing to do
#------------------------------------------------------------------------------
set run_hex=`cat ${express_list} | awk '{if (substr($0,1,1) != "#") print $0}' | grep 'run ' | grep 0x | grep -v done | grep -v runs | grep -v fail | head -1 | awk '{name = substr($2,10,5);print name}'` 

if ( ${run_hex} == "" ) then
  echo `date`:  No runs to process. >> $logfile
  set rc=0
  goto CLEANUP_AND_EXIT;
endif
set run_decimal=`printf "%i\n" 0x${run_hex}`

echo `date`: starting new job for run = $run_decimal  >> $logfile
#------------------------------------------------------------------------------
# If a *.stn or *.log file already exists in the output directory then don't 
# start a job on this run. Probably an stnmaker.exe job is currently running 
# for this run number.
#------------------------------------------------------------------------------
set n_running_jobs=`cat $express_list | grep runs | wc -l`
echo `date`:  n_running_jobs = $n_running_jobs  >> $logfile
if ( $n_running_jobs >= 4 ) then
  echo `date`:  too many running jobs : $n_running_jobs, exit  >> $logfile
  set rc=0
  goto CLEANUP_AND_EXIT;
endif
#------------------------------------------------------------------------------
# Get valid kerberos ticket and setup CDF software
#------------------------------------------------------------------------------
${HOME}/ticket >& /dev/null
cd $val_dir

# Redirect output of next command to /dev/null so cdfopr@fcdflnx3 doesn't 
# get email each time cron job runs.

( source $HOME/scripts/.source_me.csh ) >& /dev/null

# Build string to be entered into log file of this cron job.
set run_string="run ${run_decimal}(0x${run_hex})"
#------------------------------------------------------------------------------
# Run the executable to make the stntuple. The tcl is hardcoded to run on 
# files from the given run in the area fcdfsgi2:/cdf/data01/stream_a
#------------------------------------------------------------------------------
echo `date`: starting new job $PWD/run_stnmaker.sh $run_hex  >> $logfile

    cp $express_list $temp_file1
    sed s/"${run_decimal}(0x${run_hex})"/"${run_decimal}(0x${run_hex})  - runs"/ $temp_file1 > $temp_file2

    # Be sure we don't overwrite original run list with empty file:

    set original_size=`ls -l $express_list | awk '{print $5}'`
    if ( ( -e $temp_file2 )  && ( ! -z $temp_file2 ) ) then
      set new_size=`ls -l $temp_file2 | awk '{print $5}'`
    else 
      set new_size=0
    endif

echo `date`: replacing with "- runs"  new_size, original_size = $new_size $original_size  >> $logfile
    
    if ( $new_size > $original_size ) then

echo `date`: mv $temp_file2 $express_list   >> $logfile

      mv $temp_file2 $express_list
      set rc = $?

echo `date`: `ls -l $express_list`  rc = $rc   >> $logfile

    endif

    rm -f $temp_file1

./run_stnmaker.sh ${run_hex}
set rc=$status
#-----------------------------------------------------------------------
#  stntuple maker job has finished, update LIST_OF_PROCESSED_RUNS
#  make sure update_list is not running
#-----------------------------------------------------------------------
CHECK_LOCK:
if ( -f $HOME/locks/update_list.lock ) then
  echo `date`:  update_list lock file detected, waiting  >> $logfile
  wait 60
  goto CHECK_LOCK;
endif

echo `date`:  job finished with rc = $rc  >> $logfile

chmod u+w $express_list

if ( $rc != 0 ) then
#------------------------------------------------------------------------------
#  rc != 0 - job failed
#  write fail entry into cron job log file:
#  and add the string "- fail" next to this run in the list of processed runs
#------------------------------------------------------------------------------
  echo "${run_string}: ERROR -- stnmaker did not exit successfully. return code = ${rc}." >> ${logfile}

    cp ${express_list} ${temp_file1}
    sed s/"${run_decimal}(0x${run_hex})  - runs"/"${run_decimal}(0x${run_hex})  - fail"/ ${temp_file1} > ${temp_file2}
    # Be sure we don't overwrite original run list with empty file:

    set original_size=`ls -l $express_list | awk '{print $5}'`
    if ( ( -e $temp_file2 )  && ( ! -z $temp_file2 ) ) then
      set new_size=`ls -l $temp_file2 | awk '{print $5}'`
    else 
      set new_size=0
    endif

echo `date`: replacing with "- fail"  new_size, original_size = $new_size $original_size  >> $logfile
    
    if ( $new_size == $original_size ) then
      mv $temp_file2 $express_list
    endif

    rm -f $temp_file1
else
#------------------------------------------------------------------------------
#  rc = 0 - job finished successfully
#  if output stntuple file exists, print success message in cronjob log file
#  and update LIST_OF_PROCESSED_RUNS to mark run as "done"
#------------------------------------------------------------------------------
  if ( -e ${val_dir}/results/*${run_hex}*.stn ) then
    set num_events=`cat ${val_dir}/results/*${run_hex}*.log | grep FillStntuple | awk '{if ( ($1 == "FillStntuple")  && (NF == 5)) print $2}'`

    echo "${run_string}: stntuple created at `date +'%T, %b%d'`. Number of events = $num_events" >> $logfile

    cp $express_list $temp_file1
    sed s/"${run_decimal}(0x${run_hex})  - runs"/"${run_decimal}(0x${run_hex})  - done"/ $temp_file1 > $temp_file2
    rm -f $temp_file1
#-----------------------------------------------------------------------
# Be sure we don't overwrite original run list with empty file:
#-----------------------------------------------------------------------
    set original_size=`ls -l $express_list | awk '{print $5}'`
    if ( ( -e $temp_file2 )  && ( ! -z $temp_file2 ) ) then
      set new_size=`ls -l $temp_file2 | awk '{print $5}'`
    else 
      set new_list_size=0
    endif

    echo `date`: replacing with "- done"  new_size, original_size = $new_size $original_size  >> $logfile
    
    if ( $new_size == $original_size ) then
      mv $temp_file2 $express_list
    endif
  else
    echo "${run_string}: ERROR -- stnmaker did not create an stntuple file" >> $logfile
  endif
endif
#-----------------------------------------------------------------------
#     cleanup and exit
#-----------------------------------------------------------------------
CLEANUP_AND_EXIT:

rm -f $temp_file2

chmod u-w $express_list

exit  $rc
