#!/bin/sh

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

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

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

echo "Using VUDBC=$VUDBC"

#
# Determine the version of the JRE to use.
#
# If the variable VUDBC_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 "${VUDBC_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 VUDBC_JRE_ROOT"
        exit 1
    fi

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

if [ -z "${_JRE_BINDIR}" ]
then
    echo "Using VUDBC_JRE_ROOT=$VUDBC_JRE_ROOT"
else
    echo "Using VUDBC_JRE_ROOT=$_JRE_BINDIR"
fi
# Search for additional shared libraries.
if [ "`uname`" = "AIX" ]; then
  LIBPATH=`AddPath $VUDBC/native/lib $LIBPATH`
  export LIBPATH
  echo "Using LIBPATH=$LIBPATH"
elif [ "`uname`" = "HP-UX" ]; then
  SHLIB_PATH=`AddPath $VUDBC/native/lib $SHLIB_PATH`
  export SHLIB_PATH
  echo "Using SHLIB_PATH=$SHLIB_PATH"
else
  LD_LIBRARY_PATH=`AddPath $VUDBC/native/lib $LD_LIBRARY_PATH`
  export LD_LIBRARY_PATH
  echo "Using LD_LIBRARY_PATH=$LD_LIBRARY_PATH"
fi

#
# Now set classpath and invoke VUDBCCFG utility
#

# Search for additional jar file libraries.

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

while [ $# -ne 0 ]
do
   case "$1" in 
       -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


VUDBC_CLASSPATH=$VUDBC_CLASSPATH:$CLASSPATH

if [ -z "${_JRE_BINDIR}" ]
then
    $VUDBC_JRE_ROOT/bin/java -classpath $VUDBC_CLASSPATH $javaoptions com.iscobol.utility.VUDBCCFG $cobolarg 
else
    $_JRE_BINDIR/java -classpath $VUDBC_CLASSPATH $javaoptions com.iscobol.utility.VUDBCCFG $cobolarg 
fi
