#!/bin/bash

# ----------------------------------------------------------------------------------------------------------------------------------
# File name: CodeXL
# This script sets CodeXL's related environment variables and 
# executed the CodeXL binary executable.
#
# (c) 2012 Advanced Micro Devices Inc.
# ----------------------------------------------------------------------------------------------------------------------------------

# Get this script full path:
if echo "$0" | grep '^/' ; then
	thisScriptFullPath="$0"
else
	thisScriptFullPath=`pwd`/$0
fi

# CodeXL's binaries directory is this scripts directory:
CodeXLBinariesDir=`dirname "$thisScriptFullPath"`

# Calculate CodeXL's binary executable path:
CodeXLBinaryExePath="${CodeXLBinariesDir}/CodeXL-bin"

# Obtain libstdc++ needed version
LDCONFIG="ldconfig"
command -v "$LDCONFIG" &> /dev/null || LDCONFIG="/sbin/ldconfig"
CRUNTIMEPATH=$(${LDCONFIG} -p | grep libstdc++.so.6 | grep 64)
CRUNTIMEFILE=${CRUNTIMEPATH##*>}

# Test if current system C++ runtime is older from CodeXL version needed
CXL_CLIB=""
NEWCRUNTIME=$(strings $CRUNTIMEFILE | grep GLIBCXX_3.4.16)
if [ -z "$NEWCRUNTIME" ];then
    # System C++ runtime is older from CodeXL
    CXL_CLIB="${CodeXLBinariesDir}/RuntimeLibs/x86_64"
fi

# Add CodeXL's binaries directory to LD_LIBRARY_PATH:
if [ -z "$LD_LIBRARY_PATH" ]; then
	export LD_LIBRARY_PATH="${CodeXLBinariesDir}:${CXL_CLIB}"
else
	export LD_LIBRARY_PATH="${CodeXLBinariesDir}:${CXL_CLIB}:$LD_LIBRARY_PATH"
fi

# Run CodeXL:
kdbg $CodeXLBinaryExePath

