#!/bin/sh

set -e

# For Debian/Ubuntu based systems.
if [ -f "/etc/default/graylog-server" ]; then
    . "/etc/default/graylog-server"
fi

# For RedHat/Fedora based systems.
if [ -f "/etc/sysconfig/graylog-server" ]; then
    . "/etc/sysconfig/graylog-server"
fi

if [ -f "/usr/share/graylog-server/installation-source.sh" ]; then
    . "/usr/share/graylog-server/installation-source.sh"
fi

# Java versions > 8 don't support UseParNewGC
if ${JAVA:=/usr/bin/java} -XX:+PrintFlagsFinal 2>&1 | grep -q UseParNewGC; then
        GRAYLOG_SERVER_JAVA_OPTS="$GRAYLOG_SERVER_JAVA_OPTS -XX:+UseParNewGC"
fi

$GRAYLOG_COMMAND_WRAPPER ${JAVA:=/usr/bin/java} $GRAYLOG_SERVER_JAVA_OPTS \
    -cp /usr/share/graylog-server/graylog.jar{% if graylog_libs.keys() | list | length > 0 %}:{% for lib in graylog_libs.keys() | list %}:{{ graylog_root_dir }}/libs/{{ lib }}-{{ graylog_libs[lib].version }}.jar{% endfor %} {% endif %} -Dlog4j.configurationFile=file://{{ graylog_root_dir }}/etc/log4j2.xml \
    -Djava.library.path=/usr/share/graylog-server/lib/sigar \
    -Dgraylog2.installation_source=${GRAYLOG_INSTALLATION_SOURCE:=unknown} \
    org.graylog2.bootstrap.Main server -f {{ graylog_root_dir }}/etc/server.conf -np \
    $GRAYLOG_SERVER_ARGS
