#!/bin/bash
#
# jRTLogProcessor
#
# Written by Gil Tene, and released to the public domain, as explained at
#  http://creativecommons.org/publicdomain/zero/1.0/
#
JRT_Version=2.0.1
#
# jRTLogProcessor will process an inpit log and can generate two
# different log files from a single jRT histogram log file: a
# sequential interval log file and a histogram log file.
#
# The sequential interval log file logs a single %'ile stats line for
# each reporting interval.
#
# The histogram percentile log file includes a detailed %'ile histogram
# of the entire log file range.
#
# jRTLogProcessor will process an input log file when provided with
# the -i <filename> option. When no -i option is provided, standard input
# will be processed.
#
# When provided with an output file name <logfile> with the -o option
# (e.g. "-o mylog"), jRTLogProcessor will produce both output files
# under the names <logfile> and <logfile>.hgrm (e.g. mylog and mylog.hgrm).
#
# When not provided with an output file name, jRTLogProcessor will
# produce [only] the histogram percentile log output to standard output.
#
# jRTLogProcessor accepts optional -start and -end time range
# parameters. When provided, the output will only reflect the portion
# of the input log with timestamps that fall within the provided start
# and end time range parameters.
#
# jRTLogProcessor also accepts and optional -csv parameter, which
# will cause the output formatting (of both output file forms) to use
# a CSV file format.
#

# Figure out installed path:
# On Linux, we'd do the following:
# PARSED_SCRIPT=`readlink -f $0`
# INSTALLED_PATH=`dirname $PARSED_SCRIPT`
# But readlink -f doesn't work the same everywhere (e.g. Mac OS). We use this instead:
function readlink_f () { _=`pwd`; cd `dirname $1` && echo `pwd` && cd $_; }
INSTALLED_PATH=$(readlink_f $0)

# Check if running from unpacked distribution archive by assuming jRT.jar
# in the same directory as this script. If not, try to search in target/ directory
# (running from the source repository build).
JRT_JAR_FILE=$INSTALLED_PATH/jRT.jar
if [ ! -f $JRT_JAR_FILE ] ; then
  JRT_JAR_FILE=$INSTALLED_PATH/target/jRT.jar
fi

JAVA_BIN=`which java`

if [ $JAVA_HOME ]; then
    JAVA_CMD=$JAVA_HOME/bin/java
elif [ $JAVA_BIN ]; then
    JAVA_CMD=$JAVA_BIN
else
    echo "For this command to run, either $JAVA_HOME must be set, or java must be in the path."
    exit 1
fi

#
# Parse original java execution arguments:
#
# At this point, we should have valid $PARSED_BinJava, $PARSED_JavaArgs, $PARSED_AppArgs:
#echo PARSED_BinJava = "$PARSED_BinJava"
#echo PARSED_JavaArgs = "$PARSED_JavaArgs"
#echo PARSED_AppArgs = "$PARSED_AppArgs"

# Deal with Windows/cygwin path normalization syntax needs:
# Key Assumption: only cygwin/Windows installations will have a cygpath command...
cygpath -w $JRT_JAR_FILE &> /dev/null
if [ $? -eq 0 ] ; then
    # if using cygwin, use valid windows-style classpath
    JRT_JAR_FILE=`cygpath -w $JRT_JAR_FILE`
	echo Windows path for jRT jar file is $JRT_JAR_FILE
fi

exec $JAVA_CMD -cp $JRT_JAR_FILE org.jrt.internal.hdrhistogram.HistogramLogProcessor $@
#exec $CMD
