#!/bin/sh
JDKDIR=
JARTARGET=/usr/src/packages/BUILD/tau-2.35.1/TAUBUILD/x86_64/lib
TAU_LIB_DIR=/usr/src/packages/BUILD/tau-2.35.1/TAUBUILD/x86_64/lib #curiously enough the libs are in the same directory as the jars.
NON_TAUARGS=""

if [ ! -d "$TAUROOT" ]; then #If the original root directory is not found find and work from this script's bin directory 

  SOURCE="$0"
  while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
    DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"
    SOURCE="$(readlink "$SOURCE")"
    [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
  done
  TAUBIN="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"

  TAUROOT=`dirname $TAUBIN`
  MACHINE=`basename $TAUROOT`
  TAUROOT=`dirname $TAUROOT`
  TAU_LIB_DIR="$TAUROOT"/"$MACHINE"/lib
  JDKDIR=$TAU_LIB_DIR
fi #End backup root search


LD_LIBRARY_PATH=$TAU_LIB_DIR:$LD_LIBRARY_PATH

usage(){
    echo "The TauJVMTI profiling agent"
    echo $0" [options] javaprogram [args]";
    echo "Options are semicolon separated (make sure to escape the semicolon!)"
    echo "Within an option the arguments are comma separated."
    echo "    -help       Print help information"
    echo "    -verbose    Report the arguments of the script before it runs."
    echo "    -tau:agentlib=<agentlib> By default tau_java uses the most recently configured jdk, you can specify a different one here."
    echo "    -tau:java=<javapath>     Path to a java binary, by default uses the one corresponding to the most recently configured jdk."
    echo "    -tau:bootclasspath=<bootclasspath>    To modify the bootclasspath to point to a different jar, not usually necessary."
    echo "    -tau:include=<item>      Only these classes/methods"
    echo "    -tau:exclude=<item>      Exclude these classes/methodsn"
    echo "    -tau:node=<NodeID>       Use designated <NodeID> (default=0)"
    echo "    item    Qualified class and/or method names"
    echo "       e.g. (*.<init>;Foobar.method;sun.*)"
    exit
}

if [ $# = 0 ]; then
    usage;
fi

verbose=false
include=
exclude=
agentlib=-agentpath:${TAU_LIB_DIR}/libTAU.jnilib 
classpatharg=-Xbootclasspath/a:$JARTARGET/TauJVMTI.jar
java=java
node=0

for arg in "$@"; do
  # Thanks to Bernd Mohr for the following that handles quotes and spaces (see configure for explanation)
  # Thanks to sameer shende for pointing to this example.
  modarg=`echo "x$arg" | sed -e 's/^x//' -e 's/"/\\\"/g' -e s,\',%@%\',g -e 's/%@%/\\\/g' -e 's/ /\\\ /g'`
  case $modarg in
      -h|-help|--help)
	  usage
	  ;;
      -v|-verbose|--verbose)
	  verbose=true
	  shift
	  ;;
      -tau:include=*)
          include=`echo $arg | sed 's/-tau:include=//'`
	  shift
	  ;;
      -tau:exclude=*)
          exclude=`echo $arg | sed 's/-tau:exclude=//'`
          shift
          ;;
      -tau:agentlib=*)
          agentarg=`echo $arg | sed 's/-tau:agentlib=//'`
	  agentlib="-agentlib:$agentarg"
          shift
	  ;;
      -tau:bootclasspath=*)
          bootclasspath=`echo $arg | sed 's/-tau:bootclasspath=//'`
          classpatharg="$classpatharg -Xbootclasspath/a:$bootclasspath"
          shift
	  ;;
      -tau:java=*)
          java=`echo $arg | sed 's/-tau:java=//'`
          shift
	  ;;
      -tau:node=*)
	  node=`echo $arg | sed 's/-tau:node=//'`
	  shift
	  ;;
      *)
          NON_TAUARGS="$NON_TAUARGS $modarg"
	  shift
	  ;;
      esac
done

agentlib="$agentlib=node=$node"

if [ "x$exclude" != "x" ]; then 
  agentlib="$agentlib;exclude=$exclude" 
fi

if [ "x$include" != "x" ]; then 
  agentlib="$agentlib;include=$include" 
fi

if [ $verbose = "true" ] ; then
    echo ""
    echo "Program to run : $@"
    echo ""

    echo "Include is $include"
    echo "Exclude is $exclude"
    echo "AgentLib is $agentlib"
    echo "Java is $java"
    echo "NodeID is $node"
    echo "Executing> $java $agentlib $classpatharg $NON_TAUARGS"
fi

$java $agentlib $classpatharg $NON_TAUARGS

##
