#!/bin/bash

#check if scramv1 is executed
if [ -z $CMSSW_BASE ]; then
    echo "Please run cmsenv first"
    exit
fi

#variables definition
CUR_DIR=$PWD
CMSSW_DIR=$CMSSW_BASE'/src/Visualisation/Frog/'
FROG_DIR=$CMSSW_DIR'./soft/'
REPLACE='Visualisation.Frog'
Cosmics_Raw_cfg='Frog_Analyzer_CosmicsFrowRaw_cfg.py'
Cosmics_Reco_cfg='Frog_Analyzer_CosmicsFrowReco_cfg.py'
frog_config='Frog_config.txt'
script_name="frogFilter.ch"
run=0
#data_set="/Cosmics/Commissioning08-PromptReco-v2/RECO" 
data_set="/Cosmics/Commissioning08_CRAFT_ALL_V4_ReReco-v1/RECO" 
#data_set="/Cosmics/Commissioning08_CRAFT_ALL_V4_SuperPointing_SuperPointing_v4/RAW-RECO" 
#filter="filters/listNoiseEv.C"
filter=""
cmsRun=0

while [ $# -ge 2 ]; do
    case $1 in
	--help )
	    cat help/help_short
	    exit;;
	-cmsRun )
	    cmsRun=1
	    shift 1;;
	-r )
	    shift 1
	    run=$1	    
	    shift 1;;
	-d )
	    shift 1
	    data_set=$1
	    shift 1;;
	-f ) 
	    shift 1
	    filter=$1
	    shift 1;;
	-D)
	    shift 1
	    dataset=$1
	    run=-1
	    sample_name="$data_set""_AllRuns"
	    shift 1
	    break;;
	* )
	    shift 1;;
	esac
done

if ! [ "$filter" ]; then
    echo "$script_name : No filter given"
    echo "$script_name : -f <path to filter>"
    echo "$script_name : Stop"
    exit
fi
if ! [ -f "$filter" ]; then 
    echo "$script_name : Filter $filter doesn't exist"
    echo "$script_name : Stop"
    exit
fi

#define all filenames
if ! [ $sample_name ]; then
   sample_name="$data_set""_Run_""$run"
fi 
sample_name=$(echo "$sample_name" | sed -e 's:/:_:g')
sample_name=$(echo "$sample_name" | sed -e 's:-:_:g')
sample_name=$(echo "$sample_name" | sed -e 's:__:_:g')
config_file=$sample_name"_cfg.py"
filelist=$sample_name".txt"
cff_file=$sample_name"_cff"
cff_file_py=$sample_name"_cff.py"
cff_present=0
filtername=$(echo $filter | awk -F / '{print $NF}' | awk -F . '{print $1}')

