#!/usr/bin/env bash

if [ -z "$BIN_DIR" ] || [ -z "$INST_DIR" ] || [ -z "$MESH_HOME" ]; then
    echo "$0 is not intended to be run directly. Run start-mesh.sh instead"
    exit 1
fi

# Occasionally Atlassian Support may recommend that you set some specific JVM arguments.  You can use this
# variable to do that. Simply uncomment the below line and add any required arguments. Note however, if this
# environment variable has been set in the environment of the user running this script, uncommenting the below
# will override that.
#
#JVM_SUPPORT_RECOMMENDED_ARGS=

# The following 2 settings control the minimum and maximum memory allocated to the Java virtual machine.
# For larger instances, the maximum amount will need to be increased.
#
if [ -z "${JVM_MINIMUM_MEMORY}" ]; then
    JVM_MINIMUM_MEMORY=512m
fi
if [ -z "${JVM_MAXIMUM_MEMORY}" ]; then
    JVM_MAXIMUM_MEMORY=512m
fi

# Uncommenting the following will set the umask for the app. It can be used to override the default
# settings for the service user if they are not sufficiently secure.
#
# umask 0027

UMASK=$(umask)
UMASK_SYMBOLIC=$(umask -S)
if echo "$UMASK" | grep -qv '0[2367]7$'; then
    FORCE_EXIT=false

    echo -e "\nBitbucket Mesh is being run with a umask that contains potentially unsafe settings."
    echo "The following issues were found with the mask \"$UMASK_SYMBOLIC\" ($UMASK):"
    if echo "$UMASK" | grep -qv '7$'; then
        echo " - Access is allowed to 'others'. It is recommended that 'others' be denied"
        echo "   all access for security reasons."
    fi
    if echo "$UMASK" | grep -qv '[2367][0-9]$'; then
        echo " - Write access is allowed to 'group'. It is recommend that 'group' be"
        echo "   denied write access. Read access to a restricted group is recommended"
        echo "   to allow access to the logs."
    fi
    if echo "$UMASK" | grep -qv '0[0-9][0-9]$'; then
        echo " - Full access has been denied to 'user'. Bitbucket Mesh cannot be run without full"
        echo "   access being allowed."
        FORCE_EXIT=true
    fi
    echo "The recommended umask for Bitbucket Mesh is \"u=,g=w,o=rwx\" (0027) and can be"
    echo "configured in _start-mesh.sh"

    if [ "x${FORCE_EXIT}" = "xtrue" ]; then
        exit 1;
    fi
fi

MAX_OPEN_FILES=6192
if [ $(ulimit -n) -lt ${MAX_OPEN_FILES} ]; then
    echo -e "\nThe current open files limit is set to less than $MAX_OPEN_FILES\nAttempting to increase limit..."
    if ulimit -n $MAX_OPEN_FILES; then
        echo -e "\tLimit increased to $MAX_OPEN_FILES open files"
    else
        echo -e "\n Couldn't increase file limit to $MAX_OPEN_FILES\nTrying a lower number..."
        MAX_OPEN_FILES=4096
        if ulimit -n $MAX_OPEN_FILES; then
          echo -e "\tLimit increased to $MAX_OPEN_FILES open files"
        else
          echo -e "Warning: Open file limit could not be increased. You may experience problems under heavy load"
        fi
    fi
fi

if [ ! -d "$MESH_HOME/log" ]; then
    mkdir -p "$MESH_HOME/log"
    if [ $? -ne 0 ]; then
        echo "$MESH_HOME/log could not be created. Permissions issue?"
        echo "Bitbucket Mesh was not started"
        exit 1
    fi
fi

source $BIN_DIR/set-jmx-opts.sh
if [ $? -ne 0 ]; then
    exit 1
fi

# Java 11 no longer supports setting sun.jnu.encoding via the command line. With nothing set in the environment
# to influence sun.jnu.encoding it will typically default to ANSI_X3.4-1968 on Linux.
if [ -z "$LANG" ] ; then
    export LANG="en_US.UTF-8"
fi

MESH_ARGS="-Dmesh.home=$MESH_HOME"
JVM_FILE_ENCODING_ARGS="-Dfile.encoding=UTF-8 -Dsun.jnu.encoding=UTF-8"
JVM_JAVA_ARGS="-Djava.io.tmpdir=$MESH_HOME/tmp -Djava.library.path=$JVM_LIBRARY_PATH"
JVM_MEMORY_ARGS="-Xms$JVM_MINIMUM_MEMORY -Xmx$JVM_MAXIMUM_MEMORY -XX:+UseG1GC"
JVM_REQUIRED_ARGS="$JVM_MEMORY_ARGS $JVM_FILE_ENCODING_ARGS $JVM_JAVA_ARGS"

JAVA_OPTS="-classpath $INST_DIR/bin $JAVA_OPTS $MESH_ARGS $JMX_OPTS $JVM_REQUIRED_ARGS $JVM_SUPPORT_RECOMMENDED_ARGS"
LAUNCHER="-jar $BIN_DIR/mesh-app.jar"

echo -e "\nStarting Bitbucket Mesh"
if [ "$1" == "run" ]; then
    $JAVA_BINARY $JAVA_OPTS $LAUNCHER --logging.console=true
else
    nohup $JAVA_BINARY $JAVA_OPTS $LAUNCHER >> $MESH_HOME/log/mesh-launcher.log 2>&1 &
    echo $! > "$MESH_HOME/log/mesh.pid"

    if [ $? -eq 0 ]; then
        echo -e "Bitbucket Mesh has been started.\n"
        echo "Logs are available at ${MESH_HOME}/log/mesh-launcher.log and"
        echo "${MESH_HOME}/log/atlassian-mesh.log"
    else
        if [ -f "${MESH_HOME}/log/mesh.pid" ]; then
            # If the application failed to start, remove the PID file
            rm "${MESH_HOME}/log/mesh.pid"
        fi

        echo "There was a problem starting Bitbucket Mesh"
    fi
fi
