#!/bin/sh
TAUROOT=/opt/ohpc/pub/libs/intel/impi/tau/2.29
MACHINE=


SCHEMADIR=${TAUROOT}/etc
BINDIR=${TAUROOT}/${MACHINE}/bin
LIBDIR=${TAUROOT}/${MACHINE}/lib
JARDIR=${TAUROOT}/${MACHINE}/lib
JAVA2D_OPENGL_FLAG=1

CUBE_JAVA_READER=${TAUROOT}/${MACHINE}/lib/CubeReader.jar

#paraprof does not accept -64/-32/-jogl2/-jogl1. Set the flags and remove them from the parameter list before passing to paraprof. -32 (which makes no difference on non-apple platforms) and -jogl1 are the defaults on all platforms except apple.
for arg 
do
  shift
  [ "$arg" = "-libgl_indirect" ] && LIBGL_ALWAYS_INDIRECT_FLAG=1 && continue
  [ "$arg" = "-disable_java2d_opengl" ] && JAVA2D_OPENGL_FLAG=0 && continue
  [ "$arg" = "-64" ] && FLAG64=1 && continue
  [ "$arg" = "-32" ] && FLAG32=1 && continue
  [ "$arg" = "-jogl1" ] && FLAGJOGL1=1 && continue
  [ "$arg" = "-jogl2" ] && FLAGJOGL2=1 && continue
  set -- "$@" "$arg"
done

if [ "$MACHINE" = "apple" ]; then
	if [ "$FLAG32" != "1" ]; then
		FLAG64=1
	fi
fi

if [ "$MACHINE" = "apple" ] || [ "$MACHINE" = "arm64_linux" ] || [ "$MACHINE" = "ibm64linux" ]; then
	if [ "$FLAGJOGL1" != "1" ]; then
		FLAGJOGL2=1
	fi
fi

# If 3D window has problems, please uncomment
if [ $MACHINE = bgq -o $MACHINE = arm64_linux ]; then
  JAVA2D_OPENGL_FLAG=0
  LIBGL_ALWAYS_INDIRECT_FLAG=1
fi

if [ "$LIBGL_ALWAYS_INDIRECT_FLAG" = "1" ]; then 
  export LIBGL_ALWAYS_INDIRECT=1
fi

if [ "$JAVA2D_OPENGL_FLAG" = "1" ]; then 
  JAVA2D_OPENGL_OPT="-Dsun.java2d.opengl=true"
fi

if [ "$MACHINE" = "craycnl"  -a "`uname -m`" = "aarch64" ]; then
        if [ "$FLAGJOGL1" != "1" ]; then
                FLAGJOGL2=1
        fi
fi

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:${JARDIR}/mesp.jar:${CUBE_JAVA_READER}:${JARDIR}/gson-2.1.jar:${JARDIR}/postgresql.jar



if [ "$FLAGJOGL2" = "1" ]; then
	JARS=${JARDIR}/paraprof.jar:${JARDIR}/perfdmf.jar:${JARDIR}/tau-common.jar:${JARDIR}/vis-jogl2.jar:${JARDIR}/jogl-all.jar:${JARDIR}/gluegen-rt.jar:${JARDIR}/jogl-all-natives.jar:${JARDIR}/gluegen-rt-natives.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:${JARDIR}/mesp.jar:${CUBE_JAVA_READER}:${JARDIR}/gson-2.1.jar:${JARDIR}/postgresql.jar
fi


APPLE_OPTIONS=""
JAVA_VERSION=`java -version 2>&1 | head -1 | cut -d '.' -f2`
if [ "$MACHINE" = "apple" ]; then
    IS_JAVA_64BIT=`java -version 2>&1 | tail -1 | grep 64 | wc -l`
    if [ "$FLAG64" != "1" ]; then
    	OSX_BITS="-d32"
    fi
    if which sw_vers >/dev/null 2>&1 ; then
        OSX_VERSION=`sw_vers -productVersion 2>/dev/null | sed -e 's,\.,,g'`
        if [ $OSX_VERSION -ge 107 ] ; then
          OSX_BITS=""
        fi

	if [ $OSX_VERSION -ge 107 -a $IS_JAVA_64BIT -ge 1 ]; then
          if [ -d /System/Library/Frameworks/JavaVM.framework/Versions/1.6 ]; then
	    export JAVA_VERSION=1.6
	    if [ "$FLAG64" != "1" ]; then
	    	OSX_BITS="-d32"
	    fi
	  fi
        fi
    fi
    APPLE_OPTIONS="$OSX_BITS -Xdock:name=ParaProf -Xdock:icon=${TAUROOT}/${MACHINE}/lib/tau-medium.png -Dapple.laf.useScreenMenuBar=true -Dcom.apple.mrj.application.growbox.intrudes=true"
    for arg in "$@"; do
	case $arg in
	    --pack)
		APPLE_OPTIONS="$APPLE_OPTIONS -Djava.awt.headless=true"
		;;
	    --text)
		APPLE_OPTIONS="$APPLE_OPTIONS -Djava.awt.headless=true"
		;;
	    --dump)
		APPLE_OPTIONS="$APPLE_OPTIONS -Djava.awt.headless=true"
		;;
	esac
    done
