#!/bin/bash

#set -e
set -vx

echo -e "*****************************\n* Checkout TTF T012 sources\n*****************************\n"
mkdir -p ${HOME}/dev && cd ${HOME}/dev || exit 1

git clone --branch devel --recurse-submodules --single-branch https://forge.etsi.org/gitlab/emergency-communications/NG112.git ETSI_Ng112
cd ${HOME}/dev/ETSI_Ng112/titan-test-system-framework
git checkout devel
cd ./ttcn/LibHttp
ln -sf module_emtel.mk module.mk

echo -e "*****************************\n* Set up environment\n*****************************\n"
cd /home/etsi/dev/ETSI_Ng112/scripts
ln -sf /home/etsi/dev/ETSI_Ng112/scripts/devenv.bash.ubuntu /home/etsi/devenv.bash
. /home/etsi/devenv.bash

echo -e "*****************************\n* Apply patched \n*****************************\n"
cd /home/etsi/dev/ETSI_Ng112
cp ./ttcn/patch_lib_common_titan/module.mk ./ttcn/LibCommon/
cp ./ttcn/patch_lib_common_titan/*.ttcn ./ttcn/LibCommon/
cp ./ttcn/patch_lib_http/*.ttcn ./ttcn/LibIts/ttcn/Http/
cp ./ttcn/patch_ims_titan/*.ttcn ./ttcn/LibIms/ttcn/
cp ./ttcn/patch_ims_titan/module.mk ./ttcn/LibIms/
cp ./ttcn/patch_sip_titan/*.ttcn ./ttcn/LibSip/ttcn/
cp ./ttcn/patch_sip_titan/module.mk ./ttcn/LibSip/
cp  ./ttcn/patch_lib_its/module.mk ./ttcn/LibIts/

echo -e "*****************************\n* Build test suites\n*****************************\n"
ATS_LIST="AtsLIS AtsECRF AtsESRP AtsPSAP AtsBCF"
for i in ${ATS_LIST}
do
    export ATS=$i
    make
done

echo -e "*****************************\n* Init Eclipse Workspace\n*****************************\n"

echo -e "*****************************\n* Setup a configuration file\n*****************************\n"
cd ./etc/AtsLIS
ln -sf AtsNg112_Gridgears_lis.cfg_ AtsNg112.cfg
cd -

echo -e "*****************************\n* Change sudo in command line\n*****************************\n"
cd /home/etsi/dev/ETSI_Ng112/scripts
export ATS=AtsLIS
#sed --in-place 's/sudo/echo "etsi" \| sudo -S/' ./run_all.bash

cd /home/etsi/dev/ETSI_Ng112

