#!/bin/sh

# stream2wrk: wrapper to semplify the execution of stream2wrk utility
# Copyright notice: Copyright (c) 2005 - 2025 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/stream2wrk)
# 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
# derive the location of the JRE from the version of 'java' that is
# on the user's path
#

if [ -z "${ISCOBOL_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 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
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 stream2wrk utility
#

# 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-`" ;;
         *) cobolarg="$cobolarg $1";;          
   esac
   shift
done

ISCOBOL_CLASSPATH=$ISCOBOL_CLASSPATH:$CLASSPATH

if [ -z "${_JRE_BINDIR}" ]
then
    $ISCOBOL_JRE_ROOT/bin/java -classpath $ISCOBOL_CLASSPATH $javaoptions com.iscobol.utility.Stream2Wrk $cobolarg 
else
    $_JRE_BINDIR/java -classpath $ISCOBOL_CLASSPATH $javaoptions com.iscobol.utility.Stream2Wrk $cobolarg 
fi