fi

# check for GNU GCJ, which ParaProf doesn't work with
GCJ=`java -showversion 2>&1 | grep -i gcj`
if [ "x$GCJ" = "x" ] ; then
    # ok
    GCJ="no"
else
    echo ""
    echo "ParaProf: Warning: GCJ detected!"
    echo ""
    echo "You will likely receive an error because GCJ does not fully implement Java/Swing"
    echo ""
    echo "It is recommended that you install a full Java implementation from java.sun.com"
    echo ""
    echo ""
fi

# Test for java 1.4+
# 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 ParaProf."
        echo "Please update your Java SDK to a newer version to use the latest ParaProf."
        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/paraprof

        exit 0
fi



if [ -r ${JARDIR}/gluegen.jar ] ; then
  JARS=${JARS}:${JARDIR}/gluegen.jar
fi


# Default to 800m heap space
#MEMORY=-Xmx800m
#This default value gives an error, so let java pick a "good" value
MEMORY=

# Check machine type for a heap space boost
machine=`uname -m`
platform=`uname -s`
if [ "x$MACHINE" = "xx86_64" ] ; then
    MEMORY=-Xmx2000m
fi

testmax=`$BINDIR/tau_javamax.sh`
#if [ "x$testmax" != "xfailed" -a "x$platform" != "xDarwin" ] ; then
if [ "x$testmax" != "xfailed" ] ; then
    MEMORY="-Xmx${testmax}m"
fi
if [ "x$platform" = "xDarwin" ] ; then 
   if [ $testmax -gt 819200 -a $testmax -lt 819200000 ] ; then
      MEMORY="-Xmx2000m"
    if [ "$FLAG64" = "1" ]; then
	testmax=$(($testmax/1024))
        MEMORY="-Xmx"$testmax"m"
    fi
   fi
fi



# locate the java bin directory so that we can add it to the java.library.path.
# IBM JRE has libjawt.so in the bin directory and it needs to be hardcoded in
# here.
# Skip this step on Mac OS X, readlink fails in some cases.
javaLocation=`which java`
# check for readlink first
readlink=`which readlink`
if [ "x$readlink" != "x" -a "x$platform" != "xDarwin" ] ; then
    if [ -x "$readlink" ] ; then
	javaLocation=`readlink -f $javaLocation`
    fi
fi
javaLocation=`dirname $javaLocation`

# If libjawt.so is not in the java bin directory, it might be in the lib or lib/arch dir
# It is needed for the 3D window in ParaProf.
if [ ! -r $javaLocation/libjawt.so -a -r $javaLocation/../lib/libjawt.so ]
then
  javaLocation="$javaLocation:$javaLocation/../lib:$javaLocation/../lib/x86_64"
fi

if [ ! -r $javaLocation/libjawt.so -a -r $javaLocation/../lib/amd64/libjawt.so ]
then
  javaLocation="$javaLocation:$javaLocation/../lib:$javaLocation/../lib/amd64"
fi

if [ "$MACHINE" = arm_linux ]
then
  javaLocation="$javaLocation:$javaLocation/../lib:$javaLocation/../lib/arm:$javaLocation/../lib/arm/server"
#  echo "javaLocation=$javaLocation"
  export LD_LIBRARY_PATH=$javaLocation:${LIBDIR}:${LD_LIBRARY_PATH}
fi

if [ "$MACHINE" = bgq  -o "$MACHINE" = bgp -o "$MACHINE" = bgl -o "$MACHINE" = ppc64 ]
then
  javaLocation="$javaLocation:$javaLocation/../lib:$javaLocation/../lib/ppc64"
  export LD_LIBRARY_PATH=$javaLocation:${LIBDIR}:${LD_LIBRARY_PATH}
fi




# run the user setup script
$BINDIR/tau_user_setup.sh

# Launch!
# Removed: -Dsun.java2d.opengl=true for arm64.
java $MEMORY ${JAVA2D_OPENGL_OPT} -Dderby.system.home=${HOME}/.ParaProf -Dpython.home=$JAR_HOME/jython -Dpython.verbose=error -Djava.library.path=${LIBDIR}:${javaLocation} -cp ${JARS} ${APPLE_OPTIONS} edu/uoregon/tau/paraprof/ParaProf -j $JARDIR -c $SCHEMADIR $*
retVal=$?
if [ $retVal -ne 0 ]; then
  echo "Some OpenGL/rendering and other errors can be overcome using the following options: "
  echo "  -libgl_indirect 		(Turns on LIBGL_ALWAYS_INDIRECT)"
  echo "  -disable_java2d_opengl 	(Turns off java2d OpenGl rendering. Can fix title bar rendering errors on OSX)"
  echo "  -64/-32 			(Disables/Forces 32 bit memory capacity calculation and use of -d32 flag on OSX)"
  echo "  -jogl1/-jogl2 		(force use of old (jogl1)/new(jogl2) 3d libraries)"
fi
exit $retVal	
