#!/bin/bash
# Copyright (c) 2014-2015 Spotify AB
# Copyright 2019-2019 The Last Pickle Ltd
#
#  Licensed 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.

set -e

find_reaper_jar() {
    local target_path
    target_path="$1"

    find -L "$target_path" \
        -maxdepth 4 \
        -regex '.*/cassandra-reaper-.*[0-9rT]\.jar' || true
}

# Do not search from '/' as it prints many permission errors (even for
# root user), notably for '/proc', '/run', etc.
# Also it can potentially take a huge amount of time.
if [ "$(pwd -P)" != "/" ]; then
    REAPER_JAR=$(find_reaper_jar ".")
fi

if [ $REAPER_JAR ]; then
    echo "Using reaper in target"
    CLASS_PATH=$REAPER_JAR
fi

if [ -z "$CLASS_PATH" ]; then
    echo "Looking for reaper under /usr"
    CLASS_PATH=""
    CLASS_PATH+=:$(find_reaper_jar "/usr/local/share")
    CLASS_PATH+=:$(find_reaper_jar "/usr/share")
fi

if [ $# -eq 0 ]; then
    CONFIG_PATH="/usr/local/etc/cassandra-reaper/cassandra-reaper.yaml"
    if [ ! -e "$CONFIG_PATH" ]; then
        CONFIG_PATH="/etc/cassandra-reaper/cassandra-reaper.yaml"
    fi
else
    CONFIG_PATH="$@"
fi

SSL_CONFIG_PATH="/etc/cassandra-reaper/cassandra-reaper-ssl.properties"
if [ -r "$SSL_CONFIG_PATH" ]; then
    echo "Loading SSL configuration from $SSL_CONFIG_PATH"
    # The `sed` expression below removes empty lines and comments
    # (i.e. lines starting with '#' character).
    mapfile -t JVM_OPTS <<< "$(cat "$SSL_CONFIG_PATH" | sed '/^\(#.*\)*$/d')"
fi

# Use JAVA_HOME if set, otherwise look for java in PATH
if [ -n "$JAVA_HOME" ]; then
    # Why we can't have nice things: Solaris combines x86 and x86_64
    # installations in the same tree, using an unconventional path for the
    # 64bit JVM.  Since we prefer 64bit, search the alternate path first,
    # (see https://issues.apache.org/jira/browse/CASSANDRA-4638).
    for java in "$JAVA_HOME"/bin/amd64/java "$JAVA_HOME"/bin/java; do
        if [ -x "$java" ]; then
            JAVA="$java"
            break
        fi
    done
else
    JAVA=java
fi

if [ -z $JAVA ] ; then
    echo Unable to find java executable. Check JAVA_HOME and PATH environment variables. >&2
    exit 1;
fi

if [ -z "$REAPER_HEAP_SIZE" ]; then
    REAPER_HEAP_SIZE="2G"
fi


JVM_OPTS=(
    # it is safe and performant to disable assertions in production
    # environments (ie replace `-ea` with `-da`)
    -ea
    -Xms${REAPER_HEAP_SIZE}
    -Xmx${REAPER_HEAP_SIZE}
    ${JVM_OPTS[@]}
    # Prefer binding to IPv4 network intefaces (when net.ipv6.bindv6only=1). See
    # http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6342561 (short version:
    # comment out this entry to enable IPv6 support).
    -Djava.net.preferIPv4Stack=true
    )

exec ${JAVA} ${JVM_OPTS[@]} \
    -cp ${CLASS_PATH} \
    io.cassandrareaper.ReaperApplication \
    server ${CONFIG_PATH}
