#!/bin/bash

run=""
ManipFolder="/agatadisks/e676/e676"
ReplayFolder="/opt/data/GANIL/e676/Replay"
TopologyFolder="${ReplayFolder}/Topology"
Topology="${TopologyFolder}/Topology_Global.conf"
InputRunFolder=""
RunPatern="run_"
FromLocal=""
OutName=""

for i in $*
do
    if [[ $i == "-run" ]]
    then
            Mode="RunMode"
            continue
    fi
    if [[ $i == "-output_run_patern" ]]
    then
            Mode="RunPat"
            continue
    fi
    if [[ $i == "-topology" ]]
    then
            Mode="TopologyMode"
            continue
    fi
    if [[ $i == "-from_local_replay" ]]
    then
            FromLocal="_LOCAL"
            continue
    fi
    if [[ $i == "-output_folder_name" ]]
    then
            Mode="OFN"
            continue
    fi

    if [[ $Mode == "" ]]
    then
            echo -e "\e[1;34mUsage manual:"
            echo -e "-run RunNr or RunFolder => for the run number or directly the run folder"
            echo -e "-topology TopologyPath => to select a specific topology file, default is TopologyGlobal.conf"
            echo -e "-output_run_patern ==> change the run patern output"
            echo -e "-output_folder_name ==> change the output folder name"
            echo -e "-from_local_replay\e[0m"
            exit 2
    else
            if [[ ${Mode} == "RunMode" ]]
            then
                   run=${i}
            fi
            if [[ ${Mode} == "RunPat" ]]
            then
                    RunPatern=${i}
            fi
            if [[ ${Mode} == "OFN" ]]
            then
                    OutName=${i}
            fi
            if [[ ${Mode} == "TopologyMode" ]]
            then
                    if [ -f ${i} ]
                    then
                            Topology=${i}
                            abspath=`echo "$(dirname $(readlink -e ${Topology}))/$(basename ${Topology})"`
                            #abspath=`echo ${TopologyFolder}/"${Topology##*/}"`
                            #abspath=`echo "${Topology##*/}"`
                            Topology=$abspath
                    else
                            echo -e "\e[1;31m${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 RunNr => for the run number"
        echo -e "-topology TopologyPath => to select a specific topology file, default is TopologyGlobal.conf"
        echo -e "-from_local_replay RunFolder ==> from local replay, will took data from RunFolder/Out"
        echo -e "-output_run_patern ==> change the run patern output \e[0m"
        exit 2
else
        if [[ ${FromLocal} == "" ]]
        then
                if [ -d ${run} ]
                then
                        InputRunFolder=`cd "${run}"; pwd`
                else
                        if [ -d ${ManipFolder}/run_${run}* ]
                        then
                                InputRunFolder=`echo ${ManipFolder}/run_${run}*`
                        else
                                echo -e "\e[1;31mrun_$run not found in ${ManipFolder}/\e[0m"
                                exit 2
                        fi
                fi
        else
                if [ -d ${run} ]
                then
                        InputRunFolder=`cd "${run}"; pwd`
                else
                        if [ -d ${ReplayFolder}/run_${run}_LOCAL ]
                        then
                                InputRunFolder="${ReplayFolder}/run_${run}_LOCAL"
                        else
                                echo -e "\e[1;31mrun_${run}_LOCAL not found in ${ReplayFolder}/\e[0m"
                                exit 2
                        fi
                fi
        fi
fi

if [[ ${OutName} == "" ]]
then
        if [ -d ${run} ]
        then
                echo -e "\e[1;31mWhen the run folder is directly specified, you need to define an output folder name via -output_folder_name\e[0m"
                exit 2
        else
                OutName=${RunPatern}${run}
        fi
fi

echo -e "\e[1;34mSettings are"
echo -e "Replay will be created in ${OutName}"
echo -e "Topology loaded is ${Topology}"
echo -e "Data token from the folder $InputRunFolder\e[0m"

echo
echo "Press any key to continue"
read
			
if [ -d ${OutName} ]
then
	echo -e "\e[1;34mFolder already existing, please fix it\e[0m"
	exit 2
fi

cp -rp ${ReplayFolder}/template ${OutName}
cd ${OutName}

if [[ ${FromLocal} == "" ]]
then
        ln -s ${InputRunFolder}/Data
else
        ln -s ${InputRunFolder}/Out Data
fi

./gen_conf.py

/opt/analysis-tools/GANIL/AgataSoftware/agapro/WinCtest/femul ${Topology}  | tee log_glob
