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

#
# handle specific scripts; the SCRIPT_NAME is exactly the name of the Karaf
# script; for example karaf, start, stop, admin, client, ...
#
# if [ "$KARAF_SCRIPT" == "SCRIPT_NAME" ]; then
#   Actions go here...
# fi

#
# general settings which should be applied for all scripts go here; please keep
# in mind that it is possible that scripts might be executed more than once, e.g.
# in example of the start script where the start script is executed first and the
# karaf script afterwards.
#
DIRNAME=$(dirname "$0")

# Set environment variable for DDF home directory (used by DDF Java code)
DDF_HOME=$(cd "${DIRNAME}/.."; pwd -P)

# NOTE: export on separate line to support caveat of Solaris OS
export DDF_HOME

# Prefer /dev/urandom over /dev/random on Linux systems.
OS="`uname`"
LINUX="Linux"

if [ "$OS" != "${OS%$LINUX*}" ]; then
   if [ -e /dev/urandom ]; then
      if [ -z "$EXTRA_JAVA_OPTS" ]; then
         EXTRA_JAVA_OPTS="-Djava.security.egd=file:/dev/./urandom"
      else
         EXTRA_JAVA_OPTS="$EXTRA_JAVA_OPTS -Djava.security.egd=file:/dev/./urandom"
      fi
   fi
fi

# Uncomment to enable cxf logging interceptors
# EXTRA_JAVA_OPTS="$EXTRA_JAVA_OPTS -Dcom.sun.xml.ws.transport.http.HttpAdapter.dump=true"

export EXTRA_JAVA_OPTS

#
# The following section shows the possible configuration options for the default 
# karaf scripts
#

# export JAVA_MIN_MEM=2g # Minimum memory for the JVM
# export JAVA_MAX_MEM=4g # Maximum memory for the JVM
# export KARAF_HOME # Karaf home folder
# export KARAF_DATA # Karaf data folder
# export KARAF_BASE # Karaf base folder



# The following defines an environment variable referencing our script to be executed by the JVM
# when errors are detected. The environment variable will be expanded by the JVM.
# The space is important at the end of the DDF_ON_ERROR value to separate the last argument from
# the %p used by the JVM to represent the pid of the JVM
export DDF_ON_ERROR="bin/ddf_on_error.sh script "

# Defines the special on-error Java options
JAVA_ERROR_OPTS="-XX:OnOutOfMemoryError=\${DDF_ON_ERROR}%p -XX:OnError=\${DDF_ON_ERROR}%p"

export KARAF_OPTS="-Dfile.encoding=UTF8"

export JAVA_OPTS="-Xms2g -Xmx6g -XX:+UnlockDiagnosticVMOptions -Dddf.home=${DDF_HOME} -Dddf.home.perm=${DDF_HOME}/ -XX:+DisableAttachMechanism $JAVA_ERROR_OPTS"
