#!/bin/bash

# exits with a non-zero return code if any part of script fails
set -e

#
#  Usage checked in java program: bior_count_catalog_misses -c <catalog_to_check> [ -o <output_file> --fixed ]
#
# NOTE: Script requires 3 jars. These are picked up by including $BIOR_LITE_HOME/lib in the classpath. They are:
#               bior_pipeline-XXXXXX.jar
#		htsjdk-1.143.jar
#		samtools-1.5.6.jar
# 

if [[ ! $BIOR_LITE_HOME ]]; then
    echo "The environment variable BIOR_LITE_HOME is not set. Please initialize BIOR in your environment using mayobiotools to run this script."
    exit 1
fi

which java >& /dev/null
if [ $? -ne 0 ];then
   echo "java is not configured - alter your PATH environment or set up using mayobiotools"
   exit 1
fi

# Fix for excessive virtual memory usage
# newer glibc will allocate 64MB per thread
# this will limit 64MB to all threads for a process
export MALLOC_ARENA_MAX=1

# check java version
REQUIRED_MINOR_VERSION=6

# grab full version string from running "java -version"
VERSION=`java -Xmx64m -version 2>&1 | grep "java version" | awk '{ print substr($3, 2, length($3)-2); }'`

# parse out the minor version number
MINOR_VERSION=`echo $VERSION | cut -d . -f 2`

# check minor version, anything less than the required version is invalid
if [ $MINOR_VERSION -lt $REQUIRED_MINOR_VERSION ]; then
        echo "Invalid Java version $VERSION.  Java 1.$REQUIRED_MINOR_VERSION or higher is required."
        echo "You can check your java version by running: java -version"
        exit 1
fi

class_path="$BIOR_LITE_HOME/lib/*:."
#echo "Executing script with classpath value: $class_path"

java -cp $class_path -Xmx2000m edu.mayo.bior.catalog.misses.TabixReaderTest $*
exitStatus=$?

exit $exitStatus
