#!/bin/sh
# ---------------------------------------------------------------------------
# Start/Stop Script for the WILLOW Server
#
# Environment Variable Prequisites
#
#   WILLOW_HOME       Base directory for resolving dynamic portions
#                     of a Willow installation.  If not present, resolves to
#                     the same directory that WILLOW_HOME points to.
#
#   WILLOW_BOOTSTRAP  Directory path location of plugin directory
#                     the WILLOW should use .  Defaults to %WILLOW_HOME%\bootstrap.
#                     
#
#   WILLOW_OPTS       (Optional) Java runtime options used when the "start",
#                     "stop", or "run" command is executed.
#
#   WILLOW_TMPDIR     (Optional) Directory path location of temporary directory
#                     the JVM should use (java.io.tmpdir).  Defaults to %WILLOW_HOME%\temp.
#                   
#
#   JAVA_HOME         Must point at your Java Development Kit installation.
#
#   JAVA_OPTS         (Optional) Java runtime options used when the "start",
#                     "stop", or "run" command is executed.
#
# ---------------------------------------------------------------------------

# OS specific support.  $var _must_ be set to either true or false.
cygwin=false
os400=false
case "`uname`" in
CYGWIN*) cygwin=true;;
OS400*) os400=true;;
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"`
WILLOW_HOME=`cd "$PRGDIR/.." ; pwd`
if [ -r "$WILLOW_HOME"/bin/setenv.sh ]; then
  . "$WILLOW_HOME"/bin/setenv.sh
fi

WILLOW_BOOTSTRAP="$WILLOW_HOME"/bootstrap

# 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 "$WILLOW_HOME" ] && WILLOW_HOME=`cygpath --unix "$WILLOW_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

# Get standard Java environment variables
if [ -r "$WILLOW_HOME"/bin/setclasspath.sh ]; then
  BASEDIR="$WILLOW_HOME"
  . "$WILLOW_HOME"/bin/setclasspath.sh
else
  echo "Cannot find $WILLOW_HOME/bin/setclasspath.sh"
  echo "This file is needed to run this program"
  exit 1
fi

# Add on extra jar files to CLASSPATH

CLASSPATH="$CLASSPATH":"$WILLOW_BOOTSTRAP"/bootstrap.jar

CLASSPATH="$CLASSPATH":"$WILLOW_BOOTSTRAP"
for lib_jar in $WILLOW_BOOTSTRAP/lib/*.jar
do
  CLASSPATH="$CLASSPATH":"$lib_jar" 
done

if [ -z "$WILLOW_TMPDIR" ] ; then
  # Define the java.io.tmpdir to use for Willow
  WILLOW_TMPDIR="$WILLOW_HOME"/temp
fi

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

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

echo "Using WILLOW_HOME:      $WILLOW_HOME"
echo "Using WILLOW_TMPDIR:    $WILLOW_TMPDIR"
echo "Using WILLOW_BOOTSTRAP: $WILLOW_BOOTSTRAP"
echo "Using JAVA_HOME:        $JAVA_HOME"
echo "Using CLASSPATH:        $CLASSPATH"

if [ "$1" = "embedded" ] ; then

  shift
  echo "Embedded Classpath: $CLASSPATH"
  exec "$_RUNJAVA" $JAVA_OPTS $WILLOW_OPTS \
    -Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" -classpath "$CLASSPATH" \
    -Dwillow.home="$WILLOW_HOME" \
    -Djava.io.tmpdir="$WILLOW_TMPDIR" \
    org.huihoo.willow.bootstrap.Embedded "$@"

elif [ "$1" = "run" ]; then

  shift 
   
  if [ "$1" = "-security" ] ; then
    echo "Using Security Manager"
    shift      
    exec "$_RUNJAVA" $JAVA_OPTS $WILLOW_OPTS \
      -Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" -classpath "$CLASSPATH" \
      -Djava.security.manager \
      -Djava.security.policy=="$WILLOW_BASE"/conf/willow.policy \
      -Dwillow.home="$WILLOW_HOME" \
      -Djava.io.tmpdir="$WILLOW_TMPDIR" \
      org.huihoo.willow.bootstrap.Bootstrap "$@" start   
  else
      exec "$_RUNJAVA" $JAVA_OPTS $WILLOW_OPTS \
        -Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" -classpath "$CLASSPATH" \
        -Dwillow.home="$WILLOW_HOME" \
        -Djava.io.tmpdir="$WILLOW_TMPDIR" \
        org.huihoo.willow.bootstrap.Bootstrap "$@" start
  fi
  
elif [ "$1" = "start" ] ; then

  shift
  touch "$WILLOW_HOME"/logs/Willow.out
  
  if [ "$1" = "-security" ] ; then
    echo "Using Security Manager"
    shift
    "$_RUNJAVA" $JAVA_OPTS $WILLOW_OPTS \
      -Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" -classpath "$CLASSPATH" \
      -Djava.security.manager \
      -Djava.security.policy=="$WILLOW_BASE"/conf/willow.policy \
      -Dwillow.home="$WILLOW_HOME" \
      -Djava.io.tmpdir="$WILLOW_TMPDIR" \
      org.huihoo.willow.startup.Bootstrap "$@" start \
      >> "$WILLOW_HOME"/logs/willow.out 2>&1 &
  
      if [ ! -z "$WILLOW_PID" ]; then
        echo $! > $WILLOW_PID
      fi
    
  else
    "$_RUNJAVA" $JAVA_OPTS $WILLOW_OPTS \
      -Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" -classpath "$CLASSPATH" \
      -Dwillow.home="$WILLOW_HOME" \
      -Djava.io.tmpdir="$WILLOW_TMPDIR" \
      org.huihoo.willow.startup.Bootstrap "$@" start \
      >> "$WILLOW_HOME"/logs/willow.out 2>&1 &
  
      if [ ! -z "$WILLOW_PID" ]; then
        echo $! > $WILLOW_PID
      fi
  fi

elif [ "$1" = "stop" ] ; then

  shift
  exec "$_RUNJAVA" $JAVA_OPTS $WILLOW_OPTS \
    -Djava.endorsed.dirs="$JAVA_ENDORSED_DIRS" -classpath "$CLASSPATH" \
    -Dwillow.home="$WILLOW_HOME" \
    -Djava.io.tmpdir="$WILLOW_TMPDIR" \
    org.huihoo.willow.startup.Bootstrap "$@" stop

  if [ "$1" = "-force" ] ; then
    shift
    if [ ! -z "$WILLOW_PID" ]; then
       echo "Killing: `cat $WILLOW_PID`"
       kill -9 `cat $WILLOW_PID`
    fi
  fi

else

  echo "Usage: willow.sh ( commands ... )"
  echo "commands:"
  echo "  embedded          Start Willow in embedded mode"
  echo "  run               Start Willow in the current window"
  echo "  run -security     Start in the current window with security manager"
  echo "  start             Start Willow in a separate window"
  echo "  start -security   Start in a separate window with security manager"
  echo "  stop              Stop Willow"
  exit 1

fi