#define working directory
if [ $run -eq 0 ]; then
    working_dir="$CUR_DIR/Last/"$data_set"/"$filtername"/"
    if [ -d $working_dir ]; then
	rm -rf  $working_dir/*
    fi
elif [ $run -lt 0 ]; then
    working_dir="$CUR_DIR/All"$data_set"/"$filtername"/"
else
    working_dir=$CUR_DIR"/Run_"$run$data_set"/"$filtername"/"
fi

#choosing the cfg.py file to run
if  [ $(echo $data_set | grep "RAW") ]; then
    cfg="Frog_Analyzer_CosmicsFrowRaw_cfg.py"    
elif [ $(echo $data_set | grep "RECO") ]; then
    cfg="Frog_Analyzer_CosmicsFrowReco_cfg.py"
else 
    echo "$script_name : Don't know which cfg file to run. Will use default one:"
    cfg="Frog_Analyzer_CosmicsFrowRaw_cfg.py"
fi

#getting list of files if needed
if [ -f $filelist ]; then
    rm $filelist
fi


if [ $run -eq 0 ]; then
    ./get_files.py -d $data_set -f $filelist -txt
    if ! [ -f _tmp_last_run_ ]; then
	echo "$script_name : File _tmp_last_run_ doesn't exist "
	echo "$script_name : Can't found the last run for dataset $data_set"
	echo "$script_name : Please choose one with -r option, or retry later"
	echo "$script_name : stop"
	exit
    else
	run=$(head -n 1 _tmp_last_run_)
	rm _tmp_last_run_
	sample_name=$(echo $sample_name | sed -e 's:Run_0:Run_'$run':g')
	mv $filelist $sample_name".txt"
	config_file=$sample_name"_cfg.py"
	cff_file=$sample_name"_cff"
	cff_file_py=$sample_name"_cff.py"
	if [ -d $working_dir ]; then
	    rm -rf $working_dir/*
	fi
    fi
fi

if ! [ -f  $working_dir/$cff_file_py ]; then
    if [ $run -lt 0 ]; then
	./get_files.py -D $data_set -f $filelist -txt
    elif [ $run -gt 0 ]; then
	./get_files.py -r $run -d $data_set -f $filelist -txt
    fi

    if ! [ -f $filelist ]; then
	echo "$script_name : $filelist : No such file"
	echo "$script_name : Stop"
	exit
    fi
fi

mkdir -p $working_dir

if [ -f $filelist ]; then
    mv  $filelist $working_dir/.
fi

if [ $run -ge 0 ]; then
    echo "$script_name : Run: $run"
else
    echo "$script_name : Run: ALL"
fi
echo "$script_name : Dataset: $data_set"
echo "$script_name : List of root files: $filelist"
echo "$script_name : Filter : $filter"
echo "$script_name : base cfg file : $cfg"
echo "$script_name : cfg file to run : $config_file"
echo "$script_name : working dir : $working_dir"

if ! [ -f ./cfg/$cfg ]; then
    echo "$script_name : ./cfg/$cfg : No such file"
    echo "$script_name : Stop"
    exit
fi

cd $working_dir
if [ "$( ps -eo command | grep $working_dir | grep -v grep )" ]; then
    echo "$script_name : Several processus found working in directory "
    echo "$script_name : $working_dir"
    echo " PID  User    START   TIME   COMM"
    ps -eo pid,user,start,time,command | grep $working_dir | grep -v grep | awk '{print $1,$2,$3,$4,$5}'
    echo "$script_name : Running cmssw or frog may interfere.Please wait than the processus finished"
    echo "$script_name : Stop"
    exit
fi

#Generating cff file
#head
echo "import FWCore.ParameterSet.Config as cms" > $cff_file_py
echo "" >>  $cff_file_py
echo "maxEvents = cms.untracked.PSet( input = cms.untracked.int32(-1) )" >> $cff_file_py
echo "readFiles = cms.untracked.vstring()"  >> $cff_file_py
echo "eventlist = cms.untracked.VEventID()" >> $cff_file_py
echo "source = cms.Source ('PoolSource',fileNames = readFiles, eventsToProcess = eventlist)" >>  $cff_file_py
echo "readFiles.extend( [" >> $cff_file_py

#scan root files and create a list of events
#Need to redefine to make stager_qry and stager_get work properly
export STAGE_SVCCLASS=\*
total_files=$(( $(wc -l $filelist | awk '{print $1}') - 1 ))
echo "$script_name : Scanning root files from $filelist"
files_scanned=0
staged=0
stagein=0
stageout=0
canbemigr=0
invalid=0
if [ -f eventlist.log ]; then rm eventlist.log; fi
if [ -f $filtername\_error.log ]; then rm $filtername\_error.log; fi
for file in `cat $filelist`; do
    files_scanned=$(( $files_scanned + 1 ))
    #get file status, and if staged process it
    counter=0
    while [ $counter -le 10 ]; do
	status=$(stager_qry -M /castor/cern.ch/cms$file | awk 'NR == 2 {print $3}')
	echo -e -n " [$files_scanned/$total_files] status:  $status         \r"
	case $status in
	    "STAGED" )
		#Processing file
		echo "   '$file'," >>  $cff_file_py
		unset STAGE_SVCCLASS
		#root -b -q -l $CUR_DIR/$filter\(\"$file\"\) 2> $filtername\_error.log | grep \* 1>> eventlist.log
		root -b -q -l $CUR_DIR/$filter\(\"$file\"\) 2> $filtername\_error.log | cat  1>> eventlist.log
		export STAGE_SVCCLASS=\*
		staged=$(($staged + 1))
		break;;
	    "STAGEIN" )
	        #wait 10 seconds
		sleep 10;;
	    "STAGEOUT" )
		#query file and wait 10 seconds
		stager_get -M /castor/cern.ch/cms$file
		sleep 10;;
	    "CANBEMIGR" )
		#query file and wait 10 seconds
		stager_get -M /castor/cern.ch/cms${file} >> 1;
		sleep 10;;
	    "INVALID" )
	        #skip file
		invalid=$(( $invalid + 1 ))
		break;;
	    *)
		#unknoun status, wait 10 sec
		sleep 10;;
	esac;
	counter=$(( $counter + 1 ))
    done
    case $status in
	"STAGEIN" )
	    stagein=$(($stagin + 1));;
	"STAGEOUT" )
	    stageout=$(($stageout+1));;
	 "CANBEMIGR" )
	    canbemigr=$(($canbemigr+1));;
    esac;
done

echo ""
echo "STAGED : $staged of $total_files"
echo "STAGEIN : $stagein of $total_files"
echo "STAGOUT : $stageout of $total_files"
echo "CANBEMIGR : $canbemigr of $total_files"
echo "INVALID : $invalid of $total_files"

if [ $staged -eq 0 ]; then 
    echo "$script_name : No processed files."
    echo "$script_name : Stop"
    exit
fi

echo "] );" >> $cff_file_py
echo "" >> $cff_file_py
echo "eventlist.extend( ["  >> $cff_file_py
echo "Event list generated"
awk -F* ' /^#/ {print $0}(NF > 4)&&($2~/[0-9]/)  {print "\""$3":"$4"\","} ' eventlist.log >> $cff_file_py
echo "])" >> $cff_file_py

cp $cff_file_py $CMSSW_DIR/python/.
sed -e 's:'$REPLACE'.files_list_cfi:'$REPLACE'.'$cff_file':g' $CUR_DIR/cfg/$cfg | sed -e 's:output.vis:'$sample_name'.vis:g' > $working_dir/$config_file

#Run cmsRun if asked with -cmsRun option
if [ $cmsRun -eq 1 ]; then
    cp $working_dir/$config_file $CMSSW_DIR/python/_tmp_cfg.py
    cd $CMSSW_DIR
    echo "Compiling CMSSW"
    scramv1 b
    
    cd $working_dir
    
    if [ -f $working_dir/cmssw_out.log ]; then
	rm $working_dir/cmssw_out.log
    fi
    
    xterm -T "cmsRun $data_set Run: $run" -e  "cmsRun $CMSSW_DIR/python/_tmp_cfg.py 2>&1 | $CUR_DIR/src/log_filter.py | tee $working_dir/cmssw_out.log"&
fi

exit
