#!/bin/sh
# ----------------------------------------------------------------------------
#  Copyright 2005,2006 WSO2, Inc. http://www.wso2.org
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

# ----------------------------------------------------------------------------
# Script for remort debugging of the WSO2 Identity Solution 1.0
#
# Environment Variable Prequisites
#
#   WSO2IS_HOME   Home of WSO2 IS installation. If not set I will  try
#                   to figure it out.
#
#   JAVA_HOME       Must point at your Java Development Kit installation.
#
#   JAVA_OPTS       (Optional) Java runtime options used when the commands
#                   is executed.
#   $1             Must remort port
#
# NOTE: Borrowed generously from Apache Tomcat startup scripts.
# -----------------------------------------------------------------------------

DARWIN_TOOLS=""
# OS specific support.  $var _must_ be set to either true or false.
cygwin=false;
darwin=false;
os400=false;
mingw=false;
case "`uname`" in
CYGWIN*) cygwin=true;;
MINGW*) mingw=true;;
OS400*) os400=true;;
Darwin*) darwin=true
        if [ -z "$JAVA_VERSION" ] ; then
             JAVA_VERSION="CurrentJDK"
           else
             echo "Using Java version: $JAVA_VERSION"
           fi
           if [ -z "$JAVA_HOME" ] ; then
             JAVA_HOME=/System/Library/Frameworks/JavaVM.framework/Versions/${JAVA_VERSION}/Home
             DARWIN_TOOLS=$JAVA_HOME/Classes/Classes.jar
           fi
           ;;
esac

# resolve links - $0 may be a softlink
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 WSO2IS_HOME if not already set
[ -z "$WSO2IS_HOME" ] && WSO2IS_HOME=`cd "$PRGDIR/.." ; pwd`

# Set AXIS2_HOME. Needed for One Click JAR Download
AXIS2_HOME=$WSO2IS_HOME

# 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 "$WSO2IS_HOME" ] && WSO2IS_HOME=`cygpath --unix "$WSO2IS_HOME"`
  [ -n "$AXIS2_HOME" ] && WSO2IS_HOME=`cygpath --unix "$WSO2IS_HOME"`
  [ -n "$CLASSPATH" ] && CLASSPATH=`cygpath --path --unix "$CLASSPATH"`
fi

# 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
  QIBM_MULTI_THREADED=Y
  export QIBM_MULTI_THREADED
fi

# For Migwn, ensure paths are in UNIX format before anything is touched
if $mingw ; then
  [ -n "$WSO2IS_HOME" ] &&
    WSO2IS_HOME="`(cd "$WSO2IS_HOME"; pwd)`"
  [ -n "$JAVA_HOME" ] &&
    JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`"
  [ -n "$AXIS2_HOME" ] &&
    WSO2IS_HOME="`(cd "$WSO2IS_HOME"; pwd)`"
  # TODO classpath?
fi

if [ -z "$JAVACMD" ] ; then
  if [ -n "$JAVA_HOME"  ] ; then
    if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
      # IBM's JDK on AIX uses strange locations for the executables
      JAVACMD="$JAVA_HOME/jre/sh/java"
    else
      JAVACMD="$JAVA_HOME/bin/java"
    fi
  else
    JAVACMD=java
  fi
fi

if [ ! -x "$JAVACMD" ] ; then
  echo "Error: JAVA_HOME is not defined correctly."
  echo " WSO2 WSAS cannot execute $JAVACMD"
  exit 1
fi

# if JAVA_HOME is not set we're not happy
if [ -z "$JAVA_HOME" ]; then
  echo "You must set the JAVA_HOME variable before running WSO2 WSAS."
  exit 1
fi


# update classpath
WSO2IS_CLASSPATH=""
for f in "$WSO2IS_HOME"/lib/*.jar
do
  WSO2IS_CLASSPATH="$WSO2IS_CLASSPATH":$f
done
WSO2IS_CLASSPATH="$JAVA_HOME/lib/tools.jar":"$WSO2IS_CLASSPATH":"$CLASSPATH":"$DARWIN_TOOLS"

# For Cygwin, switch paths to Windows format before running java
if $cygwin; then
  JAVA_HOME=`cygpath --absolute --windows "$JAVA_HOME"`
  WSO2IS_HOME=`cygpath --absolute --windows "$WSO2IS_HOME"`
  AXIS2_HOME=`cygpath --absolute --windows "$WSO2IS_HOME"`
  CLASSPATH=`cygpath --path --windows "$CLASSPATH"`
  JAVA_ENDORSED_DIRS=`cygpath --path --windows "$JAVA_ENDORSED_DIRS"`
fi

# --- THIS SCRIPT ONLY USES FOR DEBUGGING PURPOSES AND TEST ENVIRONMENTS. ----
CMD="--debug"
PORT=$1

if [ "$CMD" = "--stop" ]; then
  sh "$WSO2IS_HOME"/bin/shutdown.sh $*
  exit 0
elif [ "$CMD" = "--start" ]; then
  sh "$WSO2IS_HOME"/bin/startup.sh $*
  exit 0
elif [ "$CMD" = "--debug" ]; then
  if [ "$PORT" = "" ]; then
    echo " Please specify the debug port after the debug script to run"
    exit 1
  fi
  CMD="RUN"
  JAVA_OPTS="-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=$PORT"
fi

# ---------- Handle the SSL Issue with proper JDK version --------------------
jdk_15=`$JAVA_HOME/bin/java -version 2>&1 | grep 1.5`
jdk_16=`$JAVA_HOME/bin/java -version 2>&1 | grep 1.6`
jdk_14=`$JAVA_HOME/bin/java -version 2>&1 | grep 1.4`

if [ "$jdk_15" ]; then
   WSO2IS_CLASSPATH="$WSO2IS_HOME/lib/bcprov-jdk15-132.jar":$WSO2IS_CLASSPATH
elif [ "$jdk_14" ]; then
   WSO2IS_CLASSPATH="$WSO2IS_HOME/lib/bcprov-jdk13-132.jar":$WSO2IS_CLASSPATH
elif [ "$jdk_16" ]; then
   echo " [WARNING] WSO2 WSAS has not been fully tested on JDK 1.6"
   WSO2IS_CLASSPATH="$WSO2IS_HOME/lib/bcprov-jdk15-132.jar":$WSO2IS_CLASSPATH
else
   echo " [ERROR] WSO2 WSAS is supported only on JDK 1.4 and higher"
   exit 1
fi

#---------- Add jar files inside patches folder to classpath ------------------
WSO2IS_PATCH_CLASSPATH=""
for f in "$WSO2IS_HOME"/lib/patches/*.jar
do
  WSO2IS_PATCH_CLASSPATH="$WSO2IS_PATCH_CLASSPATH":$f
done

WSO2IS_CLASSPATH="$WSO2IS_HOME/lib/patches":"$WSO2IS_PATCH_CLASSPATH":"$WSO2IS_HOME/conf":"$WSO2IS_CLASSPATH"


# ----- Execute The Requested Command -----------------------------------------

cd "$WSO2IS_HOME"

exec "$JAVACMD" \
$JAVA_OPTS \
-classpath "$WSO2IS_CLASSPATH" \
-Djava.endorsed.dirs="$WSO2IS_HOME/lib/endorsed":"$JAVA_HOME/jre/lib/endorsed":"$JAVA_HOME/lib/endorsed" \
-Dwso2wsas.home="$WSO2IS_HOME" -Dwso2wsas.server.xml="$WSO2IS_HOME/conf/server.xml" -Dserver.script=$0 \
org.wso2.wsas.Main --debug $1
