#!/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.
#============================================================
#   Control script for OpenEJB
#   --------------------------
#    
#   This script is the central entry point to 
#   all of OpenEJB's functions.
#  
#   Contributed by:
#
#    - David Blevins <david.blevins@visi.com>
#    - Daniel S. Haischt <daniel.haischt@daniel-s-haischt.biz>
#             
#               
# ___________________________________________________________
# $Rev$ $Date$
#============================================================

cygwin=false
case "`uname`" in
CYGWIN*) cygwin=true;;
esac

if [ -z "$OPENEJB_HOME" -o ! -d "$OPENEJB_HOME" ] ; then
  # find OpenEJB
  if [ -d /opt/openejb ] ; then
    OPENEJB_HOME=/opt/openejb
  fi

  if [ -d "${HOME}/opt/openejb" ] ; then
    OPENEJB_HOME="${HOME}/opt/openejb"
  fi

  ## resolve links - $0 may be a link to openejb's home
  PRG="$0"
  progname=`basename "$0"`

  # need this for relative symlinks
  while [ -L "$PRG" ] ; do
    ls=`ls -ld "$PRG"`
    link=`expr "$ls" : '.*-> \(.*\)$'`
    if expr "$link" : '/.*' > /dev/null; then
      PRG="$link"
    else
      PRG=`dirname "$PRG"`"/$link"
    fi
  done


  OPENEJB_HOME=`dirname "$PRG"`/..

  # make it fully qualified
  OPENEJB_HOME=`cd "$OPENEJB_HOME" && pwd`
fi

if $cygwin; then
  OPENEJB_HOME=`cygpath --path --windows "$OPENEJB_HOME"`
fi

#echo OPENEJB_HOME = $OPENEJB_HOME

OPENEJB_CORE_JAR="$OPENEJB_HOME/lib/openejb-core-${version}.jar"
OPENEJB_JAVAEE_JAR=$(echo "$OPENEJB_HOME"/lib/jakartaee-api*)
OPENEJB_CP="$OPENEJB_CORE_JAR:$OPENEJB_JAVAEE_JAR"
OPENEJB_JAVAAGENT_JAR="$OPENEJB_HOME/lib/openejb-javaagent-${version}.jar"

# Add the JAVA 9 specific start-up parameters required by OpenEJB
JDK_JAVA_OPTIONS="$JDK_JAVA_OPTIONS --add-opens=java.base/java.lang=ALL-UNNAMED"
JDK_JAVA_OPTIONS="$JDK_JAVA_OPTIONS --add-opens=java.base/java.io=ALL-UNNAMED"
JDK_JAVA_OPTIONS="$JDK_JAVA_OPTIONS --add-opens=java.base/java.util=ALL-UNNAMED"
JDK_JAVA_OPTIONS="$JDK_JAVA_OPTIONS --add-opens=java.base/java.util.concurrent=ALL-UNNAMED"
JDK_JAVA_OPTIONS="$JDK_JAVA_OPTIONS --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED"
JDK_JAVA_OPTIONS="$JDK_JAVA_OPTIONS --add-opens=java.base/jdk.internal.loader=ALL-UNNAMED"
JDK_JAVA_OPTIONS="$JDK_JAVA_OPTIONS --add-opens=java.base/java.net=ALL-UNNAMED"

export JDK_JAVA_OPTIONS

args="$@"
if [ "$1" = "debug" ]; then
    OPENEJB_OPTS="-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005 $OPENEJB_OPTS"
	args="start"
fi

java $JDK_JAVA_OPTIONS $OPENEJB_OPTS  "-Djava.util.logging.config.file=$OPENEJB_HOME/conf/logging.properties" "-javaagent:$OPENEJB_JAVAAGENT_JAR" -cp "$OPENEJB_CP"  org.apache.openejb.cli.Bootstrap $args
