#!/bin/sh

# webcclient-testtool : wrapper to simplify the execution of webcclient-testtool process
# 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/webcclient-testtool)
# 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

if [ -z `command -v xvfb-run` ]; then
    echo "Unable to locate xvfb-run command. Please install Xvfb before starting WebClient." 
    exit 1
fi
if [ ! -z `command -v ldconfig` ]; then
    if [ `ldconfig -p | grep -i libxext.so | wc -l` -eq 0 ]; then 
        echo "Missing dependent library libXext."
        exit 1
    fi
    if [ `ldconfig -p | grep -i libxi.so | wc -l` -eq 0 ]; then
        echo "Missing dependent library libXi."
        exit 1
    fi
    if [ `ldconfig -p | grep -i libxtst.so | wc -l` -eq 0 ]; then
        echo "Missing dependent library libXtst"
        exit 1
    fi
    if [ `ldconfig -p | grep -i libxrender.so | wc -l` -eq 0 ]; then
        echo "Missing dependent library libXrender."
        exit 1
    fi
fi

echo ""
echo "Run webcclient-testtool expects X Server or xvfb-run to be running"
echo ""

sleep 1

#
# Now set classpath and invoke WebClient  Server
#

# Search for additional jar file libraries.

if [ -z "${ISCOBOL_CLASSPATH}" ]
then
    ISCOBOL_CLASSPATH=$ISCOBOL/webclient/test-tool
else
    ISCOBOL_CLASSPATH=$ISCOBOL_CLASSPATH:$ISCOBOL/webclient/test-tool
fi

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

cd $ISCOBOL/webclient/test-tool

if [ -z "${_JRE_BINDIR}" ]
then
$ISCOBOL_JRE_ROOT/bin/java -classpath $ISCOBOL_CLASSPATH $javaoptions -jar server/webclient-jetty-launcher.jar -w webclient-test-tool.war -testtool -j jetty.properties $cobolarg 
else
$_JRE_BINDIR/java -classpath $ISCOBOL_CLASSPATH $javaoptions -jar server/webclient-jetty-launcher.jar -w webclient-test-tool.war -testtool -j jetty.properties $cobolarg 
fi
