#!/bin/sh
TAUROOT=/usr/src/packages/BUILD/tau-2.35.1/TAUBUILD
MACHINE=x86_64

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`

fi #End backup root search


LIBDIR=${TAUROOT}/${MACHINE}/lib
JARDIR=${TAUROOT}/${MACHINE}/lib

JARS=${JARDIR}/paraprof.jar:${JARDIR}/perfdmf.jar:${JARDIR}/tau-common.jar:${JARDIR}/vis.jar:${JARDIR}/jogl.jar:${JARDIR}/jatha.jar:${JARDIR}/jgraph.jar:${JARDIR}/xerces.jar:${JARDIR}/jargs.jar:${JARDIR}/batik-combined.jar:${JARDIR}/jfreechart-1.0.12.jar:${JARDIR}/jcommon-1.0.15.jar:${JARDIR}/jython.jar

# Check machine type for a heap space boost
MEMORY=-Xmx800m

machine=`uname -m`
if [ "x$machine" = "xx86_64" ] ; then
    MEMORY=-Xmx2000m
fi
JAVA_VERSION=`java -version 2>&1 | head -1 | cut -d '.' -f2`
if [ "x$JAVA_VERSION" = "x4" ] ; then
        echo ""
        echo "Java 1.5 or newer is required to run perfdmf_configure."
        echo "Please update your Java SDK to a newer version to use the newest version."
        echo "You will still be able to use the version from the TAU v2.19.1 release."
        echo ""
        if [ ! -d ${TAUROOT}/${MACHINE}/bin/bin-1.4 ] ; then
                ${TAUROOT}/${MACHINE}/bin/configure-1.4
        fi
  :%      ${TAUROOT}/${MACHINE}/bin/bin-1.4/perfdmf_configure

        exit 0
fi
java $MEMORY \
-Dderby.system.home=${HOME}/.ParaProf \
-cp ${JARS} \
edu.uoregon.tau.perfdmf.loader.PhaseConverter \
"$@"
