#!/bin/bash
#
# #first initialize connection to Dirac by using:
# lhcb-proxy-init

PathToGangaFolder='/afs/cern.ch/user/r/rekopecn/gangadir/workspace/rekopecn/LocalXML'
NameTagForOutputFiles='B2Kstmumu'
#NameTagForOutputFiles='B2KstJpsi'
NameOfRootfileInGangaOutput=''

E_NO_ARGS=65
if [ $# -eq 5 ]  # Must have five command-line args to demo script.
then
   channel=$1
   job=$2
   year=$3
   magnet=$4
   datatype=$5
   echo "Collecting $NameTagForOutputFiles for $year$magnet"
else
  echo "Please invoke this script with one or more command-line arguments in the following format:"
  echo "./jobloader.sh CHANNEL JOB_ID YEAR MAGNET DATATYPE"
  exit $E_NO_ARGS
fi

if [ -e data ]
then
	echo "folder data/ found!"
else
	echo "creating folder data/"
	mkdir data
fi

if [ -e data/$datatype ]
then
	echo "folder data/$datatype found!"
else
	echo "creating folder data/$datatype"
	mkdir data/$datatype
fi


if [ -e data/$datatype/$channel ]
then
	echo "folder data/$datatype/$channel found!"
else
	echo "creating folder data/$datatype/$channel"
	mkdir data/$datatype/$channel
fi

if [ -e data/$datatype/$channel/$year$magnet ]
then
	echo "folder data/$datatype/$channel/$year$magnet found!"
else
	echo "creating folder data/$datatype/$channel/$year$magnet"
	mkdir data/$datatype/$channel/$year$magnet
fi

maxNumberOfSubjobs=0
if  [ "$datatype" = "data" ];
then
  maxNumberOfSubjobs=150
  echo "maxNumberOfSubjobs= $maxNumberOfSubjobs"
fi

if [ "$datatype" = "MC" ];
then
  maxNumberOfSubjobs=25
  echo "maxNumberOfSubjobs= $maxNumberOfSubjobs"
fi

if [ "$datatype" = "MCref" ];
then
  maxNumberOfSubjobs=15
  echo "maxNumberOfSubjobs= $maxNumberOfSubjobs"
fi

if [ "$datatype" = "PHSP" ];
then
  maxNumberOfSubjobs=15
  echo "maxNumberOfSubjobs= $maxNumberOfSubjobs"
fi


if [ "$datatype" = "MCB0" ];
then
  maxNumberOfSubjobs=15
  echo "maxNumberOfSubjobs= $maxNumberOfSubjobs"
fi


if [ "$datatype" = "MCK1" ];
then
  maxNumberOfSubjobs=15
  echo "maxNumberOfSubjobs= $maxNumberOfSubjobs"
fi


if [ "$datatype" = "MCInc" ];
then
  maxNumberOfSubjobs=15
  echo "maxNumberOfSubjobs= $maxNumberOfSubjobs"
fi


echo "Collecting $NameTagForOutputFiles for $datatype and $channel, $year$magnet location ..."
counter=0
prefix="root->"
apperfix=":::"

while [ $counter -lt $maxNumberOfSubjobs ]
do
   outfile=$year$magnet\_$NameTagForOutputFiles\_$counter.root
   
   if [ -e  $PathToGangaFolder/$job/$counter/output/\_\_postprocesslocations\_\_ ]
   then
      while read line
          do
	      line="${line#*$prefix}"
	      line="${line%%$apperfix*}"
	      NameOfRootfileInGangaOutput="${line##*/}"
	      echo "$datatype $channel $year $magnet $counter $line" >> "ListOfLocations.out"
		NameOfRootfileInGangaOutput="${line##*/}"
	   done < $PathToGangaFolder/$job/$counter/output/\_\_postprocesslocations\_\_
        #else echo "file $PathToGangaFolder/$counter/\_\_postprocesslocations\_\_ not found!"
     fi
   let counter=$counter+1
done
echo "Finished downloading $datatype/$channel/$year$magnet."
