#!/bin/bash
# Copyright (C) 2012 Intel Corporation. All rights reserved.

#---------------------------------------------------------------------
# JRE check (ret = 0 - supported jre version
#                  1 - unsupported jre version  (critical)
#                  2 - jre version not deteted
#                  3 - java not detected        (critical)
#---------------------------------------------------------------------
jre_check() {
    JRE_VER_SUPPORTED="1.6"
    ret=0
    
    which java 2>/dev/null 1>&2
    if [ $? -eq 0 ]; then
        local JRE_STRING="$(java $java_options -version 2>&1)"
        local JRE_VER_STRING="$(expr "$JRE_STRING" : '.*"\(.*\)".*')"

        JRE_VER_CUR="$(expr "$JRE_VER_STRING" : '\([0-9]*\.[0-9]*\)')"
        
        if [ -n "$JRE_VER_CUR" ]; then
            JRE_VER_SUPPORTED_MINOR=$(echo "$JRE_VER_SUPPORTED" | cut -d '.' -f2)
            JRE_VER_CUR_MINOR=$(echo "$JRE_VER_CUR" | cut -d '.' -f2)
            if [ $JRE_VER_CUR_MINOR -lt $JRE_VER_SUPPORTED_MINOR ]; then
                ret=1
            fi
        else
            ret=2
        fi
    else
        ret=3
    fi

    return ${ret}
}

set_gc_threads_count() {
    local orig_java_options="$java_options"
    local phys_cpu_core_count=$(cat /proc/cpuinfo | grep 'physical id' | sort | uniq | wc -l 2>/dev/null)
    if [ $phys_cpu_core_count -ge 2 ]; then
        java_options="-Xgcthreads$phys_cpu_core_count $orig_java_options"
    fi
    # check if option is supported (if not - drop it)
    if ! java $java_options -version 1>&2 2>/dev/null; then
        java_options="$orig_java_options"
    fi
}

# script start
thisdir=`dirname $0`
runningdir=`pwd`
args=$@

trap "" SIGTSTP #disable Ctrl-Z

# if ISM was called with arguments (means from some other program), it should run silently
silent_mode=0
if [ ${#@} -ne 0 ]; then
    silent_mode=1
fi

# set java_options with GC thread count
set_gc_threads_count

jre_check
case $? in
    1)
        if [ $silent_mode -eq 0 ]; then
            echo "WARNING: The Java* Runtime Environment (JRE) version $JRE_VER_CUR detected."
            echo "Sun* or Oracle* JRE 1.6 or higher is required to run Intel(R) Software Manager."
        fi
        exit 1
        ;;
    3)
        if [ $silent_mode -eq 0 ]; then
            echo "WARNING: The Java* Runtime Environment (JRE) is not detected."
            echo "Sun* or Oracle* JRE 1.6 or higher is required to run Intel(R) Software Manager."
        fi
        exit 1
        ;;
    *)
        if [ $silent_mode -eq 0 ]; then
            # check for OpenJDK
            if echo $(java $java_options -version 2>&1) | tr [:upper:] [:lower:] | grep -e 'open[ ]*jdk' &>/dev/null; then
                echo "WARNING: The OpenJDK* Runtime Environment is detected."
                echo "Sun* or Oracle* JRE 1.6 or higher is recommended to run Intel(R) Software Manager."
                echo
            fi
        fi
        ;;
esac

if echo $thisdir | grep -q -s ^/ || echo $thisdir | grep -q -s ^~ ; then
# absolute path
   fullpath="$thisdir"
else
# relative path 
   fullpath="$runningdir/$thisdir"
fi

system_cpu=`uname -m`
if [ "$system_cpu" == "ia64" ]; then
    echo "Unsupported arch!"
    exit -1
elif [ "$system_cpu" == "x86_64" ]; then
    my_arch=intel64
else
    my_arch=ia32
fi

export LD_LIBRARY_PATH="$fullpath/lib/$my_arch:$LD_LIBRARY_PATH"

# check if X is running
xset -q 1>/dev/null 2>&1
if [ $? -ne 0 ]; then
    if [ $silent_mode -eq 0 ]; then
        echo "ERROR: Unable to run Intel(R) Software Manager. Please make sure that X server is up and running."
    fi
    exit 1
fi

java_options="$java_options -Djava.rmi.server.codebase=file:///$fullpath/ism.jar -jar $fullpath/ism.jar $args"
if [ -n $HOME ]; then
    java_options="-Duser.home=$HOME $java_options"
fi

java $java_options 2>/dev/null 1>&2 &
