#!/bin/sh

#
# Determine the version of NoSQL Bridge to use.
#
# If VNOSQL has been set by the user, use that, otherwise, determine
# the location of this script (which is known to be $VUDBC/bin/visql)
# and derive the value of $VUDBC from that.
#

AddPath () {
  if [ -z "$2" ]; then
    echo $1
  else
    echo $1:$2
  fi
}

if [ -z "${VNOSQL}" ]
then
    D=`dirname $0`
    B=`basename $0`
    abspath="`cd \"${D}\" 2>/dev/null && pwd || echo \"${D}\"`/${B}"
    _BIN_DIR=`dirname ${abspath}`
    VNOSQL=`dirname ${_BIN_DIR}`
fi

echo "Using VNOSQL=$VNOSQL"

#
# Determine the version of the JRE to use.
#
# If the variable NOSQL_JRE_ROOT has been set, use that, otherwise
# derive the location of the JRE from the version of 'java' that is
# on the user's path
#

if [ -z "${NOSQL_JRE_ROOT}" ]
then

    _JAVA_LOCATION=`type java | cut -f3 -d' '` 

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

    if [ -z "$_JAVA_LOCATION" ]
    then
        echo "ERROR: Could not locate JRE. Please ensure that 'java' is"
        echo "on your PATH or set NOSQL_JRE_ROOT"
        exit 1
    fi

    _JRE_BINDIR=`dirname $_JAVA_LOCATION`
    NOSQL_JRE_ROOT=`dirname $_JRE_BINDIR`
    if [ ! -f "$VUDBC_JRE_ROOT/bin/java" ]
    then
        NOSQL_JRE_ROOT=`dirname $NOSQL_JRE_ROOT`
        if [ ! -f "$NOSQL_JRE_ROOT/bin/java" ]
        then
            echo "ERROR: Could not locate java runtime."
            echo "Please ensure that NOSQL_JRE_ROOT is set correctly."
            exit 1
        fi
     fi
fi

if [ -z "${_JRE_BINDIR}" ]
then
    echo "Using NOSQL_JRE_ROOT=$NOSQL_JRE_ROOT"
else
    echo "Using NOSQL_JRE_ROOT=$_JRE_BINDIR"
fi

#
# Now set classpath and invoke NoSQL Bridge
#

# Search for additional jar file libraries.
for f in $VNOSQL/jar/*.jar
do
  if [ -z "${NOSQL_CLASSPATH}" ]
  then
    NOSQL_CLASSPATH=$f
  else
    NOSQL_CLASSPATH=$NOSQL_CLASSPATH:$f
  fi
done

for f in $VNOSQL/nosqlbridge/*.jar
do
  if [ -z "${NOSQL_CLASSPATH}" ]
  then
    NOSQL_CLASSPATH=$f
  else
    NOSQL_CLASSPATH=$NOSQL_CLASSPATH:$f
  fi
done

for f in $VNOSQL/lib/*.jar
do
  if [ -z "${NOSQL_CLASSPATH}" ]
  then
    NOSQL_CLASSPATH=$f
  else
    NOSQL_CLASSPATH=$NOSQL_CLASSPATH:$f
  fi
done

while [ $# -ne 0 ]
do
   case "$1" in 
    -query) shift; query=$1 ;;
       -J*) javaoptions="$javaoptions `echo $1 | cut -c3-`" ;;
         *) cobolarg="$cobolarg $1";;          
   esac
   shift
done

# Determine the platform of provided native libraries

if [ -f "$VUDBC/native/lib/libdyncall_n.so" ]
then
  echo "Configuration:" `file $VUDBC/native/lib/libdyncall_n.*o | cut -f2 -d':' | cut -c2-`
fi

NOSQL_CLASSPATH=$NOSQL_CLASSPATH:$CLASSPATH

if [ -z "${_JRE_BINDIR}" ]
then
   JAVA=$NOSQL_JRE_ROOT/bin/java
else
   JAVA=$_JRE_BINDIR/java
fi

$JAVA -classpath $NOSQL_CLASSPATH $javaoptions com.veryant.nsb.launcher.Main --war=$VNOSQL/nosqlbridge/veryant-nsb.war $cobolarg
