#!/bin/bash
# (Copyright) [2017 - 2017] Confluent, Inc.

#
# Use shellcheck to lint this file
#

if [ $# -lt 1 ]; 
then
    echo "USAGE: $0 [-daemon] ksql-server.properties"
    exit 1
fi

base_dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." && pwd )
: "${KSQL_CONFIG_DIR:="$base_dir/config"}"

: "${KSQL_LOG4J_OPTS:=""}"
if [ -z "$KSQL_LOG4J_OPTS" ] && [ -e "$KSQL_CONFIG_DIR/log4j-rolling.properties" ]; then
  export KSQL_LOG4J_OPTS="-Dlog4j.configuration=file:$KSQL_CONFIG_DIR/log4j-rolling.properties"
fi

EXTRA_ARGS="-name ksql-server -loggc"

if [ -z "$KSQL_OPTS" ]; then
  KSQL_OPTS=-Dksql.server.install.dir=$base_dir
else
  KSQL_OPTS="${KSQL_OPTS} -Dksql.server.install.dir=$base_dir"
fi

export KSQL_OPTS

exec "$base_dir"/bin/ksql-run-class io.confluent.ksql.rest.server.KsqlServerMain $EXTRA_ARGS "$@"
