#!/bin/sh
#
# loader for Sun Studio Performance Analyzer
#
# Copyright 04/26/05 Sun Microsystems, Inc. All Rights Reserved
#

# This string is here for the 'version' command:
# It gets sed'ed in at build-time:
# @(#)RELEASE VERSION Sun Performance Analyzer 7.5 Patch 121623-02 2006/05/25

#
# L10N Message translation utility
#	$1 - message id
#	$2 - fallback message text
#

GTXT()
{
    eval I18Ntxt$2="\"$1\""
}

# define all i18n messages
GTXT "Usage: %s {options} arguments\n\nOptions can be\n\n" 1
GTXT "\t-?|-h|--help\n\t    show usage and exit\n" 2
GTXT "\t-j|--jdkhome <path>\n\t    specify the JVM directory\n" 3
GTXT "\t-J<jvm_options>\n\t    pass <jvm_options> to JVM\n" 4
GTXT "\t-v|--verbose\n\t    enable verbose output\n" 5
GTXT "\t-V|--version\n\t    show version and exit\n" 6
GTXT "\nAll other options and arguments are passed to the Analyzer.\nSee documentations for details.\n" 7
GTXT "Cannot find JVM. Please set the JAVA_PATH environment variable to point\nto your JVM installation directory, or use the -j switch.\n" 8
GTXT "Cannot find JVM at \"%s\". Please set the JAVA_PATH\nenvironment variable to point to your JVM installation directory,\nor use the -j switch.\n" 9
GTXT "\t-f|--fontsize <size>\n\t    specify the font size to be used in the Analyzer\n" 10
GTXT "Unrecognized option \"%s\"\n" 11
GTXT "Sun Studio Default" 12
GTXT "WARNING: JDK_1_4_HOME is obsolete, and is ignored\n" 13

Message()
{
    text=I18Ntxt$1
    if [ -x /usr/dt/bin/dtdspmsg ]; then
	eval eval /usr/dt/bin/dtdspmsg -s 17 analyzer.cat $1 \\\"\\\${$text}\\\"  ${2:+"\\\"$2\\\""} ${3:+"\\\"$3\\\""} ${4:+"\\\"$4\\\""}
    else
        eval eval printf \\\"\\\${$text}\\\" ${2:+"\\\"$2\\\""} ${3:+"\\\"$3\\\""} ${4:+"\\\"$4\\\""}
    fi
}

Usage()
{
    Message 1 $0    # "Usage: %s {options} arguments\n\nOptions can be\n\n"
    Message 2       # "\t-?|-h|--help\n\t    show usage and exit\n"
    Message 3       # "\t-j|--jdkhome <path>\n\t    specify the JVM directory\n"
    Message 4       # "\t-J<jvm_options>\n\t    pass <jvm_options> to JVM\n"
    Message 10      # "\t-f|--fontsize <size>\n\t    specify the font size to be used in the Analyzer\n"
    Message 5       # "\t-v|--verbose\n\t    enable verbose output\n"
    Message 6       # "\t-V|--version\n\t    show version and exit\n"
    Message 7       # "\nAll other options and arguments are passed to the Analyzer.\nSee documentations for details.\n"
}

###########################################################################
# main program starts here

# defaults
# jdkhome will be really set below
# the jvmflags value set here can be augmented by a -J argument
# the verbose value set here can be overriden by a -v argument

jdkhome=""
java_how="?"
jvmflags="-Xmx1024m"
verbose="false"

# end of defaults

PRG=$0

PATH=/bin:/usr/bin:$PATH
export PATH

#
# resolve symlinks
#

while [ -h "$PRG" ]; do
    ls=`ls -ld "$PRG"`
    link=`expr "$ls" : '^.*-> \(.*\)$' 2>/dev/null`
    if expr "$link" : '^/' > /dev/null 2>/dev/null; then
	PRG="$link"
    else
	PRG="`dirname $PRG`/$link"
    fi
done

progdir=`dirname $PRG`
fdhome="$progdir/.."

#
# L10N path
#

NLSPATH="$progdir/../lib/locale/%L/LC_MESSAGES/%N.cat:$progdir/../lib/locale/%L/LC_MESSAGES/%N:$NLSPATH"
export NLSPATH

#
# absolutize fdhome
#

oldpwd=`pwd` ; cd $fdhome; fdhome=`pwd`; cd $oldpwd; unset oldpwd

jargs=$jvmflags

args=""

# parse arguments
while [ $# -gt 0 ] ; do
    case "$1" in
	-j|--jdkhome) shift; if [ $# -gt 0 ] ; then jdkhome=$1;java_how="-j";fi;;
	-J*) jopt=`expr $1 : '-J\(.*\)'`; jargs="$jargs \"$jopt\"";;
	-v|--verbose) verbose="true";;
	-V|--version) echo `basename $PRG`: "Sun Performance Analyzer 7.5 Patch 121623-02 2006/05/25"; exit 0;;
	-\?|-h|--help) Usage; exit 0;;
	-f|--fontsize) args="$args \"$1\"" ;;
	-*) Message 11 $1; Usage; exit 1;;
	*) args="$args \"$1\"" ;;
    esac
    shift
done

# if user did not specify a jdkhome, set default
if [ "$jdkhome" = "" ]; then
    # if Java was installed with the release, then use that
    # unless some other environment variable, or -j overrides it
    if [ "`uname`" = "Linux" ]; then
	INST_JAVA="/usr/java/j2sdk1.4.2_08"
    else
	INST_JAVA="/usr/jdk/j2sdk1.4.2_08"
    fi

    if [ -d "$INST_JAVA" ] ; then
	jdkhome="$INST_JAVA"
	java_how=I18Ntxt12
    else
	jdkhome=""
    fi

    # if JAVA_PATH is set it overrides the default in the script
    if [ ! -z "$JAVA_PATH" ] ; then
	jdkhome="$JAVA_PATH"
	java_how="JAVA_PATH"
    fi

    # if JDK_HOME is set it overrides the default and JAVA_PATH in the script
    if [ ! -z "$JDK_HOME" ] ; then
	jdkhome="$JDK_HOME"
	java_how="JDK_HOME"
    fi

    # JDK_1_4_HOME is obsolete, and ignored; just tell the user
    if [ ! -z "$JDK_1_4_HOME" ] ; then
	Message 13
    fi

    # if we still don't have a jdkhome, try the user's path
    if [ "$jdkhome" = "" ]; then
        # It is a trick. To ignore any output from .cshrc,
        # we run 'which' with two arguments and take the last line.
        javaloc=`/usr/bin/which java java 2>&1 | sed -n '$p'`
        if [ -f "$javaloc" ]; then
            javaloc1=`/usr/bin/dirname $javaloc`
            jdkhome=`/usr/bin/dirname $javaloc1`
            java_how="PATH"
        fi
    fi
fi

#
# check JVM
#

if [ -z "$jdkhome" ] ; then
    Message 8
    exit 1
fi

if [ ! -x "$jdkhome/bin/java" ] ; then
    Message 9 $jdkhome
    exit 1
fi

if [ $verbose = "true" ] ; then
	echo `basename $PRG`: "Sun Performance Analyzer 7.5 Patch 121623-02 2006/05/25";
	$jdkhome/bin/java -version;
fi

#
# run perftools_validate
#

anvalidate="$progdir/perftools_validate"
if [ -x "$anvalidate" ] ; then
    eval $anvalidate -j $jdkhome/bin/java -H \"$java_how\" -q
fi

eval $jdkhome/bin/java $jargs -jar $fdhome/lib/analyzer_st.jar $args
