#!/bin/bash

run=""
ManipFolder="/agatadisks/e676/e676"
ReplayFolder="/opt/data/GANIL/e676/Replay"
TopologyFolder="${ReplayFolder}/Topology"
Topology="${TopologyFolder}/TopologyLocal.conf"

CrystalsList="00A 00B 00C 01A 01B 01C 02A 02B 02C 03A 03B 03C 04A 04B 04C 05A 05B 05C 09B 09C 10A 10B 10C 11A 11B 11C 12A 12B 12C 13A 13B 13C 14A 14B 14C"

for i in $*
do
	if [[ $i == "-run" ]]
        then Mode="RunMode"
             continue
    fi
    if [[ $i == "-topology" ]]
        then Mode="TopologyMode"
             continue
    fi
	if [[ $Mode == "" ]]
    then
         echo -e "\e[1;34mUsage manual:"
         echo -e "-run RunNr => for the run number"
         echo -e "-topology TopologyPath => to select a specific topology file, default is TopologyLocal.conf \e[0m"
         exit 2
    else
	if [[ ${Mode} == "RunMode" ]]
            then
				if [ -d ${ManipFolder}/run_${i}* ]
                	then run=$i
					else echo -e "\e[1;31m[\u2717]run_$run not found in ${ManipFolder}/\e[0m"
						 exit 2
				fi
        fi
		if [[ ${Mode} == "TopologyMode" ]]
			then
				if [ -f ${i} ]
                	then Topology=${i}
						 abspath=`echo ${PWD}/"${Topology##*/}"`
					 	 Topology=$abspath
					else echo -e "\e[1;31m[\u2717]${i} not found\e[0m"
						 exit 2
				fi	
		fi
	fi
done

if [[ ${run} == "" ]]
then
     echo -e "\e[1;31mYou at least need to specify the run number"
     echo -e "Usage manual: "
     echo -e "-run for the run number"
     echo -e "-topology to select a specific topology file, default is Topologies/TopologyLocal.conf\e[0m"
     exit 2
fi

echo -e "\e[1;34mSettings are"
echo -e "Replay will be created in run_${run}_LOCAL"
echo -e "Replay will be done for crystals : $CrystalsList"
echo -e "Topology loaded is ${Topology}\e[0m"

echo
echo "Press any key to continue"
read
			
echo " will copy template directory in run_${run} "
cp -rp ${ReplayFolder}/template run${run}_LOCAL
cd run${run}_LOCAL
ln -s ${ManipFolder}/run_${run}*/Data
./gen_conf.py
rm -rf ${ReplayFolder}/run${run}_LOCAL/Out/vamos
ln -s ${ManipFolder}/run_${run}*/Data/vamos Out/vamos
source multiFemul.sh
#cd Out
#rm -rf vamos
#ln -s ${ManipFolder}/run_${run}*/Data/vamos
#cd ..
#mkdir -p $TopologyFolder/tmp
#
#for i in $CrystalsList
#do 
#	sed "s/???/${i}/g" ${Topology} > $TopologyFolder/tmp/TopologyLocal_${i}.conf
#	/opt/analysis-tools/GANIL/AgataSoftware/bin/femul $TopologyFolder/tmp/TopologyLocal_${i}.conf -nothr > log_${i} &
#
#done
