#!/bin/sh
TAUROOT=/usr
MACHINE=

SCHEMADIR=${TAUROOT}/etc
BINDIR=${TAUROOT}/${MACHINE}/bin
LIBDIR=/usr/share/java
JARDIR=/usr/share/java

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-j2.jar:${JARDIR}/jargs.jar:${JARDIR}/batik-combined.jar:${JARDIR}/jfreechart-1.0.12.jar:${JARDIR}/jcommon-1.0.15.jar:${JARDIR}/jython.jar

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+
test=`java -cp ${JARS} edu.uoregon.tau.common.VersionTester 1.4`
if [ $test = "failed" ] ; then
    echo  ""
    echo "Java 1.4 or newer is required to run ParaProf."
    echo "Please update your Java SDK to a newer version to use ParaProf."
    echo "The final TAU version with Java 1.3 support was TAU 2.18.1."
    echo ""
    exit
fi




# Default to 800m heap space
MEMORY=-Xmx800m

# 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


# 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 $*	
