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

# use default memory settings, if not specified
if [ -z "${JAVA_MAX_MEM}" ] ; then
    export JAVA_MAX_MEM="2G"
fi
if [ -z "${JAVA_MAX_PERM_MEM}" ] ; then
    export JAVA_MAX_PERM_MEM="256m"
fi
# use the default DNS TTL, if not specified
if [ -z "${DNS_TTL}" ] ; then
    export DNS_TTL="60"
fi

# OS specific support (must be 'true' or 'false').
    cygwin=false;
    darwin=false;
    aix=false;
    os400=false;
    case "`uname`" in
        CYGWIN*)
            cygwin=true
            ;;
        Darwin*)
            darwin=true
            ;;
        AIX*)
            aix=true
            ;;
        OS400*)
            os400=true
            ;;
    esac

# abort if java is not installed
if $cygwin ; then
    [ -n "$JAVA" ] && JAVA=`cygpath --unix "$JAVA"`
    [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
fi

if [ "x$JAVA_HOME" = "x" ] && [ "$darwin" = "true" ]; then
    JAVA_HOME="$(/usr/libexec/java_home -v 1.8)"
fi
if [ "x$JAVA" = "x" ] && [ -r /etc/gentoo-release ] ; then
    JAVA_HOME=`java-config --jre-home`
fi

if [ "x$JAVA" = "x" ]; then
    if [ "x$JAVA_HOME" != "x" ]; then
        if [ ! -d "$JAVA_HOME" ]; then
            echo "Aborting: JAVA_HOME is not valid: $JAVA_HOME"
            exit 1
        fi
        JAVA="$JAVA_HOME/bin/java"
    else
        echo "JAVA_HOME not set; results may vary"
        JAVA=`type java`
        JAVA=`expr "$JAVA" : '.* \(/.*\)$'`
        if [ "x$JAVA" = "x" ]; then
            echo "Aborting: java command not found"
            exit 1
        fi
    fi
fi

if [ -z "${BROOKLYN_PERSISTENCE_DIR}" ] ; then
    export BROOKLYN_PERSISTENCE_DIR="~/.brooklyn/brooklyn-persisted-state"
fi

# force resolution of localhost to be loopback
export EXTRA_JAVA_OPTS="-Dbrooklyn.location.localhost.address=127.0.0.1 ${EXTRA_JAVA_OPTS}"

# Increase garbage collection, see https://issues.apache.org/jira/browse/BROOKLYN-375
export EXTRA_JAVA_OPTS="-XX:SoftRefLRUPolicyMSPerMB=1 ${EXTRA_JAVA_OPTS}"

# Set the DNS TTL for the JVM
# By default, java does not refresh DNS records, ever. This is due to the default value of networkaddress.cache.ttl set to -1, i.e. cache forever.
# However, networkaddress.cache.ttl is not a system property, but a security property. It cannot be updated through from the JVM.
# But, using the old system property sun.net.inetaddr.ttl has the desirable effect (see: https://stackoverflow.com/a/17219327)
export EXTRA_JAVA_OPTS="-Dsun.net.inetaddr.ttl=${DNS_TTL} ${EXTRA_JAVA_OPTS}"

# Set the TLS protocol versions
export EXTRA_JAVA_OPTS="-Dhttps.protocols=TLSv1.1,TLSv1.2 ${EXTRA_JAVA_OPTS}"

# Set the persistence directory
export EXTRA_JAVA_OPTS="-Dbrooklyn.persistence.dir=${BROOKLYN_PERSISTENCE_DIR} ${EXTRA_JAVA_OPTS}"
