#!/bin/sh

# easydb: wrapper to simplify the execution of Database Bridge
# Copyright notice: Copyright (c) 2005 - 2024 Veryant
#
# Determine the version of ISCOBOL to use.
#
# If ISCOBOL has been set by the user, use that, otherwise, determine
# the location of this script (which is known to be $ISCOBOL/bin/edbiis)
# and derive the value of $ISCOBOL from that.
#

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

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

if [ -f "$ISCOBOL/bin/default_java.conf" ]
then
. $ISCOBOL/bin/default_java.conf
fi

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

if [ -z "${ISCOBOL_JRE_ROOT}" ]
then
  if [ -z "${ISCOBOL_JDK_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 ISCOBOL_JRE_ROOT"
        exit 1
    fi

    _JRE_BINDIR=`dirname $_JAVA_LOCATION`
    ISCOBOL_JRE_ROOT=`dirname $_JRE_BINDIR`
    if [ ! -f "$ISCOBOL_JRE_ROOT/bin/java" ]
    then
        ISCOBOL_JRE_ROOT=`dirname $ISCOBOL_JRE_ROOT`
        if [ ! -f "$ISCOBOL_JRE_ROOT/bin/java" ]
        then
            echo "ERROR: Could not locate java runtime."
            echo "Please ensure that ISCOBOL_JRE_ROOT is set correctly."
            exit 1
        fi
    fi
  else
    # if ISCOBOL_JDK_ROOT is set, then use it
    ISCOBOL_JRE_ROOT=$ISCOBOL_JDK_ROOT/jre
  fi
fi

# Search for additional shared libraries.
if [ "`uname`" = "AIX" ]; then
  LIBPATH=`AddPath $ISCOBOL/native/lib $LIBPATH`
  export LIBPATH
elif [ "`uname`" = "HP-UX" ]; then
  SHLIB_PATH=`AddPath $ISCOBOL/native/lib $SHLIB_PATH`
  export SHLIB_PATH
elif [ "`uname`" = "Darwin" ]; then
  DYLD_LIBRARY_PATH=`AddPath $ISCOBOL/native/lib $DYLD_LIBRARY_PATH`
  export DYLD_LIBRARY_PATH
else
  LD_LIBRARY_PATH=`AddPath $ISCOBOL/native/lib $LD_LIBRARY_PATH`
  export LD_LIBRARY_PATH
fi

#
# Now set classpath and invoke Isrun isCOBOL main runtime
#

# Search for additional jar file libraries.

for f in $ISCOBOL/jars/*.jar
do
  if [ -z "${ISCOBOL_CLASSPATH}" ]
  then
    ISCOBOL_CLASSPATH=$f
  else
    ISCOBOL_CLASSPATH=$ISCOBOL_CLASSPATH:$f
  fi
done

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

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

ISCOBOL_CLASSPATH=$ISCOBOL_CLASSPATH:$CLASSPATH

if [ ! "${ISCOBOL_DISPLAY}" = "" -a -z "$SERVER_OPTIONS" ]
then
    $ISCOBOL/native/shell/runclient ${cobolarg}
elif [ ! "${ISCOBOL_ISJCTERM}" = ""  ]
then
   printf "\033\033\033START_IS_PRG\033${cobolarg}\033"
   numenv=`env | wc -l`
   printf "${numenv}\033"
   env | while read ENVVALUE
   do
      printf "${ENVVALUE}\033"
   done
   if [ "$?" = "0" ]
   then
      if [ ! "${ISCOBOL_ISJCTERM}" = ""  ]
      then
         while [ "1" = "1" ]
         do
            read a
            eval $a
         done
      fi
   fi   
else
   if [ -z "${_JRE_BINDIR}" ]
   then
       $ISCOBOL_JRE_ROOT/bin/java -classpath $ISCOBOL_CLASSPATH $javaoptions com.iscobol.easydb.EdbiIs $cobolarg
   else
       $_JRE_BINDIR/java -classpath $ISCOBOL_CLASSPATH $javaoptions com.iscobol.easydb.EdbiIs $cobolarg
   fi 
fi
