#!/bin/sh
#
# Copyright (c) 2018-2022
# Beijing Baolande Software Corporation 
# 
# All Rights Reserved
#

cygwin=false
os400=false
case "`uname`" in
CYGWIN*) cygwin=true;;
OS400*) os400=true;;
esac

PRG="$0"

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

# Get standard environment variables
PRGDIR=`dirname "$PRG"`

# Only set BES_HOME if not already set
[ -z "$BES_HOME" ] && BES_HOME=`cd "$PRGDIR/.." >/dev/null; pwd`

# Copy BES_ROOT from BES_HOME if not already set
[ -z "$BES_ROOT" ] && BES_ROOT="$BES_HOME"

# Ensure that any user defined CLASSPATH variables are not used on startup,
# but allow them to be specified in setenv.conf, in rare case when it is needed.
CLASSPATH=

. "$BES_HOME/bin/setenv.conf"

PATCH_ENV_TYPE=bootstrap
. "$BES_HOME/bin/setpatchenv.conf"

# For Cygwin, ensure paths are in UNIX format before anything is touched
if $cygwin; then
  [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
  [ -n "$JAVA_BASE" ] && JAVA_BASE=`cygpath --unix "$JAVA_BASE"`
  [ -n "$BES_HOME" ] && BES_HOME=`cygpath --unix "$BES_HOME"`
  [ -n "$BES_ROOT" ] && BES_ROOT=`cygpath --unix "$BES_ROOT"`
  [ -n "$CLASSPATH" ] && CLASSPATH=`cygpath --path --unix "$CLASSPATH"`
  [ -n "$PATCH_CLASSPATH" ] && PATCH_CLASSPATH=`cygpath --path --unix "$PATCH_CLASSPATH"`
fi

# Ensure that neither BES_HOME nor BES_ROOT contains a colon
# as this is used as the separator in the classpath and Java provides no
# mechanism for escaping if the same character appears in the path.
case $BES_HOME in
  *:*) echo "Unable to start as BES_HOME contains a colon (:) character";
       exit 1;
esac

case $BES_ROOT in
  *:*) echo "Unable to start as BES_ROOT contains a colon (:) character";
       exit 1;
esac

# For OS400
if $os400; then
  # Set job priority to standard for interactive (interactive - 6) by using
  # the interactive priority - 6, the helper threads that respond to requests
  # will be running at the same priority as interactive jobs.
  COMMAND='chgjob job('$JOBNAME') runpty(6)'
  system $COMMAND

  # Enable multi threading
  export QIBM_MULTI_THREADED=Y
fi

# For Cygwin, switch paths to Windows format before running java
if $cygwin; then
  JAVA_HOME=`cygpath --absolute --windows "$JAVA_HOME"`
  JAVA_BASE=`cygpath --absolute --windows "$JAVA_BASE"`
  BES_HOME=`cygpath --absolute --windows "$BES_HOME"`
  BES_ROOT=`cygpath --absolute --windows "$BES_ROOT"`
  CLASSPATH=`cygpath --path --windows "$CLASSPATH"`
  PATCH_CLASSPATH=`cygpath --path --windows "$PATCH_CLASSPATH"`
  [ -n "$JAVA_ENDORSED_DIRS" ] && JAVA_ENDORSED_DIRS=`cygpath --path --windows "$JAVA_ENDORSED_DIRS"`
fi

CLASSPATH=$CLASSPATH:$BES_HOME/lib/bootstrap/bes-common-util.jar
CLASSPATH=$CLASSPATH:$BES_HOME/lib/bootstrap/bes-common-core.jar
CLASSPATH=$CLASSPATH:$BES_HOME/lib/bootstrap/bes-config-provider.jar
CLASSPATH=$CLASSPATH:$BES_HOME/lib/bootstrap/bes-gmssl.jar

if [ -n "$PATCH_CLASSPATH" ] ; then
    CLASSPATH="$PATCH_CLASSPATH:$CLASSPATH"
fi

$JAVA -Dcom.bes.installRoot="$BES_HOME" -Dcom.bes.instanceRoot="$BES_ROOT" $JAVA_OPTS -cp "$CLASSPATH" com.bes.enterprise.config.digest.DigestMain "$@"