#!/usr/bin/env bash
#
#/**
# * 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.
# */

# BookKeeper CLI (experimental)

BINDIR=`dirname "$0"`
BK_HOME=`cd ${BINDIR}/..;pwd`
mkdir -p $BK_HOME/logs

source ${BK_HOME}/bin/common.sh
source ${BK_HOME}/conf/bk_cli_env.sh

CLI_MODULE_PATH=tools/all
CLI_MODULE_NAME="(org.apache.bookkeeper-)?bookkeeper-tools"
CLI_MODULE_HOME=${BK_HOME}/${CLI_MODULE_PATH}

# find the module jar
CLI_JAR=$(find_module_jar ${CLI_MODULE_PATH} ${CLI_MODULE_NAME})

# set up the classpath
CLI_CLASSPATH=$(set_module_classpath ${CLI_MODULE_PATH})

DEFAULT_CONF=${BK_HOME}/conf/bk_server.conf
if [ -z "${CLI_CONF}" ]; then
  CLI_CONF=${DEFAULT_CONF}
fi

DEFAULT_LOG_CONF=${BK_HOME}/conf/log4j2.cli.xml
if [ -z "${CLI_LOG_CONF}" ]; then
  CLI_LOG_CONF=${DEFAULT_LOG_CONF}
fi
CLI_LOG_DIR=${CLI_LOG_DIR:-"$BK_HOME/logs"}
CLI_LOG_FILE=${CLI_LOG_FILE:-"bkctl.log"}
CLI_ROOT_LOG_LEVEL=${CLI_ROOT_LOG_LEVEL:-"INFO"}
CLI_ROOT_LOG_APPENDER=${CLI_ROOT_LOG_APPENDER:-"CONSOLE"}

# Configure the classpath
CLI_CLASSPATH="$CLI_JAR:$CLI_CLASSPATH:$CLI_EXTRA_CLASSPATH"
CLI_CLASSPATH="`dirname $CLI_LOG_CONF`:$CLI_CLASSPATH"

# Build the OPTs
BOOKIE_OPTS=$(build_bookie_opts)
GC_OPTS=$(build_cli_jvm_opts ${CLI_LOG_DIR} "bkctl-gc.log")
NETTY_OPTS=$(build_netty_opts)
LOGGING_OPTS=$(build_cli_logging_opts ${CLI_LOG_CONF} ${CLI_ROOT_LOG_LEVEL} ${CLI_ROOT_LOG_APPENDER} ${CLI_LOG_DIR} ${CLI_LOG_FILE})

OPTS="${OPTS} -cp ${CLI_CLASSPATH} ${BOOKIE_OPTS} ${GC_OPTS} ${NETTY_OPTS} ${LOGGING_OPTS} ${CLI_EXTRA_OPTS}"

#Change to BK_HOME to support relative paths
cd "$BK_HOME"
exec "${JAVA}" ${OPTS} org.apache.bookkeeper.tools.cli.BKCtl --conf ${CLI_CONF} $@
