#! /bin/sh
NDHOMEPATH='/home/cavisson/netdiagnostics'
if [ -z $NDHOME ]; then 
	NDHOMEPATH='/home/cavisson/netdiagnostics'
else
	$NDHOMEPATH = $NDHOME
	echo 'newndhome'
	echo $NDHOMEPATH
fi

CAVGOPATH=$NDHOMEPATH/go
echo 'cavgopath'
echo $CAVGOPATH

CLIBBASEPATH='/opt/cavisson'
CLIBLOGPATH='/opt/cavisson/netdiagnostics'
CAVLIBFILE=/usr/lib/libcavwrapperlib.so

if test -f "$CAVLIBFILE"; then
    sudo rm $CAVLIBFILE
fi

if [ -d "$CAVGOPATH" ]; then
	echo 'delete cavgopath'
    sudo rm -rf $CAVGOPATH
fi

[ ! -d "$NDHOMEPATH" ] && mkdir -p "$NDHOMEPATH"
[ ! -d "$CLIBBASEPATH" ] && mkdir -p "$CLIBBASEPATH"
[ ! -d "$CLIBLOGPATH" ] && mkdir -p "$CLIBLOGPATH"
[ ! -d "$CLIBLOGPATH/logs" ] && mkdir -p "$CLIBLOGPATH/logs"

echo "" 
echo "Extracting tar under " $NDHOMEPATH
sudo tar -xzf go.tar.gz -C $NDHOMEPATH

if [ -z $NDHOME ]; then 
	echo 'export ndhome'
	sudo echo 'export NDHOME=/home/cavisson/netdiagnostics' >> /root/.bashrc 
else 
	echo 'export ndhome2'
	sudo sed 's,NDHOME=[^;]*,NDHOME='$NDHOMEPATH',' -i ~/.bashrc
fi

if [ -z $LD_LIBRARY_PATH ]; then 
	echo 'export ldlib'
	sudo echo 'export LD_LIBRARY_PATH='$NDHOMEPATH'/go/lib/' >> /root/.bashrc 
else 
	sudo sed 's,LD_LIBRARY_PATH=[^;]*,LD_LIBRARY_PATH='$NDHOMEPATH'/go/lib/,' -i ~/.bashrc
fi

if [ -z $DYNINSTAPI_RT_LIB ]; then 
	echo 'export dynrt'
	sudo echo 'export DYNINSTAPI_RT_LIB='$NDHOMEPATH'/go/lib/libdyninstAPI_RT.so' >> /root/.bashrc 
else 
	sudo sed 's,DYNINSTAPI_RT_LIB=[^;]*,DYNINSTAPI_RT_LIB='$NDHOMEPATH'/go/lib/libdyninstAPI_RT.so,' -i ~/.bashrc
fi

#sudo sed 's,NDHOME=[^;]*,NDHOME=/home/cavisson/netdiagnostics/go/,' -i ~/.bashrc
#sudo sed 's,LD_LIBRARY_PATH=[^;]*,LD_LIBRARY_PATH=/home/cavisson/netdiagnostics/go/lib/,' -i ~/.bashrc
#sudo sed 's,DYNINSTAPI_RT_LIB=[^;]*,DYNINSTAPI_RT_LIB=/home/cavisson/netdiagnostics/go/lib/libdyninstAPI_RT.so,' -i ~/.bashrc
#sudo echo 'export NDHOME=/home/cavisson/netdiagnostics/go/' >> /root/.bashrc
#sudo echo 'export LD_LIBRARY_PATH=/home/cavisson/netdiagnostics/go/lib/' >> /root/.bashrc
#sudo echo 'export DYNINSTAPI_RT_LIB=/home/cavisson/netdiagnostics/go/lib/libdyninstAPI_RT.so' >> /root/.bashrc

echo "Installing the agent under " $CAVGOPATH
cwd=$(pwd)
cd $CAVGOPATH/lib
sudo ln -s libapr-1.so libapr-1.so.0

sudo ln -s libasm-0.186.so libasm.so.1
sudo ln -s libasm.so.1 libasm.so

sudo ln -s libelf-0.186.so libelf.so.1
sudo ln -s libelf.so.1 libelf.so

sudo ln -s libdw-0.186.so libdw.so.1
sudo ln -s libdw.so.1 libdw.so

sudo ln -s libtbb.so.2 libtbb.so
sudo ln -s libtbbmalloc.so.2 libtbbmalloc.so
sudo ln -s libtbbmalloc_proxy.so.2 libtbbmalloc_proxy.so

