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

LIBDIR=/usr/share/java
JARDIR=/usr/share/java


# If paraprof is invoked with the -p option, read and/or generate pprof.dat
PPROFOPT=""
if [ "x$1" = "x-p" ] ; then
    shift
    echo "Using pprof to generate pprof.dat"
    pprof_path=${TAUROOT}/${MACHINE}/bin/pprof
    if [ "x$pprof_path" = "x" ] ; then
	echo "pprof not found ... will start ParaProf anyway."
	echo "Unless you have a pprof.dat file already present,"
	echo "you will have to load one manually."
    else
	echo "Found pprof in ${pprof_path}"
	if [ -r pprof.dat ] ; then
	    echo "Found pprof.dat"
	    if [ profile.0.0.0 -nt pprof.dat ] ; then
		echo "Regenerating pprof.dat file ..." 
		${pprof_path} -d > pprof.dat
	    else 
		echo "pprof.dat is current"
	    fi
	else
	    if [ -r profile.0.0.0 ] ; then
		echo "Please wait while I generate the pprof.dat file ..."
		${pprof_path} -d > pprof.dat
	    else
		for i in MULTI__* ; do
		    echo "Looking for pprof.dat file in $i directory ..."
		    if [ -r $i/pprof.dat ] ; then
			echo "Found $i/pprof.dat"
			if [ $i/profile.0.0.0 -nt $i/pprof.dat ] ; then
			    echo "Regenerating $i/pprof.dat file ..."
			    ${pprof_path} -d -f $i/profile > $i/pprof.dat
			else
			    echo "$i/pprof.dat file is current"
			fi
		    else
			echo "Please wait while I generate the $i/pprof.dat file ..."
			${pprof_path} -d -f $i/profile > $i/pprof.dat
		    fi
		done   
	    fi
	fi
      echo "Done ... now starting ParaProf ..."
    fi
    PPROFOPT="-f pprof pprof.dat"
fi 


if [ "$MACHINE" = "apple" ]; then
    APPLE_OPTIONS="-Xdock:name=ParaProf -Xdock:icon=${TAUROOT}/${MACHINE}/lib/tau-medium.png -Dapple.laf.useScreenMenuBar=true -Dcom.apple.mrj.application.growbox.intrudes=true"
else
    APPLE_OPTIONS=""
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
PPROFOPT="${PPROFOPT} -t ${TAUROOT} -a ${MACHINE}"
# Invoke ParaProf with the optional PPROFOPT argument. For large profiles, increase 1000m (1000MB). If you run out of memory, decrease this number.
if [ "$OLDJDK" = "yes" ] ; then
    JARS=${JARDIR}/paraprof-1.3.jar:${JARDIR}/perfdmf.jar:${JARDIR}/tau-common.jar:${JARDIR}/jatha.jar:${JARDIR}/jgraph-1.3.jar:${JARDIR}/xerces-j2.jar:${JARDIR}/jargs.jar:{JARDIR}/jfreechart0.jar:${JARDIR}/jcommon0.jar:${JARDIR}/jython.jar
else
    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}/jfreechart0.jar:${JARDIR}/jcommon0.jar:${JARDIR}/jython.jar
fi

# Launch!
java -Xmx800m -Dderby.system.home=${HOME}/.ParaProf -Dpython.verbose=error -Djava.library.path=${LIBDIR} -cp ${JARS} ${APPLE_OPTIONS} edu/uoregon/tau/paraprof/ParaProf ${PPROFOPT} $*	
