#!/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: 539227 $ $Date: 2007-05-17 19:48:49 -0700 (Thu, 17 May 2007) $
##

# OS specific support (must be '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

PRGDIR=`dirname "$PRG"`

# Locate GSHELL_HOME if not it is not set
if [ "x$GSHELL_HOME" = "x" ]; then
    GSHELL_HOME=`cd "$PRGDIR/.."; pwd`
fi

# Execute optional environment  if provided by the user
if [ -r "$GSHELL_HOME"/bin/setenv.sh ]; then
    . "$GSHELL_HOME"/bin/setenv.sh
fi

# For Cygwin, ensure paths are in UNIX format before anything is touched
if $cygwin ; then
    [ -n "$JRE_HOME" ] && JRE_HOME=`cygpath --unix "$JRE_HOME"`
    [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
    [ -n "$GSHELL_HOME" ] && GSHELL_HOME=`cygpath --unix "$GSHELL_HOME"`
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
# (based upon Tomcat's setclasspath.sh but renamed since Geronimo's classpath
# is set in the JAR manifest)
if $os400; then
    # -r will Only work on the os400 if the files are:
    # 1. owned by the user
    # 2. owned by the PRIMARY group of the user
    # this will not work if the user belongs in secondary groups
    BASEDIR="$GSHELL_HOME"
    . "$GSHELL_HOME"/bin/setjavaenv.sh
else
    if [ -r "$GSHELL_HOME"/bin/setjavaenv.sh ]; then
        BASEDIR="$GSHELL_HOME"
        . "$GSHELL_HOME"/bin/setjavaenv.sh
    else
        echo "Cannot find $GSHELL_HOME/bin/setjavaenv.sh"
        echo "This file is needed to run this program"
        exit 1
    fi
fi

# 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"`
    GSHELL_HOME=`cygpath --absolute --windows "$GSHELL_HOME"`
fi

BOOTJAR="$GSHELL_HOME/lib/boot/gshell-bootstrap.jar"
if $cygwin ; then
    BOOTJAR=`cygpath --windows "$BOOTJAR"`
fi

# Start the JVM
exec "$_RUNJAVA" $JAVA_OPTS -jar "$BOOTJAR" "$@"