sudo ln -s libboost_timer.so.1.71.0 libboost_timer.so
sudo ln -s libboost_filesystem.so.1.71.0 libboost_filesystem.so
sudo ln -s libboost_date_time.so.1.71.0 libboost_date_time.so
sudo ln -s libboost_chrono.so.1.71.0 libboost_chrono.so
sudo ln -s libboost_thread.so.1.71.0 libboost_thread.so
sudo ln -s libboost_atomic.so.1.71.0 libboost_atomic.so
sudo ln -s libboost_system.so.1.71.0 libboost_system.so

sudo ln -s libstackwalk.so.10.2.1 libstackwalk.so.10.2
sudo ln -s libstackwalk.so.10.2 libstackwalk.so

sudo ln -s libdynElf.so.10.2.1 libdynElf.so.10.2
sudo ln -s libdynElf.so.10.2 libdynElf.so

sudo ln -s libpatchAPI.so.10.2.1 libpatchAPI.so.10.2
sudo ln -s libpatchAPI.so.10.2 libpatchAPI.so

sudo ln -s libparseAPI.so.10.2.1 libparseAPI.so.10.2
sudo ln -s libparseAPI.so.10.2 libparseAPI.so

sudo ln -s libdyninstAPI_RT.so.10.2.1 libdyninstAPI_RT.so.10.2
sudo ln -s libdyninstAPI_RT.so.10.2 libdyninstAPI_RT.so

sudo ln -s libpcontrol.so.10.2.1 libpcontrol.so.10.2
sudo ln -s libpcontrol.so.10.2 libpcontrol.so

sudo ln -s libdynDwarf.so.10.2.1 libdynDwarf.so.10.2
sudo ln -s libdynDwarf.so.10.2 libdynDwarf.so

sudo ln -s libdynC_API.so.10.2.1 libdynC_API.so.10.2
sudo ln -s libdynC_API.so.10.2 libdynC_API.so

sudo ln -s libsymtabAPI.so.10.2.1 libsymtabAPI.so.10.2
sudo ln -s libsymtabAPI.so.10.2 libsymtabAPI.so

sudo ln -s libsymLite.so.10.2.1 libsymLite.so.10.2
sudo ln -s libsymLite.so.10.2 libsymLite.so

sudo ln -s libinstructionAPI.so.10.2.1 libinstructionAPI.so.10.2
sudo ln -s libinstructionAPI.so.10.2 libinstructionAPI.so

sudo ln -s libdyninstAPI.so.10.2.1 libdyninstAPI.so.10.2
sudo ln -s libdyninstAPI.so.10.2 libdyninstAPI.so

sudo ln -s libcommon.so.10.2.1 libcommon.so.10.2
sudo ln -s libcommon.so.10.2 libcommon.so

sudo ln -s libunwind.so.8.0.1 libunwind.so.8
sudo ln -s libunwind.so.8.0.1 libunwind.so

sudo ln -s libunwind-ptrace.so.0.0.0 libunwind-ptrace.so.0
sudo ln -s libunwind-ptrace.so.0.0.0 libunwind-ptrace.so

sudo ln -s libunwind-coredump.so.0.0.0 libunwind-coredump.so.0
sudo ln -s libunwind-coredump.so.0.0.0 libunwind-coredump.so

sudo ln -s libunwind-x86_64.so.8.0.1 libunwind-x86_64.so.8
sudo ln -s libunwind-x86_64.so.8.0.1 libunwind-x86_64.so

sudo ln -s libunwind-setjmp.so.0.0.0 libunwind-setjmp.so.0
sudo ln -s libunwind-setjmp.so.0.0.0 libunwind-setjmp.so

sudo ln -s libunwind-x86_64.so libunwind-generic.so

sudo ln -s libcapstone.so.5.0.0 libcapstone.so.5
sudo ln -s libcapstone.so.5 libcapstone.so

sudo mv $CAVGOPATH/logs $NDHOMEPATH
echo 'etc path'
echo $CAVGOPATH/etc
sudo mv $CAVGOPATH/etc $NDHOMEPATH
sudo mv $CAVGOPATH/config $NDHOMEPATH
sudo mv $CAVGOPATH/cavgoService /etc/init.d/
sudo mv $CAVGOPATH/gocontrolService /etc/init.d/
sudo cp -f libcavwrapperlib.so /usr/lib
cd $cwd
echo 'refreshing bashrc'
#echo $cwd
#sudo echo 'source ~/.bashrc'
#echo "Installation done. Starting agent "
#echo ""
#'/etc/init.d/cavgoService' start
#echo ""
#echo "Agent started"
echo "********************"
echo "launch service using /etc/init.d/cavgoService start"
echo "run go Application using LD_PRELOAD like export LD_PRELOAD=/usr/lib/libcavwrapperlib.so; ./goapp<any go application>"
echo "********************"
echo ""

