#!/bin/sh
JDKDIR=
JARTARGET=/home/users/sameer/fresh/f/head/release/tau2/default/lib
TAU_LIB_DIR=/home/users/sameer/fresh/f/head/release/tau2/default/lib #curiously enough the libs are in the same directory as the jars.
NON_TAUARGS=""

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 "    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=-agentlib:TAUsh-jdk 
classpatharg=-Xbootclasspath/a:$JARTARGET/TauJVMTI.jar
java=java


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
	  ;;
      *)
          NON_TAUARGS="$NON_TAUARGS $modarg"
	  shift
	  ;;
      esac
done

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 "Executing> $java $agentlib $classpatharg $NON_TAUARGS"
fi

$java $agentlib $classpatharg $NON_TAUARGS

##
