#!/bin/sh

##
## Determine the absolute path of the installation directory.
##
dirname=`dirname "$0"`
WLP_INSTALL_DIR=`cd "$dirname/.." && pwd`
nativeEBCDIC=false

##
## Platform specific setup
##
PATHSEP_QUOTED=:
newline='
'
case `uname` in
  CYGWIN_*)
    WLP_INSTALL_DIR=`cygpath -m "${WLP_INSTALL_DIR}"`
    PATHSEP_QUOTED='\;'
    ;;

  OS/390)
    defaultFileEncoding=iso8859-1
    nativeEBCDIC=true         # Auto-convert server.env/jvm.options from ASCII-to-EBCDIC, if necessary.
    ;;
esac

##
## safeEcho: Portable echo that can handle backslashes and leading dashes.
safeEcho()
{
  cat <<EOF
$*
EOF
}

##
## Quote ${WLP_INSTALL_DIR} for eval.
##
WLP_INSTALL_DIR_QUOTED=\'`safeEcho "$WLP_INSTALL_DIR" | sed s/\'/\'\"\'\"\'/g`\'

##
## Detects the code page of the file and converts to EBCDIC,
## if necessary, before cat'ing.
##
## Only applicable if running in a native EBCDIC environment (z/OS).
##
## $1 the file name
## $2 pattern denoting the expected first char of file
readNativeFile() {
  if ${nativeEBCDIC}; then
    # ASCII 'm' overlaps with EBCDIC '_', so strip it out before detecting the codepage.
    # Note: cat used here to handle ASCII-tagged files.
    filecontents=`cat "$1" | iconv -f ISO8859-1 -t IBM-1047 | tr -d 'm\r\n'`
    case $filecontents in
      $2*) iconv -f ISO8859-1 -t IBM-1047 "$1" ;;  # ASCII file.
      *) cat "$1"                # EBCDIC file or ASCII-tagged file.
    esac
  else
    cat "$1"
  fi
}

##
## readServerEnv: Read server.env file and export environment variables.
readServerEnv()
{
  if [ -f "$1" ]; then
    saveIFS=$IFS
    IFS=$newline
    for line in `readNativeFile "$1" '[#_A-Za-z=]' | tr -d '\r'`; do
      case $line in
      \#*);;
      *=*)
        # Only accept alphanumeric variable names to avoid eval complexities.
        if var=`safeEcho "$line" | sed -e 's/^\([a-zA-Z0-9_][a-zA-Z0-9_]*\)=.*/\1/'`; then
          value=\'`safeEcho "$line" | sed -e 's/[^=]*=//' -e s/\'/\'\"\'\"\'/g`\'
          eval "$var=$value; export $var"
        fi
      esac
    done
    IFS=$saveIFS
  fi
}

readServerEnv "${WLP_INSTALL_DIR}/etc/default.env"
readServerEnv "${WLP_INSTALL_DIR}/etc/server.env"

if [ -z "${WLP_USER_DIR}" ]
then
  if [ -z "${WLP_DEFAULT_USER_DIR}" ]
  then
    WLP_DEFAULT_USER_DIR=${WLP_INSTALL_DIR}/usr
  fi
  WLP_USER_DIR=${WLP_DEFAULT_USER_DIR}
  export WLP_USER_DIR
fi

if [ -z "${WLP_OUTPUT_DIR}" ]
then
  if [ -z "${WLP_DEFAULT_OUTPUT_DIR}" ]
  then
    WLP_DEFAULT_OUTPUT_DIR=${WLP_USER_DIR}/servers
  fi
  WLP_OUTPUT_DIR=${WLP_DEFAULT_OUTPUT_DIR}
  export WLP_OUTPUT_DIR
fi

if [ -z "${JAVA_HOME}" ]
then
  if [ -z "${JRE_HOME}" ]
  then
    if [ -z "${WLP_DEFAULT_JAVA_HOME}" ]
    then
      # Use whatever java is on the path
      JAVA_CMD=java
    else
      JAVA_CMD=${WLP_DEFAULT_JAVA_HOME}/bin/java
    fi
  else
    JAVA_CMD=${JRE_HOME}/bin/java
  fi
else
  JAVA_CMD=${JAVA_HOME}/bin/java
fi

# Set a default file encoding if needed
if [ -n "$defaultFileEncoding" ]; then
  if ! expr "${JVM_ARGS} ${IBM_JAVA_OPTIONS}" : '.*\(-Dfile\.encoding\=[^[:space:]]\)' > /dev/null; then
    JVM_ARGS="${JVM_ARGS} -Dfile.encoding=$defaultFileEncoding"
  fi
fi

eval "set -- ${JVM_ARGS} -jar ${WLP_INSTALL_DIR_QUOTED}/bin/tools/ws-securityutil.jar "'"$@"'
"${JAVA_CMD}" "$@"
