#!/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.

# ----------------------------------------------------------------------------
# Main Script for the WSO2 Web Services Application Server v@wso2is_version@
#
# Environment Variable Prequisites
#
#   WSO2IS_HOME     Home of WSO2 Identity Solution 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.
#
# 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
  export QIBM_MULTI_THREADED=Y
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 Identity Solution 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 Identity Solution."
  exit 1
fi

# 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

export SERVER_NAME="WSO2 Identity Solution, v@wso2is_version@"

# ----- Process the input command ----------------------------------------------
if [ "$1" = "" ]; then
 CMD="$WSO2IS_HOME/lib/wso2wsas/bin/wso2wsas.sh"
elif [ "$1" = "-help" ] || [ "$1" = "--help" ] || [ "$1" = "help" ] || [ "$1" = "?" ]; then
  echo "Usage: wso2is.sh ( commands ... )"
  echo "commands:"
  echo "  --start        Start WSO2 Identity Solution as a background process"
  echo "  --run          Run WSO2 Identity Solution "
  echo "  --restart      Restart WSO2 Identity Solution "
  echo "  --stop         Stop WSO2 Identity Solution "
  echo "  --debug        Start WSO2 Identity Solution  in remote debugging mode.\
  You need to specify the debugging port after this argument."
  echo "  -version       What version of WSO2 Identity Solution  are you running?"
  exit 1
elif [ "$1" = "-version" ] || [ "$1" = "--version" ] || [ "$1" = "version" ]; then
  echo " WSO2 Identity Solution v @wso2is_version@ "
  exit 0
elif [ "$1" = "-stop" ]  || [ "$1" = "--stop" ] || [ "$1" = "stop" ]; then
  sh "$WSO2IS_HOME"/lib/wso2wsas/bin/shutdown.sh
  exit 0
elif [ "$1" = "-start" ]  || [ "$1" = "--start" ] || [ "$1" = "start" ]; then
  sh "$WSO2IS_HOME"/lib/wso2wsas/bin/startup.sh
  exit 0
elif [ "$1" = "-debug" ]  || [ "$1" = "--debug" ] || [ "$1" = "debug" ]; then
  echo " Starting $SERVER_NAME in debug mode"
  if [ "$2" = "" ]; then
    echo " Please specify the debug port after the --debug option"
    exit 1
  fi
  CMD="$WSO2IS_HOME/lib/wso2wsas/bin/wso2wsas.sh --debug $2"
else
  CMD="$1"
fi


sh $CMD
