#!/bin/bash


# Location of your chosen .sif file
export MUONE_SING_IMG=/hepstore/scharity/Containers/muone_ubuntu_2004.sif

# Name of your chosen install folder - script will create $HOME/$DIRNAME and 
# install the repositories there
DIRNAME=MUonE

# Number of cores to use for build - default is 2
NCORES=2

###############################################################################

export MUONEBASEDIR=$HOME/$DIRNAME
export FAIRSOFTBASE=$MUONEBASEDIR/FairSoft
export FAIRROOTBASE=$MUONEBASEDIR/FairRoot
export FAIRMUONEBASE=$MUONEBASEDIR/fairmuone
export FAIRSOFTBUILD=$MUONEBASEDIR/FairSoft/build
export FAIRROOTBUILD=$MUONEBASEDIR/FairRoot/build
export FAIRSOFTINSTALL=$MUONEBASEDIR/FairSoft/install
export FAIRROOTINSTALL=$MUONEBASEDIR/FairRoot/install
export FAIRROOTMUONE=$FAIRROOTBASE/MUonE


if [ ! -d $MUONEBASEDIR ] ; then
    mkdir -p $MUONEBASEDIR
fi

cd $MUONEBASEDIR

if [ ! -d FairSoft ] ; then
    git clone -b master https://github.com/FairRootGroup/FairSoft
fi

if [ ! -d FairRoot ] ; then 
    git clone -b dev https://github.com/FairRootGroup/FairRoot.git
fi

if [ ! -d $FAIRROOTBUILD ] ; then 
    mkdir $FAIRROOTBUILD
fi

if [ ! -d fairmuone ] ; then 
    git clone -b master https://gitlab.cern.ch/mgoncerz/fairmuone.git
fi


if [ ! -d $FAIRROOTMUONE ] ; then
    cp -r $FAIRMUONEBASE/MUonE $FAIRROOTBASE/.
    if grep "MUonE" $FAIRROOTBASE/CMakeLists.txt ; then 
	echo
    else 
	awk '/fairtools/ { print; print "add_subdirectory(MUonE)"; next }1' $FAIRROOTBASE/CMakeLists.txt > $FAIRROOTBASE/CMakeLists_new.txt
	mv $FAIRROOTBASE/CMakeLists_new.txt $FAIRROOTBASE/CMakeLists.txt
    fi
    grep "MUonE" $FAIRROOTBASE/CMakeLists.txt
fi


# these are required by FairRoot and FairMUONE
export SIMPATH=$MUONEBASEDIR/FairSoft/install
export FAIRROOTPATH=$MUONEBASEDIR/FairRoot/install

function fairsoft_build { export thisdir=`pwd` && echo $thisdir && singularity exec $MUONE_SING_IMG cmake -S $FAIRSOFTBASE -B $FAIRSOFTBASE/build -C $FAIRSOFTBASE/FairSoftConfig.cmake && singularity exec $MUONE_SING_IMG cmake --build $FAIRSOFTBASE/build -j$NCORES && cd $thisdir; }

function fairroot_build { export thisdir=`pwd` && cd $FAIRROOTBUILD && echo `pwd` && singularity exec $MUONE_SING_IMG cmake -DCMAKE_INSTALL_PREFIX="${FAIRROOTPATH}" .. && singularity exec $MUONE_SING_IMG make && singularity exec $MUONE_SING_IMG make install && cd $thisdir && echo `pwd`;}

function update_muone_cmakelists {
    export thisdir=`pwd`

    cd $FAIRROOTMUONE
    
    for FILE in `ls */CMakeLists.txt` ; do
	echo $FILE
	if grep "::FairTools" $FILE ; then
	    sed -i 's/::FairTools/::Tools/' $FILE
	    grep "::Tools" $FILE
	    cat $FILE
	fi

	if grep "::FairFastSim" $FILE ; then
	    sed -i 's/::FairFastSim/::FastSim/' $FILE
	    grep "::FastSim" $FILE
	    cat $FILE
	fi
    done

    for FILE in `ls */*/CMakeLists.txt` ; do
	PATH_TO_FILE=`readlink -f $FILE`
	echo $PATH_TO_FILE
	if grep FairRoot::Tools $FILE ; then
	    echo
	else 
	    awk '/target_link_libraries/ { print; print "    FairRoot::Tools"; next }1' $FILE > tmp_file.txt
	    mv tmp_file.txt $PATH_TO_FILE
	    grep "FairRoot::Tools" $FILE
	fi
    done
    
    cd $thisdir
}
