#!/bin/sh
#
#  Licensed to the Apache Software Foundation (ASF) under one or more
#  contributor license agreements.  See the NOTICE file distributed with
#  this work for additional information regarding copyright ownership.
#  The ASF licenses this file to You 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.

# --------------------------------------------------------------------
# $Rev: 926934 $ $Date: 2010-03-24 01:02:39 -0400 (Wed, 24 Mar 2010) $
# --------------------------------------------------------------------

# -----------------------------------------------------------------------------
# Deploy Script for the Geronimo Server
#
# You should not have to edit this file.  If you wish to have environment
# variables set each time you run this script refer to the information
# on the setenv.sh script that is called by this script below. 
#
# Invocation Syntax:
#
#   deploy [general options] command [command options] 
#
#   For detailed command usage information, just run deploy without any 
#   arguments.
#
# Environment Variable Prequisites:
#
#   GERONIMO_HOME   (Optional) May point at your Geronimo top-level directory.
#                   If not specified, it will default to the parent directory
#                   of the location of this script.
#                   The HOME directory holds the binary install of Geronimo.
#
#   GERONIMO_SERVER (Optional) May point at your Geronimo Server Instance
#                   top-level directory. If not specified, it will default to
#                   $GERONIMO_HOME.
#                   The SERVER directory holds the configuration and data for 
#                   a Geronimo instance.
#
#   GERONIMO_OPTS   (Optional) Java runtime options.
#
#   GERONIMO_TMPDIR (Optional) Directory path location of temporary directory
#                   the JVM should use (java.io.tmpdir). Defaults to var/temp
#                   (resolved to server instance directory GERONIMO_SERVER).
#
#   JAVA_HOME       Points to your Java Development Kit installation.
#                   JAVA_HOME doesn't need to be set if JRE_HOME is set.
#                   It is mandatory either JAVA_HOME or JRE_HOME are set.
#
#   JRE_HOME        Points to your Java Runtime Environment installation.
#                   Set this if you wish to run Geronimo using the JRE 
#                   instead of the JDK. Defaults to JAVA_HOME if empty.
#                   It is mandatory either JAVA_HOME or JRE_HOME are set.
#
#   JAVA_OPTS       (Optional) Java runtime options.
#
# Troubleshooting execution of this script file:
#
#  GERONIMO_ENV_INFO    (Optional) Environment variable that when set to
#                       "on" (the default) outputs the 
#                       values of GERONIMO_HOME, 
#                       GERONIMO_TMPDIR, JAVA_HOME, JRE_HOME before
#                       the command is issued. Set to "off" if you
#                       do want to see this information.
#
# Scripts called by this script:
# 
#   $GERONIMO_HOME/bin/setenv.sh
#                   (Optional) This script file is called if it is present.
#                   Its contents may set one or more of the above environment
#                   variables.  It is preferable (to simplify migration to
#                   future Geronimo releases) to set environment variables
#                   in this file rather than modifying Geronimo's script files.
#
#   $GERONIMO_HOME/bin/setjavaenv.sh
#                   This batch file is called to set environment variables
#                   relating to the java or jdb executable to invoke.
#                   This file should not need to be modified.
#
# Exit Codes:
#
#  0 - Success
#  1 - Error
# -----------------------------------------------------------------------------

# 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 GERONIMO_HOME if not already set
[ -z "$GERONIMO_HOME" ] && GERONIMO_HOME=`cd "$PRGDIR/.." ; pwd`

if [ -f "$GERONIMO_HOME"/bin/setenv.sh ]; then
  . "$GERONIMO_HOME"/bin/setenv.sh
fi

if [ -f "$GERONIMO_HOME"/bin/setjavaenv.sh ]; then
    BASEDIR="$GERONIMO_HOME"
    . "$GERONIMO_HOME"/bin/setjavaenv.sh
else
    echo "Cannot find $GERONIMO_HOME/bin/setjavaenv.sh"
    echo "This file is needed to run this program"
    exit 1
fi

# 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 "$JRE_HOME" ] && JRE_HOME=`cygpath --unix "$JRE_HOME"`
  [ -n "$GERONIMO_HOME" ] && GERONIMO_HOME=`cygpath --unix "$GERONIMO_HOME"`
  [ -n "$GERONIMO_SERVER" ] && GERONIMO_SERVER=`cygpath --unix "$GERONIMO_SERVER"`
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

# Classpath: Add the jars in the lib dir
for file in "$GERONIMO_HOME"/lib/*.jar
do
  if [ -z "$CLASSPATH" ]; then
    CLASSPATH="$file"
  else
    CLASSPATH="$CLASSPATH:$file"
  fi
done

# Endorsed dirs
ENDORSED_DIRS="$GERONIMO_HOME/lib/endorsed:$JRE_HOME/lib/endorsed"

# For Cygwin, switch paths to Windows format before running java
if $cygwin; then
  JAVA_HOME=`cygpath --absolute --windows "$JAVA_HOME"`
  JRE_HOME=`cygpath --absolute --windows "$JRE_HOME"`
  GERONIMO_HOME=`cygpath --absolute --windows "$GERONIMO_HOME"`
  GERONIMO_SERVER=`cygpath --absolute --windows "$GERONIMO_SERVER"`
  GERONIMO_TMPDIR=`cygpath --windows "$GERONIMO_TMPDIR"`
  CLASSPATH=`cygpath --path --windows "$CLASSPATH"`
  ENDORSED_DIRS=`cygpath --path --windows "$ENDORSED_DIRS"`
fi

# ----- Execute The Requested Command -----------------------------------------
if [ "$GERONIMO_ENV_INFO" != "off" ] ; then
  echo "Using GERONIMO_HOME:   $GERONIMO_HOME"
  echo "Using GERONIMO_SERVER: $GERONIMO_SERVER"
  echo "Using GERONIMO_TMPDIR: $GERONIMO_TMPDIR"
  echo "Using JRE_HOME:        $JRE_HOME"
fi

"$_RUNJAVA" $JAVA_OPTS $GERONIMO_OPTS \
  -Dkaraf.startLocalConsole=false -Dkaraf.startRemoteShell=false \
  -Dorg.apache.geronimo.home.dir="$GERONIMO_HOME" \
  -Dorg.apache.geronimo.server.dir="$GERONIMO_SERVER" \
  -Dkaraf.home="$GERONIMO_HOME" \
  -Dkaraf.base="$GERONIMO_SERVER" \
  -Djava.util.logging.config.file="$GERONIMO_SERVER"/etc/java.util.logging.properties \
  -Djava.endorsed.dirs="$ENDORSED_DIRS" \
  -Djava.io.tmpdir="$GERONIMO_TMPDIR" \
  -classpath "$CLASSPATH" \
  org.apache.geronimo.cli.deployer.DeployerCLI "$@"

# Remember the exit status
status=$?

# Reset tty
stty echo > /dev/null 2>&1

exit $status
