#!/bin/sh
TAUROOT=/home/users/sameer/fresh/f/head/release/tau2
MACHINE=default

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

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


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}

APPLE_OPTIONS=""
if [ "$MACHINE" = "apple" ]; then
    APPLE_OPTIONS="-d32 -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"
		;;
	    --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



# 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
    MEMORY="-Xmx${testmax}m"
fi
if [ "x$platform" = "xDarwin" ] ; then 
   if [ $testmax -gt 819200 -a $testmax -lt 819200000 ] ; then
      MEMORY="-Xmx2000m"
   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`


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

# Launch!
java $MEMORY -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 $*	
